Skip to content

Commit

Permalink
Prefixed constants
Browse files Browse the repository at this point in the history
  • Loading branch information
xy2z committed Nov 20, 2023
1 parent c45b06c commit 32f4745
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 49 deletions.
24 changes: 12 additions & 12 deletions capro
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ if (file_exists(__DIR__ . '/vendor/autoload.php')) {
// Development (when developing/testing this project),
// using `php capro <cmd>`
require __DIR__ . '/vendor/autoload.php';
define('SITE_ROOT_DIR', __DIR__ . DIRECTORY_SEPARATOR);
define('CAPRO_SITE_ROOT_DIR', __DIR__ . DIRECTORY_SEPARATOR);
}

if (file_exists(__DIR__ . '/../../autoload.php')) {
// For sites that is using this using vendor/bin
// Also for global installed..
require __DIR__ . '/../../autoload.php';
define('SITE_ROOT_DIR', __DIR__ . '/../../../'); // go back 3 dirs (/vendor/xy2z/capro/)
define('CAPRO_SITE_ROOT_DIR', __DIR__ . '/../../../'); // go back 3 dirs (/vendor/xy2z/capro/)
}

if (!defined('SITE_ROOT_DIR')) {
if (!defined('CAPRO_SITE_ROOT_DIR')) {
echo 'Error: Composer autoload.php was not found. (try "composer install")' . PHP_EOL;
exit(1);
}
Expand All @@ -36,9 +36,9 @@ if (!defined('SITE_ROOT_DIR')) {
class_alias('xy2z\Capro\Capro', 'Capro');

// Load env (before config)
if (file_exists(SITE_ROOT_DIR . '.env')) {
if (file_exists(CAPRO_SITE_ROOT_DIR . '.env')) {
// Load .env file
$dotenv = Dotenv\Dotenv::createImmutable(SITE_ROOT_DIR);
$dotenv = Dotenv\Dotenv::createImmutable(CAPRO_SITE_ROOT_DIR);
try {
$dotenv->load();
} catch (Exception $e) {
Expand All @@ -48,19 +48,19 @@ if (file_exists(SITE_ROOT_DIR . '.env')) {
}

// Load config
define('CONFIG_DIR', SITE_ROOT_DIR . 'config');
if (is_dir(CONFIG_DIR)) {
define('CAPRO_CONFIG_DIR', CAPRO_SITE_ROOT_DIR . 'config');
if (is_dir(CAPRO_CONFIG_DIR)) {
Config::load_capro_config();
// } else {
// tell('Notice: Config dir not found: ' . CONFIG_DIR); // verbose. (but never for global composer "capro" commands.)
// tell('Notice: Config dir not found: ' . CAPRO_CONFIG_DIR); // verbose. (but never for global composer "capro" commands.)
}

// Set CONSTANTS from Config (or use defaults) -- after loading config.
// Cant use realpath() here because the directories might not exist yet.
define('PUBLIC_DIR', SITE_ROOT_DIR . Config::get('core.public_dir', 'public'));
define('VIEWS_DIR', SITE_ROOT_DIR . Config::get('core.views_dir', 'views'));
define('VIEWS_CACHE_DIR', SITE_ROOT_DIR . Config::get('core.views_cache_dir', 'views/cache'));
define('STATIC_DIR', SITE_ROOT_DIR . Config::get('core.static_dir', 'static'));
define('CAPRO_PUBLIC_DIR', CAPRO_SITE_ROOT_DIR . Config::get('core.public_dir', 'public'));
define('CAPRO_VIEWS_DIR', CAPRO_SITE_ROOT_DIR . Config::get('core.views_dir', 'views'));
define('CAPRO_VIEWS_CACHE_DIR', CAPRO_SITE_ROOT_DIR . Config::get('core.views_cache_dir', 'views/cache'));
define('CAPRO_STATIC_DIR', CAPRO_SITE_ROOT_DIR . Config::get('core.static_dir', 'static'));

// Check if command argument is set.
$command = $argv[1] ?? null;
Expand Down
26 changes: 13 additions & 13 deletions src/Commands/CommandBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function run(): void {
validate_in_capro_dir();

// Validate dirs exists
if (!is_dir(VIEWS_DIR)) {
tell_error('Views dir not found: ' . VIEWS_DIR);
if (!is_dir(CAPRO_VIEWS_DIR)) {
tell_error('Views dir not found: ' . CAPRO_VIEWS_DIR);
}

// Start
Expand All @@ -58,7 +58,7 @@ public function run(): void {
private function rm_public_dir(): void {
// Remove all content in the public dir (to keep chmod and chown)
try {
rm_dir_content(PUBLIC_DIR);
rm_dir_content(CAPRO_PUBLIC_DIR);
} catch (\Exception $e) {
tell_error('Error: ' . $e->getMessage() . ' - is the file/dir open?');
}
Expand All @@ -67,11 +67,11 @@ private function rm_public_dir(): void {

private function check_cache_dir(): void {
// Make cache dir if not exists.
if (!is_dir(VIEWS_CACHE_DIR)) {
if (mkdir(VIEWS_CACHE_DIR, 0775, true)) {
tell('✔ Created cache dir: ' . realpath(VIEWS_CACHE_DIR));
if (!is_dir(CAPRO_VIEWS_CACHE_DIR)) {
if (mkdir(CAPRO_VIEWS_CACHE_DIR, 0775, true)) {
tell('✔ Created cache dir: ' . realpath(CAPRO_VIEWS_CACHE_DIR));
} else {
tell_error('Error: Could not create cache dir: ' . VIEWS_CACHE_DIR);
tell_error('Error: Could not create cache dir: ' . CAPRO_VIEWS_CACHE_DIR);
}
return;
}
Expand All @@ -81,7 +81,7 @@ private function check_cache_dir(): void {
// Remove all content in the cache dir.
/*
try {
rm_dir_content(VIEWS_CACHE_DIR);
rm_dir_content(CAPRO_VIEWS_CACHE_DIR);
} catch (\Exception $e) {
tell_error('Error: ' . $e->getMessage() . ' - is the file/dir open?');
}
Expand All @@ -91,12 +91,12 @@ private function check_cache_dir(): void {
}

private function copy_static_dir_to_public(): void {
if (!is_dir(STATIC_DIR)) {
if (!is_dir(CAPRO_STATIC_DIR)) {
// tell('Notice: Static dir not found: ' . STATIC_DIR); // verbose. (but never for "capro new" commands)
return;
}

rcopy(STATIC_DIR, PUBLIC_DIR);
rcopy(CAPRO_STATIC_DIR, CAPRO_PUBLIC_DIR);
// tell('✔ Copied static content in to public directory.'); // verbose.
}

Expand All @@ -115,7 +115,7 @@ private static function is_filename_ignored(string $filename): bool {

private function load_views(): void {
// Load Pages (dont build)
$page_dir = VIEWS_DIR . DIRECTORY_SEPARATOR . 'pages'; // No trailing slashes!
$page_dir = CAPRO_VIEWS_DIR . DIRECTORY_SEPARATOR . 'pages'; // No trailing slashes!
foreach ($this->get_all_pages($page_dir) as $file) {
$view = new View(
path: $file->path,
Expand All @@ -131,7 +131,7 @@ private function load_views(): void {
}

// Load Collections (dont build)
$collections_dir = VIEWS_DIR . DIRECTORY_SEPARATOR . 'collections';
$collections_dir = CAPRO_VIEWS_DIR . DIRECTORY_SEPARATOR . 'collections';
foreach ($this->get_all_collections_views($collections_dir) as $file) {
$view = new View(
path: $file->path,
Expand Down Expand Up @@ -201,7 +201,7 @@ protected function build_views(): void {
}

// Delete temporary file build.
$tmp_path = VIEWS_DIR . '/__tmp.blade.php';
$tmp_path = CAPRO_VIEWS_DIR . '/__tmp.blade.php';
@unlink($tmp_path);
}

Expand Down
14 changes: 7 additions & 7 deletions src/Commands/CommandServe.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public function run(): void {
// Prepare file watcher, to watch for any changes.
$this->FileWatcher = new FileWatcher([
// Watch dirs
VIEWS_DIR,
STATIC_DIR,
CONFIG_DIR,
CAPRO_VIEWS_DIR,
CAPRO_STATIC_DIR,
CAPRO_CONFIG_DIR,
], [
// Exclude paths
VIEWS_CACHE_DIR,
CAPRO_VIEWS_CACHE_DIR,
]);

while ($this->server->isRunning()) {
Expand All @@ -77,10 +77,10 @@ private function start_server(): void {
$this->server = Server::new()
->host($this->host)
->port($this->port)
->root(PUBLIC_DIR);
->root(CAPRO_PUBLIC_DIR);

if (file_exists(SITE_ROOT_DIR . '/.env')) {
$this->server->withEnvFile(SITE_ROOT_DIR . '/.env');
if (file_exists(CAPRO_SITE_ROOT_DIR . '/.env')) {
$this->server->withEnvFile(CAPRO_SITE_ROOT_DIR . '/.env');
}

$this->server->runInBackground();
Expand Down
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected static function custom_handler(string $extension, string $path): mixed
}

public static function load_capro_config(): void {
self::loadDir(CONFIG_DIR, true);
self::loadDir(CAPRO_CONFIG_DIR, true);
}

public static function reload(): void {
Expand Down
18 changes: 9 additions & 9 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(string $path, int $type, string $label = '', string

$this->label = $label; // "collection" label.
$this->basename = str_replace('.blade.php', '', basename($this->path));
$this->relative_path = str_replace(SITE_ROOT_DIR, '', $this->path);
$this->relative_path = str_replace(CAPRO_SITE_ROOT_DIR, '', $this->path);

$file_content = @file_get_contents($this->path) ?: '';

Expand Down Expand Up @@ -86,33 +86,33 @@ private function set_save_path(): void {
}

if ($this->type === self::TYPE_PAGE) {
$relative_dir = str_replace(VIEWS_DIR . DIRECTORY_SEPARATOR . 'pages', '', $this->dir);
$relative_dir = str_replace(CAPRO_VIEWS_DIR . DIRECTORY_SEPARATOR . 'pages', '', $this->dir);
} elseif ($this->type === self::TYPE_COLLECTION) {
$relative_dir = str_replace(VIEWS_DIR . DIRECTORY_SEPARATOR . 'collections', '', $this->dir);
$relative_dir = str_replace(CAPRO_VIEWS_DIR . DIRECTORY_SEPARATOR . 'collections', '', $this->dir);
} else {
throw new Exception('Unknown View type.');
}

if (!empty($this->get_view_data('core.save_as'))) {
$save_as = $this->get_view_data('core.save_as');
$this->href = str_replace('\\', '/', $relative_dir) . '/' . $save_as;
$this->save_path = PUBLIC_DIR . $relative_dir . DIRECTORY_SEPARATOR . $save_as;
$this->save_path = CAPRO_PUBLIC_DIR . $relative_dir . DIRECTORY_SEPARATOR . $save_as;
return;
}

if (($this->basename == 'index') && empty($relative_dir)) {
// Root index.html
$this->href = '/';
$this->save_path = PUBLIC_DIR . DIRECTORY_SEPARATOR . 'index.html';
$this->save_path = CAPRO_PUBLIC_DIR . DIRECTORY_SEPARATOR . 'index.html';
} else {
if ($this->basename == 'index') {
// Place the file as index.html
$this->href = str_replace('\\', '/', $relative_dir) . '/';
$this->save_path = PUBLIC_DIR . $relative_dir . DIRECTORY_SEPARATOR . 'index.html';
$this->save_path = CAPRO_PUBLIC_DIR . $relative_dir . DIRECTORY_SEPARATOR . 'index.html';
} else {
// Not "index" file, so make a dir for the basename.
$this->href = str_replace('\\', '/', $relative_dir) . '/' . $this->basename . '/';
$this->save_path = PUBLIC_DIR . $relative_dir . DIRECTORY_SEPARATOR . $this->basename . DIRECTORY_SEPARATOR . 'index.html';
$this->save_path = CAPRO_PUBLIC_DIR . $relative_dir . DIRECTORY_SEPARATOR . $this->basename . DIRECTORY_SEPARATOR . 'index.html';
}
}
}
Expand Down Expand Up @@ -165,7 +165,7 @@ public function build(): bool {
// Save the file without the yaml-front-matter in a temp location.
// This is needed because Blade cannot make from a string, it needs to be an actual file.
// TODO: Refactor when possible.
$saved = file_put_contents(VIEWS_DIR . '/__tmp.blade.php', $this->yaml_front_matter->body());
$saved = file_put_contents(CAPRO_VIEWS_DIR . '/__tmp.blade.php', $this->yaml_front_matter->body());

if ($saved === false) {
throw new Exception('Could not build view.'); // TODO: If this is in CommandServe, just retry in a few microseconds...
Expand Down Expand Up @@ -195,7 +195,7 @@ protected static function load_blade(): void {
return;
}

self::$blade = new Blade(VIEWS_DIR, VIEWS_CACHE_DIR);
self::$blade = new Blade(CAPRO_VIEWS_DIR, CAPRO_VIEWS_CACHE_DIR);
class_alias('xy2z\\Capro\\Config', 'Config');

self::$blade->directive('markdown', function () {
Expand Down
4 changes: 2 additions & 2 deletions src/ViewTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ protected function get_item_path(array $item): string {
/** @param array<mixed> $item */
protected function get_public_path(array $item): string {
$path = $this->get_item_path($item);
return PUBLIC_DIR . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . 'index.html';
return CAPRO_PUBLIC_DIR . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . 'index.html';
}

public function load_views(): Generator {
$this->template_view_path = VIEWS_DIR . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->template_view . '.blade.php';
$this->template_view_path = CAPRO_VIEWS_DIR . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->template_view . '.blade.php';

if (!file_exists($this->template_view_path)) {
tell_error('Error: Template view not found: ' . $this->template_view_path);
Expand Down
10 changes: 5 additions & 5 deletions tests/CollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use xy2z\Capro\PublicView;
use xy2z\Capro\View;

define('SITE_ROOT_DIR', __DIR__ . '/../');
define('PUBLIC_DIR', SITE_ROOT_DIR . 'public');
define('VIEWS_DIR', SITE_ROOT_DIR . 'views');
define('VIEWS_CACHE_DIR', SITE_ROOT_DIR . 'views/cache');
define('STATIC_DIR', SITE_ROOT_DIR . 'static');
define('CAPRO_SITE_ROOT_DIR', __DIR__ . '/../');
define('CAPRO_PUBLIC_DIR', CAPRO_SITE_ROOT_DIR . 'public');
define('CAPRO_VIEWS_DIR', CAPRO_SITE_ROOT_DIR . 'views');
define('CAPRO_VIEWS_CACHE_DIR', CAPRO_SITE_ROOT_DIR . 'views/cache');
define('CAPRO_STATIC_DIR', CAPRO_SITE_ROOT_DIR . 'static');

class CollectorTest extends TestCase {
public function setUp(): void {
Expand Down

0 comments on commit 32f4745

Please sign in to comment.