diff -u b/core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php b/core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php --- b/core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php +++ b/core/lib/Drupal/Core/Plugin/Discovery/HookDiscovery.php @@ -14,12 +14,9 @@ class HookDiscovery implements DiscoveryInterface { protected $hook; - protected $cacheKey; - protected $definitions; - function __construct($hook, $cache_key = NULL) { + function __construct($hook) { $this->hook = $hook; - $this->cacheKey = $cache_key; } /** @@ -34,30 +31,15 @@ * Implements DicoveryInterface::getPluginDefinitions(). */ public function getPluginDefinitions() { - if (!isset($this->definitions)) { - if (isset($this->cacheKey) && $cache = cache()->get($this->cacheKey)) { - $this->definitions = $cache->data; - } - else { - $this->definitions = array(); - - foreach (module_implements($this->hook) as $module) { - $function = $module . '_' . $this->hook; - foreach ($function() as $plugin_id => $definition) { - $definition['module'] = $module; - $this->definitions[$plugin_id] = $definition; - } - } - - drupal_alter($this->hook, $this->definitions); - - if (isset($this->cacheKey)) { - cache()->set($this->cacheKey, $this->definitions); - } + foreach (module_implements($this->hook) as $module) { + $function = $module . '_' . $this->hook; + foreach ($function() as $plugin_id => $definition) { + $definition['module'] = $module; + $definitions[$plugin_id] = $definition; } } - - return $this->definitions; + drupal_alter($this->hook, $definitions); + return $definitions; } } diff -u b/core/modules/image/lib/Drupal/image/Effect.php b/core/modules/image/lib/Drupal/image/Effect.php --- b/core/modules/image/lib/Drupal/image/Effect.php +++ b/core/modules/image/lib/Drupal/image/Effect.php @@ -6,6 +6,7 @@ namespace Drupal\image; use Drupal\Component\Plugin\PluginType; +use Drupal\Core\Plugin\Discovery\CacheDecorator; use Drupal\Core\Plugin\Discovery\HookDiscovery; class Effect extends PluginType { @@ -14,7 +15,7 @@ // Effects include translated strings. $cache_key = 'image_effects:' . drupal_container()->get(LANGUAGE_TYPE_INTERFACE)->langcode; - $this->discovery = new HookDiscovery('image_effect_info', $cache_key); + $this->discovery = new CacheDecorator(new HookDiscovery('image_effect_info'), $cache_key); $this->defaults = array( 'data' => array(), ); only in patch2: unchanged: --- /dev/null +++ b/core/lib/Drupal/Core/Plugin/Discovery/CacheDecorator.php @@ -0,0 +1,85 @@ +decorated = $decorated; + $this->cacheKey = $cache_key; + } + + /** + * Implements DicoveryInterface::getPluginDefinition(). + */ + public function getPluginDefinition($plugin_id) { + $definitions = $this->getCachedDefinitions(); + if (isset($definitions)) { + $definition = isset($definitions[$plugin_id]) ? $definitions[$plugin_id] : NULL; + } + else { + $definition = $this->decorated->getPluginDefinition($plugin_id); + } + return $definition; + } + + /** + * Implements DicoveryInterface::getPluginDefinitions(). + */ + public function getPluginDefinitions() { + $definitions = $this->getCachedDefinitions(); + if (!isset($definitions)) { + $definitions = $this->decorated->getPluginDefinitions(); + $this->setCachedDefinitions($definitions); + } + return $definitions; + } + + /** + * + */ + protected function getCachedDefinitions() { + if (!isset($this->definitions) && isset($this->cacheKey) && $cache = cache()->get($this->cacheKey)) { + $this->definitions = $cache->data; + } + return $this->definitions; + } + + /** + * + */ + protected function setCachedDefinitions($definitions) { + if (isset($this->cacheKey)) { + cache()->set($this->cacheKey, $definitions); + } + $this->definitions = $definitions; + } + + /** + * Pass through all unknown calls onto the decorated object. + */ + public function __call($method, $args) { + return call_user_func_array(array($this->decorated, $method), $args); + } +}