From 7947ba24422e74a4831151828e5f768267fda130 Mon Sep 17 00:00:00 2001 From: Gert Date: Thu, 16 Apr 2015 17:42:29 +0200 Subject: [PATCH 1/3] fix config reload to actually reload from files --- system/src/Grav/Common/Config/Config.php | 100 ++++++++---------- .../Common/Service/ConfigServiceProvider.php | 12 +-- 2 files changed, 48 insertions(+), 64 deletions(-) diff --git a/system/src/Grav/Common/Config/Config.php b/system/src/Grav/Common/Config/Config.php index 1fe373a40..ef08830a0 100644 --- a/system/src/Grav/Common/Config/Config.php +++ b/system/src/Grav/Common/Config/Config.php @@ -75,6 +75,8 @@ class Config extends Data ] ]; + protected $setup = []; + protected $blueprintFiles = []; protected $configFiles = []; protected $checksum; @@ -88,33 +90,25 @@ class Config extends Data protected $environment; protected $messages = []; - public function __construct(array $items = array(), Grav $grav = null, $environment = null) + public function __construct(array $setup = array(), Grav $grav = null, $environment = null) { $this->grav = $grav ?: Grav::instance(); $this->finder = new ConfigFinder; $this->environment = $environment ?: 'localhost'; $this->messages[] = 'Environment Name: ' . $this->environment; - if (isset($items['@class'])) { - if ($items['@class'] != get_class($this)) { - throw new \InvalidArgumentException('Unrecognized config cache file!'); - } - // Loading pre-compiled configuration. - $this->timestamp = (int) $items['timestamp']; - $this->checksum = $items['checksum']; - $this->items = (array) $items['data']; - } else { - // Make sure that - if (!isset($items['streams']['schemes'])) { - $items['streams']['schemes'] = []; - } - $items['streams']['schemes'] += $this->streams; - - $items = $this->autoDetectEnvironmentConfig($items); - $this->messages[] = $items['streams']['schemes']['config']['prefixes']['']; - - parent::__construct($items); + // Make sure that + if (!isset($setup['streams']['schemes'])) { + $setup['streams']['schemes'] = []; } + $setup['streams']['schemes'] += $this->streams; + + $setup = $this->autoDetectEnvironmentConfig($setup); + $this->messages[] = $setup['streams']['schemes']['config']['prefixes']['']; + + $this->setup = $setup; + parent::__construct($setup); + $this->check(); } @@ -125,8 +119,10 @@ class Config extends Data public function reload() { + $this->items = $this->setup; $this->check(); $this->init(); + $this->debug(); return $this; } @@ -150,6 +146,7 @@ class Config extends Data foreach ($this->messages as $message) { $this->grav['debugger']->addMessage($message); } + $this->messages = []; } public function init() @@ -161,15 +158,6 @@ class Config extends Data $this->blueprintLookup = $locator->findResources('blueprints://config'); $this->pluginLookup = $locator->findResources('plugins://'); - if (!isset($this->checksum)) { - $this->messages[] = 'No cached configuration, compiling new configuration..'; - } elseif ($this->checksum() != $this->checksum) { - $this->messages[] = 'Configuration checksum mismatch, reloading configuration..'; - } else { - $this->messages[] = 'Configuration checksum matches, using cached version.'; - return; - } - $this->loadCompiledBlueprints($this->blueprintLookup, $this->pluginLookup, 'master'); $this->loadCompiledConfig($this->configLookup, $this->pluginLookup, 'master'); @@ -255,7 +243,6 @@ class Config extends Data 'files' => $blueprintFiles, 'data' => $this->blueprints->toArray() ]; - // If compiled file wasn't already locked by another process, save it. if ($file->locked() !== false) { $this->messages[] = 'Saving compiled blueprints.'; @@ -269,44 +256,51 @@ class Config extends Data protected function loadCompiledConfig($configs, $plugins, $filename = null) { - $checksum = md5(json_encode($configs)); $filename = $filename ? CACHE_DIR . 'compiled/config/' . $filename . '-' . $this->environment . '.php' : CACHE_DIR . 'compiled/config/' . $checksum . '-' . $this->environment . '.php'; $file = PhpFile::instance($filename); $cache = $file->exists() ? $file->content() : null; - $configFiles = $this->finder->locateConfigFiles($configs, $plugins); - $checksum .= ':'.md5(json_encode($configFiles)); $class = get_class($this); + $checksum = $this->checksum(); - // Load real file if cache isn't up to date (or is invalid). if ( !is_array($cache) || !isset($cache['checksum']) || !isset($cache['@class']) - || $cache['checksum'] != $checksum || $cache['@class'] != $class ) { - // Attempt to lock the file for writing. - $file->lock(false); + $this->messages[] = 'No cached configuration, compiling new configuration..'; + } else if ($cache['checksum'] !== $checksum) { + $this->messages[] = 'Configuration checksum mismatch, reloading configuration..'; + } else { + $this->messages[] = 'Configuration checksum matches, using cached version.'; - // Load configuration. - foreach ($configFiles as $files) { - $this->loadConfigFiles($files); - } - $cache = [ - '@class' => $class, - 'timestamp' => time(), - 'checksum' => $this->checksum(), - 'data' => $this->toArray() - ]; + $this->items = $cache['data']; + return; + } - // If compiled file wasn't already locked by another process, save it. - if ($file->locked() !== false) { - $this->messages[] = 'Saving compiled configuration.'; - $file->save($cache); - $file->unlock(); - } + $configFiles = $this->finder->locateConfigFiles($configs, $plugins); + + // Attempt to lock the file for writing. + $file->lock(false); + + // Load configuration. + foreach ($configFiles as $files) { + $this->loadConfigFiles($files); + } + $cache = [ + '@class' => $class, + 'timestamp' => time(), + 'checksum' => $checksum, + 'data' => $this->toArray() + ]; + + // If compiled file wasn't already locked by another process, save it. + if ($file->locked() !== false) { + $this->messages[] = 'Saving compiled configuration.'; + $file->save($cache); + $file->unlock(); } $this->items = $cache['data']; diff --git a/system/src/Grav/Common/Service/ConfigServiceProvider.php b/system/src/Grav/Common/Service/ConfigServiceProvider.php index c66732b08..28a6ac4d9 100644 --- a/system/src/Grav/Common/Service/ConfigServiceProvider.php +++ b/system/src/Grav/Common/Service/ConfigServiceProvider.php @@ -38,18 +38,8 @@ class ConfigServiceProvider implements ServiceProviderInterface public function loadMasterConfig(Container $container) { $environment = $this->getEnvironment($container); - $file = CACHE_DIR . 'compiled/config/master-'.$environment.'.php'; - $data = is_file($file) ? (array) include $file : []; - if ($data) { - try { - $config = new Config($data, $container, $environment); - } catch (\Exception $e) { - } - } - if (!isset($config)) { - $config = new Config($this->setup, $container, $environment); - } + $config = new Config($this->setup, $container, $environment); return $config; } From f3b4efb66155e8df3a2ca8b5b36b240436e7c094 Mon Sep 17 00:00:00 2001 From: Gert Date: Thu, 16 Apr 2015 17:43:03 +0200 Subject: [PATCH 2/3] Make plugins and themes load the merged config from all files --- system/src/Grav/Common/Plugins.php | 4 ++-- system/src/Grav/Common/Themes.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/system/src/Grav/Common/Plugins.php b/system/src/Grav/Common/Plugins.php index d0c30c3e3..9d8167df9 100644 --- a/system/src/Grav/Common/Plugins.php +++ b/system/src/Grav/Common/Plugins.php @@ -123,10 +123,10 @@ class Plugins extends Iterator $obj = new Data($file->content(), $blueprint); // Override with user configuration. - $file = CompiledYamlFile::instance("user://config/plugins/{$name}.yaml"); - $obj->merge($file->content()); + $obj->merge($this->grav['config']->get('plugins.' . $name) ?: []); // Save configuration always to user/config. + $file = CompiledYamlFile::instance("config://plugins/{$name}.yaml"); $obj->file($file); return $obj; diff --git a/system/src/Grav/Common/Themes.php b/system/src/Grav/Common/Themes.php index 4ec394ee9..ab4696a88 100644 --- a/system/src/Grav/Common/Themes.php +++ b/system/src/Grav/Common/Themes.php @@ -106,10 +106,10 @@ class Themes extends Iterator $obj = new Data($file->content(), $blueprint); // Override with user configuration. - $file = CompiledYamlFile::instance("user://config/themes/{$name}" . YAML_EXT); - $obj->merge($file->content()); + $obj->merge($this->grav['config']->get('themes.' . $name) ?: []); // Save configuration always to user/config. + $file = CompiledYamlFile::instance("config://themes/{$name}" . YAML_EXT); $obj->file($file); return $obj; From 7e94e464595c53684ebc406a61d823273f81dd7e Mon Sep 17 00:00:00 2001 From: Gert Date: Thu, 16 Apr 2015 17:54:48 +0200 Subject: [PATCH 3/3] use gravtrait in plugins --- system/src/Grav/Common/Grav.php | 2 +- system/src/Grav/Common/Plugins.php | 21 +++++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index df584fe4b..2068f3454 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -78,7 +78,7 @@ class Grav extends Container return new Cache($c); }; $container['plugins'] = function ($c) { - return new Plugins($c); + return new Plugins(); }; $container['themes'] = function ($c) { return new Themes($c); diff --git a/system/src/Grav/Common/Plugins.php b/system/src/Grav/Common/Plugins.php index 9d8167df9..b6264e9d9 100644 --- a/system/src/Grav/Common/Plugins.php +++ b/system/src/Grav/Common/Plugins.php @@ -4,6 +4,7 @@ namespace Grav\Common; use Grav\Common\Config\Config; use Grav\Common\Data\Blueprints; use Grav\Common\Data\Data; +use Grav\Common\GravTrait; use Grav\Common\File\CompiledYamlFile; use RocketTheme\Toolbox\Event\EventDispatcher; use RocketTheme\Toolbox\Event\EventSubscriberInterface; @@ -17,12 +18,7 @@ use RocketTheme\Toolbox\Event\EventSubscriberInterface; */ class Plugins extends Iterator { - protected $grav; - - public function __construct(Grav $grav) - { - $this->grav = $grav; - } + use GravTrait; /** * Recurses through the plugins directory creating Plugin objects for each plugin it finds. @@ -33,11 +29,11 @@ class Plugins extends Iterator public function init() { /** @var Config $config */ - $config = $this->grav['config']; + $config = self::getGrav()['config']; $plugins = (array) $config->get('plugins'); /** @var EventDispatcher $events */ - $events = $this->grav['events']; + $events = self::getGrav()['events']; foreach ($plugins as $plugin => $data) { if (empty($data['enabled'])) { @@ -45,9 +41,10 @@ class Plugins extends Iterator continue; } - $filePath = $this->grav['locator']('plugins://' . $plugin . DS . $plugin . PLUGIN_EXT); + $locator = self::getGrav()['locator']; + $filePath = $locator('plugins://' . $plugin . DS . $plugin . PLUGIN_EXT); if (!is_file($filePath)) { - $this->grav['log']->addWarning(sprintf("Plugin '%s' enabled but not found! Try clearing cache with `bin/grav clear-cache`", $plugin)); + self::getGrav()['log']->addWarning(sprintf("Plugin '%s' enabled but not found! Try clearing cache with `bin/grav clear-cache`", $plugin)); continue; } @@ -70,7 +67,7 @@ class Plugins extends Iterator throw new \RuntimeException(sprintf("Plugin '%s' class not found! Try reinstalling this plugin.", $plugin)); } - $instance = new $pluginClassName($plugin, $this->grav, $config); + $instance = new $pluginClassName($plugin, self::getGrav(), $config); if ($instance instanceof EventSubscriberInterface) { $events->addSubscriber($instance); } @@ -123,7 +120,7 @@ class Plugins extends Iterator $obj = new Data($file->content(), $blueprint); // Override with user configuration. - $obj->merge($this->grav['config']->get('plugins.' . $name) ?: []); + $obj->merge(self::getGrav()['config']->get('plugins.' . $name) ?: []); // Save configuration always to user/config. $file = CompiledYamlFile::instance("config://plugins/{$name}.yaml");