From 4d05553ffc07a4e1473b313bca2aeb628d51711b Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 11 Dec 2018 13:57:30 +0300 Subject: [PATCH] Settings - cleanup and refactoring --- flextype/Cache.php | 28 +++++++-------- flextype/Content.php | 8 ++--- flextype/Flextype.php | 34 +++++++------------ flextype/Themes.php | 16 ++++----- site/themes/simple/views/partials/head.php | 16 ++++----- .../simple/views/partials/navigation.php | 2 +- site/themes/simple/views/partials/tail.php | 2 +- 7 files changed, 48 insertions(+), 58 deletions(-) diff --git a/flextype/Cache.php b/flextype/Cache.php index 8ae125d1..4fc7fb94 100755 --- a/flextype/Cache.php +++ b/flextype/Cache.php @@ -94,7 +94,7 @@ class Cache Cache::$now = time(); // Create cache key to allow invalidate all cache on configuration changes. - Cache::$key = (Registry::get('system.cache.prefix') ?? 'flextype') . '-' . md5(PATH['site'] . Flextype::VERSION); + Cache::$key = (Registry::get('settings.cache.prefix') ?? 'flextype') . '-' . md5(PATH['site'] . Flextype::VERSION); // Get Cache Driver Cache::$driver = Cache::getCacheDriver(); @@ -111,7 +111,7 @@ class Cache */ public static function getCacheDriver() { - $driver_name = Registry::get('system.cache.driver'); + $driver_name = Registry::get('settings.cache.driver'); if (!$driver_name || $driver_name == 'auto') { if (extension_loaded('apcu')) { @@ -143,8 +143,8 @@ class Cache case 'memcache': $memcache = new \Memcache(); $memcache->connect( - Registry::get('system.cache.memcache.server', 'localhost'), - Registry::get('system.cache.memcache.port', 11211) + Registry::get('settings.cache.memcache.server', 'localhost'), + Registry::get('settings.cache.memcache.port', 11211) ); $driver = new DoctrineCache\MemcacheCache(); $driver->setMemcache($memcache); @@ -152,23 +152,23 @@ class Cache case 'memcached': $memcached = new \Memcached(); $memcached->addServer( - Registry::get('system.cache.memcached.server', 'localhost'), - Registry::get('system.cache.memcache.port', 11211) + Registry::get('settings.cache.memcached.server', 'localhost'), + Registry::get('settings.cache.memcache.port', 11211) ); $driver = new DoctrineCache\MemcachedCache(); $driver->setMemcached($memcached); break; case 'redis': $redis = new \Redis(); - $socket = Registry::get('system.cache.redis.socket', false); - $password = Registry::get('system.cache.redis.password', false); + $socket = Registry::get('settings.cache.redis.socket', false); + $password = Registry::get('settings.cache.redis.password', false); if ($socket) { $redis->connect($socket); } else { $redis->connect( - Registry::get('system.cache.redis.server', 'localhost'), - Registry::get('system.cache.redis.port', 6379) + Registry::get('settings.cache.redis.server', 'localhost'), + Registry::get('settings.cache.redis.port', 6379) ); } @@ -220,7 +220,7 @@ class Cache */ public static function fetch(string $id) { - if (Registry::get('system.cache.enabled')) { + if (Registry::get('settings.cache.enabled')) { return Cache::$driver->fetch($id); } else { return false; @@ -235,7 +235,7 @@ class Cache */ public static function contains($id) { - if (Registry::get('system.cache.enabled')) { + if (Registry::get('settings.cache.enabled')) { return Cache::$driver->contains(($id)); } else { return false; @@ -254,7 +254,7 @@ class Cache */ public static function save(string $id, $data, $lifetime = null) { - if (Registry::get('system.cache.enabled')) { + if (Registry::get('settings.cache.enabled')) { if ($lifetime === null) { $lifetime = Cache::getLifetime(); } @@ -305,7 +305,7 @@ class Cache public static function getLifetime() { if (Cache::$lifetime === null) { - Cache::$lifetime = Registry::get('system.cache.lifetime') ?: 604800; + Cache::$lifetime = Registry::get('settings.cache.lifetime') ?: 604800; } return Cache::$lifetime; diff --git a/flextype/Content.php b/flextype/Content.php index 5ee9a14e..f8e87270 100755 --- a/flextype/Content.php +++ b/flextype/Content.php @@ -158,7 +158,7 @@ class Content { // if $url is empty then set path for defined main page if ($url === '') { - $file_path = PATH['pages'] . '/' . Registry::get('system.pages.main') . '/page.html'; + $file_path = PATH['pages'] . '/' . Registry::get('settings.pages.main') . '/page.html'; } else { $file_path = PATH['pages'] . '/' . $url . '/page.html'; } @@ -354,7 +354,7 @@ class Content $url = str_replace('//', '/', $url); $url = str_replace('http:/', 'http://', $url); $url = str_replace('https:/', 'https://', $url); - $url = str_replace('/'.Registry::get('system.pages.main'), '', $url); + $url = str_replace('/'.Registry::get('settings.pages.main'), '', $url); $url = rtrim($url, '/'); $_page['url'] = $url; @@ -365,7 +365,7 @@ class Content $_page['slug'] = str_replace(Http::getBaseUrl(), '', $url); // Create page date item - $_page['date'] = $_page['date'] ?? date(Registry::get('system.date_format'), filemtime($file_path)); + $_page['date'] = $_page['date'] ?? date(Registry::get('settings.date_format'), filemtime($file_path)); // Create page content item with $page_content if ($ignore_content) { @@ -442,7 +442,7 @@ class Content */ private static function displayCurrentPage() : void { - Http::setRequestHeaders('Content-Type: text/html; charset='.Registry::get('system.charset')); + Http::setRequestHeaders('Content-Type: text/html; charset='.Registry::get('settings.charset')); Themes::view(empty(Content::$page['template']) ? 'templates/default' : 'templates/' . Content::$page['template']) ->assign('page', Content::$page, true) ->display(); diff --git a/flextype/Flextype.php b/flextype/Flextype.php index f938f64a..437b64ea 100755 --- a/flextype/Flextype.php +++ b/flextype/Flextype.php @@ -74,18 +74,18 @@ class Flextype // Turn on output buffering ob_start(); - Flextype::setSiteConfig(); + Flextype::setConfig(); // Set internal encoding function_exists('mb_language') and mb_language('uni'); - function_exists('mb_regex_encoding') and mb_regex_encoding(Registry::get('system.charset')); - function_exists('mb_internal_encoding') and mb_internal_encoding(Registry::get('system.charset')); + function_exists('mb_regex_encoding') and mb_regex_encoding(Registry::get('settings.charset')); + function_exists('mb_internal_encoding') and mb_internal_encoding(Registry::get('settings.charset')); // Set error handler Flextype::setErrorHandler(); // Set default timezone - date_default_timezone_set(Registry::get('system.timezone')); + date_default_timezone_set(Registry::get('settings.timezone')); // Start the session Session::start(); @@ -114,7 +114,7 @@ class Flextype private static function setErrorHandler() : void { // Display Errors - if (Registry::get('system.errors.display')) { + if (Registry::get('settings.errors.display')) { define('DEVELOPMENT', true); error_reporting(-1); } else { @@ -132,30 +132,20 @@ class Flextype } /** - * Set site config + * Set config * * @access private */ - private static function setSiteConfig() : void + private static function setConfig() : void { // Set empty site item - Registry::set('site', []); + Registry::set('settings', []); - // Set site items if site config exists - if (Filesystem::fileExists($site_config = PATH['config'] . '/' . 'site.yaml')) { - Registry::set('site', Yaml::parseFile($site_config)); + // Set settings items if settings config exists + if (Filesystem::fileExists($settings_config = PATH['config'] . '/' . 'settings.yaml')) { + Registry::set('settings', Yaml::parseFile($settings_config)); } else { - throw new \RuntimeException("Flextype site config file does not exist."); - } - - // Set empty system item - Registry::set('system', []); - - // Set site items if system config exists - if (Filesystem::fileExists($system_config = PATH['config'] . '/' . 'system.yaml')) { - Registry::set('system', Yaml::parseFile($system_config)); - } else { - throw new \RuntimeException("Flextype system config file does not exist."); + throw new \RuntimeException("Flextype settings config file does not exist."); } } diff --git a/flextype/Themes.php b/flextype/Themes.php index b46e60a8..a0df9f41 100644 --- a/flextype/Themes.php +++ b/flextype/Themes.php @@ -69,7 +69,7 @@ class Themes $theme_cache_id = ''; // Get current theme - $theme = Registry::get('system.theme'); + $theme = Registry::get('settings.theme'); // Set empty themes items Registry::set('themes', []); @@ -78,16 +78,16 @@ class Themes $theme_cache_id = md5('theme' . filemtime(PATH['themes'] .'/'. $theme . '/' . 'settings.yaml') . filemtime(PATH['themes'] .'/'. $theme . '/' . $theme . '.yaml')); - // Get Theme mafifest file and write to site.themes array + // Get Theme mafifest file and write to settings.themes array if (Cache::contains($theme_cache_id)) { - Registry::set('themes.'.Registry::get('system.theme'), Cache::fetch($theme_cache_id)); + Registry::set('themes.'.Registry::get('settings.theme'), Cache::fetch($theme_cache_id)); } else { if (Filesystem::fileExists($theme_settings = PATH['themes'] . '/' . $theme . '/' . 'settings.yaml') and Filesystem::fileExists($theme_config = PATH['themes'] . '/' . $theme . '/' . $theme . '.yaml')) { $theme_settings = Yaml::parseFile($theme_settings); $theme_config = Yaml::parseFile($theme_config); $_theme = array_merge($theme_settings, $theme_config); - Registry::set('themes.'.Registry::get('system.theme'), $_theme); + Registry::set('themes.'.Registry::get('settings.theme'), $_theme); Cache::save($theme_cache_id, $_theme); } } @@ -105,8 +105,8 @@ class Themes { // Set view file // From current theme folder or from plugin folder - if (Filesystem::fileExists(PATH['themes'] . '/' . Registry::get('system.theme') . '/views/' . $template . View::$view_ext)) { - $template = PATH['themes'] . '/' . Registry::get('system.theme') . '/views/' . $template; + if (Filesystem::fileExists(PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/' . $template . View::$view_ext)) { + $template = PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/' . $template; } else { $template = PATH['plugins'] . '/' . $template; } @@ -126,7 +126,7 @@ class Themes $templates = []; // Get templates files - $_templates = Filesystem::getFilesList(PATH['themes'] . '/' . Registry::get('system.theme') . '/views/templates/', 'php'); + $_templates = Filesystem::getFilesList(PATH['themes'] . '/' . Registry::get('settings.theme') . '/views/templates/', 'php'); // If there is any template file then go... if (count($_templates) > 0) { @@ -153,7 +153,7 @@ class Themes $blueprints = []; // Get blueprints files - $_blueprints = Filesystem::getFilesList(PATH['themes'] . '/' . Registry::get('system.theme') . '/blueprints/', 'yaml'); + $_blueprints = Filesystem::getFilesList(PATH['themes'] . '/' . Registry::get('settings.theme') . '/blueprints/', 'yaml'); // If there is any template file then go... if (count($_blueprints) > 0) { diff --git a/site/themes/simple/views/partials/head.php b/site/themes/simple/views/partials/head.php index 6bc0ef18..c12e4b39 100755 --- a/site/themes/simple/views/partials/head.php +++ b/site/themes/simple/views/partials/head.php @@ -3,23 +3,23 @@ use Flextype\Component\{Event\Event, Http\Http, Registry\Registry, Assets\Assets, Text\Text, Html\Html}; ?> - + - + - - - + + + - <?php echo Html::toText($page['title']); ?> | <?php echo Html::toText(Registry::get('site.title')); ?> + <?php echo Html::toText($page['title']); ?> | <?php echo Html::toText(Registry::get('settings.title')); ?> - - + + diff --git a/site/themes/simple/views/partials/navigation.php b/site/themes/simple/views/partials/navigation.php index 7a33dbdc..53309223 100755 --- a/site/themes/simple/views/partials/navigation.php +++ b/site/themes/simple/views/partials/navigation.php @@ -4,7 +4,7 @@ ?>