From 32f4745de297a2ef8f3b717e25129b8629ea10c7 Mon Sep 17 00:00:00 2001 From: xy2z Date: Mon, 20 Nov 2023 20:50:41 +0100 Subject: [PATCH] Prefixed constants --- capro | 24 ++++++++++++------------ src/Commands/CommandBuild.php | 26 +++++++++++++------------- src/Commands/CommandServe.php | 14 +++++++------- src/Config.php | 2 +- src/View.php | 18 +++++++++--------- src/ViewTemplate.php | 4 ++-- tests/CollectorTest.php | 10 +++++----- 7 files changed, 49 insertions(+), 49 deletions(-) diff --git a/capro b/capro index 8a26267..d6bbaaa 100644 --- a/capro +++ b/capro @@ -17,17 +17,17 @@ if (file_exists(__DIR__ . '/vendor/autoload.php')) { // Development (when developing/testing this project), // using `php capro ` 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); } @@ -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) { @@ -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; diff --git a/src/Commands/CommandBuild.php b/src/Commands/CommandBuild.php index e1539d1..2a746fb 100644 --- a/src/Commands/CommandBuild.php +++ b/src/Commands/CommandBuild.php @@ -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 @@ -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?'); } @@ -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; } @@ -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?'); } @@ -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. } @@ -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, @@ -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, @@ -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); } diff --git a/src/Commands/CommandServe.php b/src/Commands/CommandServe.php index 65e80a2..c870c95 100644 --- a/src/Commands/CommandServe.php +++ b/src/Commands/CommandServe.php @@ -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()) { @@ -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(); diff --git a/src/Config.php b/src/Config.php index f7b7211..41f4eac 100644 --- a/src/Config.php +++ b/src/Config.php @@ -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 { diff --git a/src/View.php b/src/View.php index 1a5ef83..7cd5a70 100644 --- a/src/View.php +++ b/src/View.php @@ -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) ?: ''; @@ -86,9 +86,9 @@ 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.'); } @@ -96,23 +96,23 @@ private function set_save_path(): void { 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'; } } } @@ -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... @@ -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 () { diff --git a/src/ViewTemplate.php b/src/ViewTemplate.php index 47c3f67..05d6b04 100644 --- a/src/ViewTemplate.php +++ b/src/ViewTemplate.php @@ -45,11 +45,11 @@ protected function get_item_path(array $item): string { /** @param array $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); diff --git a/tests/CollectorTest.php b/tests/CollectorTest.php index 428a57e..73a4d52 100644 --- a/tests/CollectorTest.php +++ b/tests/CollectorTest.php @@ -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 {