From b6c4045b6528fe29f4f18b2a98005b6de0db7c76 Mon Sep 17 00:00:00 2001 From: Awilum Date: Wed, 8 Apr 2020 19:48:45 +0300 Subject: [PATCH] refactor(core): constants refactoring --- index.php | 8 +------ src/flextype/bootstrap.php | 4 ++-- src/flextype/core/Entries/Entries.php | 6 ++--- src/flextype/core/Plugins/Plugins.php | 23 ++++++++++---------- src/flextype/dependencies.php | 2 +- src/flextype/endpoints/delivery/entries.php | 4 ++-- src/flextype/endpoints/delivery/images.php | 6 ++--- src/flextype/endpoints/delivery/registry.php | 4 ++-- 8 files changed, 26 insertions(+), 31 deletions(-) diff --git a/index.php b/index.php index 80612abf..23af5b8d 100755 --- a/index.php +++ b/index.php @@ -33,13 +33,7 @@ define('ROOT_DIR', str_replace(DIRECTORY_SEPARATOR, '/', getcwd())); */ define('PATH', [ 'site' => ROOT_DIR . '/site', - 'plugins' => ROOT_DIR . '/site/plugins', - 'entries' => ROOT_DIR . '/site/entries', - 'tokens' => ROOT_DIR . '/site/tokens', - 'config' => [ - 'default' => ROOT_DIR . '/src/flextype/config', - 'site' => ROOT_DIR . '/site/config', - ], + 'config' => ROOT_DIR . '/src/flextype/config', 'cache' => ROOT_DIR . '/var/cache', 'logs' => ROOT_DIR . '/var/logs', ]); diff --git a/src/flextype/bootstrap.php b/src/flextype/bootstrap.php index 41b3e583..f29b5688 100755 --- a/src/flextype/bootstrap.php +++ b/src/flextype/bootstrap.php @@ -43,8 +43,8 @@ $registry = new Registry(); * 3. Merge settings. * 4. Store settings in the flextype registry. */ -$default_flextype_settings_file_path = PATH['config']['default'] . '/settings.yaml'; -$custom_flextype_settings_file_path = PATH['config']['site'] . '/settings.yaml'; +$default_flextype_settings_file_path = PATH['config'] . '/settings.yaml'; +$custom_flextype_settings_file_path = PATH['site'] . '/config/settings.yaml'; // Create config dir ! Filesystem::has(PATH['site'] . '/config/') and Filesystem::createDir(PATH['site'] . '/config/'); diff --git a/src/flextype/core/Entries/Entries.php b/src/flextype/core/Entries/Entries.php index 291e5d7c..15ec40bc 100755 --- a/src/flextype/core/Entries/Entries.php +++ b/src/flextype/core/Entries/Entries.php @@ -329,7 +329,7 @@ class Entries // Get entry uid // 1. Remove entries path // 2. Remove left and right slashes - $uid = ltrim(rtrim(str_replace(PATH['entries'], '', $current_entry['path']), '/'), '/'); + $uid = ltrim(rtrim(str_replace(PATH['site'] . '/entries/', '', $current_entry['path']), '/'), '/'); // For each founded entry we should create $entries array. $entry = $this->fetch($uid); @@ -585,7 +585,7 @@ class Entries */ public function getFileLocation(string $id) : string { - return PATH['entries'] . '/' . $id . '/entry.md'; + return PATH['site'] . '/entries/' . $id . '/entry.md'; } /** @@ -599,6 +599,6 @@ class Entries */ public function getDirLocation(string $id) : string { - return PATH['entries'] . '/' . $id; + return PATH['site'] . '/entries/' . $id; } } diff --git a/src/flextype/core/Plugins/Plugins.php b/src/flextype/core/Plugins/Plugins.php index b4cbcd3b..4bd8ba66 100755 --- a/src/flextype/core/Plugins/Plugins.php +++ b/src/flextype/core/Plugins/Plugins.php @@ -106,14 +106,14 @@ class Plugins // Go through... foreach ($plugins_list as $plugin) { // Set plugin settings directory - $site_plugin_settings_dir = PATH['config']['site'] . '/plugins/' . $plugin['dirname']; + $site_plugin_settings_dir = PATH['config'] . '/site/plugins/' . $plugin['dirname']; // Set default plugin settings and manifest files - $default_plugin_settings_file = PATH['plugins'] . '/' . $plugin['dirname'] . '/settings.yaml'; - $default_plugin_manifest_file = PATH['plugins'] . '/' . $plugin['dirname'] . '/plugin.yaml'; + $default_plugin_settings_file = PATH['site'] . '/plugins/' . $plugin['dirname'] . '/settings.yaml'; + $default_plugin_manifest_file = PATH['site'] . '/plugins/' . $plugin['dirname'] . '/plugin.yaml'; // Set site plugin settings file - $site_plugin_settings_file = PATH['config']['site'] . '/plugins/' . $plugin['dirname'] . '/settings.yaml'; + $site_plugin_settings_file = PATH['site'] . '/config/plugins/' . $plugin['dirname'] . '/settings.yaml'; // Create site plugin settings directory ! Filesystem::has($site_plugin_settings_dir) and Filesystem::createDir($site_plugin_settings_dir); @@ -180,6 +180,7 @@ class Plugins // Save plugins dictionary $dictionary = $this->getPluginsDictionary($plugins_list, $locale); $this->flextype['cache']->save($locale, $dictionary[$locale]); + } $this->includeEnabledPlugins($flextype, $app); @@ -197,7 +198,7 @@ class Plugins private function getPluginsDictionary(array $plugins_list, string $locale) : array { foreach ($plugins_list as $plugin) { - $language_file = PATH['plugins'] . '/' . $plugin['dirname'] . '/lang/' . $locale . '.yaml'; + $language_file = PATH['site'] . '/plugins/' . $plugin['dirname'] . '/lang/' . $locale . '.yaml'; if (! Filesystem::has($language_file)) { continue; @@ -230,9 +231,9 @@ class Plugins // Go through... if (is_array($plugins_list) && count($plugins_list) > 0) { foreach ($plugins_list as $plugin) { - $default_plugin_settings_file = PATH['plugins'] . '/' . $plugin['dirname'] . '/settings.yaml'; - $default_plugin_manifest_file = PATH['plugins'] . '/' . $plugin['dirname'] . '/plugin.yaml'; - $site_plugin_settings_file = PATH['config']['site'] . '/plugins/' . $plugin['dirname'] . '/settings.yaml'; + $default_plugin_settings_file = PATH['site'] . '/plugins/' . $plugin['dirname'] . '/settings.yaml'; + $default_plugin_manifest_file = PATH['site'] . '/plugins/' . $plugin['dirname'] . '/plugin.yaml'; + $site_plugin_settings_file = PATH['config'] . '/site/plugins/' . $plugin['dirname'] . '/settings.yaml'; $f1 = Filesystem::has($default_plugin_settings_file) ? filemtime($default_plugin_settings_file) : ''; $f2 = Filesystem::has($default_plugin_manifest_file) ? filemtime($default_plugin_manifest_file) : ''; @@ -243,7 +244,7 @@ class Plugins } // Create Unique Cache ID for Plugins - $plugins_cache_id = md5('plugins' . PATH['plugins'] . '/' . $_plugins_cache_id); + $plugins_cache_id = md5('plugins' . PATH['site'] . '/plugins/' . $_plugins_cache_id); // Return plugin cache id return $plugins_cache_id; @@ -307,7 +308,7 @@ class Plugins // Get Plugins List $plugins_list = []; - foreach (Filesystem::listContents(PATH['plugins']) as $plugin) { + foreach (Filesystem::listContents(PATH['site'] . '/plugins/') as $plugin) { if ($plugin['type'] !== 'dir') { continue; } @@ -334,7 +335,7 @@ class Plugins continue; } - include_once PATH['plugins'] . '/' . $plugin_name . '/bootstrap.php'; + include_once PATH['site'] . '/plugins/' . $plugin_name . '/bootstrap.php'; } } } diff --git a/src/flextype/dependencies.php b/src/flextype/dependencies.php index 70af9be7..10d14287 100644 --- a/src/flextype/dependencies.php +++ b/src/flextype/dependencies.php @@ -128,7 +128,7 @@ $flextype['images'] = static function ($container) { // Set source filesystem $source = new Filesystem( - new Local(PATH['uploads'] . '/entries/') + new Local(PATH['site'] . '/uploads/entries/') ); // Set cache filesystem diff --git a/src/flextype/endpoints/delivery/entries.php b/src/flextype/endpoints/delivery/entries.php index 15ff3f7b..e5611313 100644 --- a/src/flextype/endpoints/delivery/entries.php +++ b/src/flextype/endpoints/delivery/entries.php @@ -20,7 +20,7 @@ use function count; */ function validate_delivery_entries_token($request, $flextype) : bool { - return Filesystem::has(PATH['tokens'] . '/delivery/entries/' . $request->getQueryParams()['token'] . '/token.yaml'); + return Filesystem::has(PATH['site'] . '/tokens/delivery/entries/' . $request->getQueryParams()['token'] . '/token.yaml'); } /** @@ -39,7 +39,7 @@ $app->get('/api/delivery/entries', function (Request $request, Response $respons if ($flextype['registry']->get('flextype.api.entries.enabled')) { // Validate delivery token if (validate_delivery_entries_token($request, $flextype)) { - $delivery_entries_token_file_path = PATH['tokens'] . '/delivery/entries/' . $request->getQueryParams()['token'] . '/token.yaml'; + $delivery_entries_token_file_path = PATH['site'] . '/tokens/delivery/entries/' . $request->getQueryParams()['token'] . '/token.yaml'; // Set delivery token file if ($delivery_entries_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_entries_token_file_path), 'yaml')) { diff --git a/src/flextype/endpoints/delivery/images.php b/src/flextype/endpoints/delivery/images.php index 6398703d..6a4d3103 100644 --- a/src/flextype/endpoints/delivery/images.php +++ b/src/flextype/endpoints/delivery/images.php @@ -20,7 +20,7 @@ use function header; */ function validate_delivery_images_token($request, $flextype) : bool { - return Filesystem::has(PATH['tokens'] . '/delivery/images/' . $request->getQueryParams()['token'] . '/token.yaml'); + return Filesystem::has(PATH['site'] . '/tokens/delivery/images/' . $request->getQueryParams()['token'] . '/token.yaml'); } /** @@ -35,7 +35,7 @@ $app->get('/api/delivery/images/{path:.+}', function (Request $request, Response if ($flextype['registry']->get('flextype.api.images.enabled')) { // Validate delivery image token if (validate_delivery_images_token($request, $flextype)) { - $delivery_images_token_file_path = PATH['tokens'] . '/delivery/images/' . $request->getQueryParams()['token'] . '/token.yaml'; + $delivery_images_token_file_path = PATH['site'] . '/site/delivery/images/' . $request->getQueryParams()['token'] . '/token.yaml'; // Set delivery token file if ($delivery_images_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_images_token_file_path), 'yaml')) { @@ -47,7 +47,7 @@ $app->get('/api/delivery/images/{path:.+}', function (Request $request, Response // Update calls counter Filesystem::write($delivery_images_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_images_token_file_data, ['calls' => $delivery_images_token_file_data['calls'] + 1]), 'yaml')); - if (Filesystem::has(PATH['uploads'] . '/entries/' . $args['path'])) { + if (Filesystem::has(PATH['site'] . '/uploads/entries/' . $args['path'])) { header('Access-Control-Allow-Origin: *'); return $flextype['images']->getImageResponse($args['path'], $_GET); diff --git a/src/flextype/endpoints/delivery/registry.php b/src/flextype/endpoints/delivery/registry.php index 697f44c4..f7f3fb0a 100644 --- a/src/flextype/endpoints/delivery/registry.php +++ b/src/flextype/endpoints/delivery/registry.php @@ -19,7 +19,7 @@ use function array_replace_recursive; */ function validate_delivery_registry_token($request, $flextype) : bool { - return Filesystem::has(PATH['tokens'] . '/delivery/registry/' . $request->getQueryParams()['token'] . '/token.yaml'); + return Filesystem::has(PATH['site'] . '/tokens/delivery/registry/' . $request->getQueryParams()['token'] . '/token.yaml'); } /** @@ -37,7 +37,7 @@ $app->get('/api/delivery/registry', function (Request $request, Response $respon if ($flextype['registry']->get('flextype.api.registry.enabled')) { // Validate delivery token if (validate_delivery_registry_token($request, $flextype)) { - $delivery_registry_token_file_path = PATH['tokens'] . '/delivery/registry/' . $request->getQueryParams()['token'] . '/token.yaml'; + $delivery_registry_token_file_path = PATH['site'] . '/tokens/delivery/registry/' . $request->getQueryParams()['token'] . '/token.yaml'; // Set delivery token file if ($delivery_registry_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_registry_token_file_path), 'yaml')) {