From 03521cd3f6bb56d068a1c25fa23f5297e89b1a2e Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 29 Jan 2015 17:22:55 -0700 Subject: [PATCH] Some performance optimizations --- system/src/Grav/Common/Page/Page.php | 11 +++++ system/src/Grav/Common/Plugin.php | 62 +++++++++++++--------------- system/src/Grav/Common/Plugins.php | 2 +- system/src/Grav/Common/Theme.php | 2 +- 4 files changed, 41 insertions(+), 36 deletions(-) diff --git a/system/src/Grav/Common/Page/Page.php b/system/src/Grav/Common/Page/Page.php index 48b86cacc..79dd9fef9 100644 --- a/system/src/Grav/Common/Page/Page.php +++ b/system/src/Grav/Common/Page/Page.php @@ -280,6 +280,17 @@ class Page return $this->header; } + /** + * Modify a header value directly + * + * @param $key + * @param $value + */ + public function modifyHeader($key, $value) + { + $this->header->$key = $value; + } + /** * Get the summary. * diff --git a/system/src/Grav/Common/Plugin.php b/system/src/Grav/Common/Plugin.php index f4fb3230d..b264f722b 100644 --- a/system/src/Grav/Common/Plugin.php +++ b/system/src/Grav/Common/Plugin.php @@ -27,6 +27,10 @@ class Plugin implements EventSubscriberInterface protected $config; protected $active = true; + /** + * @var \Grav\Common\string + */ + protected $name; /** * By default assign all methods as listeners using the default priority. @@ -50,13 +54,15 @@ class Plugin implements EventSubscriberInterface /** * Constructor. * - * @param Grav $grav - * @param Config $config + * @param string $name + * @param Grav $grav + * @param Config $config */ - public function __construct(Grav $grav, Config $config) + public function __construct($name, Grav $grav, Config $config) { $this->grav = $grav; $this->config = $config; + $this->name = $name; } public function isAdmin() @@ -112,47 +118,35 @@ class Plugin implements EventSubscriberInterface /** * Merge global and page configurations. * - * @param Page $page The page to merge the configurations with the + * @param Page $page The page to merge the configurations with the * plugin settings. + * + * @return \Grav\Common\Data\Data */ protected function mergeConfig(Page $page) { - static $className; + $class_name = $this->name; + $class_name_merged = $class_name . '.merged'; + $defaults = $this->config->get('plugins.' . $class_name, array()); + $header = array(); - if ( is_null($className) ) { - // Load configuration based on class name - $reflector = new \ReflectionClass($this); - - // Remove namespace and trailing "Plugin" word - $name = $reflector->getShortName(); - $name = substr($name, 0, -strlen('Plugin')); - - // Guess configuration path from class name - $class_formats = array( - strtolower($name), # all lowercased - Inflector::underscorize($name), # underscored - ); - - $defaults = array(); - // Try to load configuration - foreach ( $class_formats as $name ) { - if ( !is_null($this->config->get('plugins.' . $name, NULL)) ) { - $className = $name; - break; - } + if (isset($page->header()->$class_name_merged)) { + $merged = $page->header()->$class_name_merged; + if (count($merged) > 0) { + return $merged; + } else { + return new Data($defaults); } } // Get default plugin configurations and retrieve page header configuration - $plugin = (array) $this->config->get('plugins.' . $className, array()); - $header = (array) $page->header(); + if (isset($page->header()->$class_name)) { + $header = array_merge($defaults, $page->header()->$class_name); + } - // Create new config data class - $config = new Data(); - - // Join configuration options - $config->setDefaults($header); - $config->joinDefaults($className, $plugin); + // Create new config object and set it on the page object so it's cached for next time + $config = new Data($header); + $page->modifyHeader($class_name_merged, $config); // Return configurations as a new data config class return $config; diff --git a/system/src/Grav/Common/Plugins.php b/system/src/Grav/Common/Plugins.php index 4caa71b16..7397c8bb1 100644 --- a/system/src/Grav/Common/Plugins.php +++ b/system/src/Grav/Common/Plugins.php @@ -69,7 +69,7 @@ class Plugins extends Iterator throw new \RuntimeException(sprintf("Plugin '%s' class not found! Try reinstalling this plugin.", $plugin)); } - $instance = new $pluginClassName($this->grav, $config); + $instance = new $pluginClassName($plugin, $this->grav, $config); if ($instance instanceof EventSubscriberInterface) { $events->addSubscriber($instance); } diff --git a/system/src/Grav/Common/Theme.php b/system/src/Grav/Common/Theme.php index 3ec5fb8af..12195127e 100644 --- a/system/src/Grav/Common/Theme.php +++ b/system/src/Grav/Common/Theme.php @@ -18,6 +18,6 @@ class Theme extends Plugin { $this->name = $name; - parent::__construct($grav, $config); + parent::__construct($name, $grav, $config); } }