Issue #2039521 by Berdir, tim.plunkett: Convert FilterPluginManager to extend DefaultPluginManager.

8.0.x
Alex Pott 2013-08-23 02:53:08 +01:00
parent e21eae47ea
commit 6cb659a97e
4 changed files with 68 additions and 17 deletions

View File

@ -52,6 +52,13 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt
*/
protected $cacheKey;
/**
* An array of cache tags to use for the cached definitions.
*
* @var array
*/
protected $cacheTags = array();
/**
* Name of the alter hook if one should be invoked.
*
@ -116,12 +123,16 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt
* @param string $cache_key_prefix
* Cache key prefix to use, the language code will be appended
* automatically.
* @param array $cache_tags
* (optional) When providing a list of cache tags, the cached definitions
* are tagged and are used to clear the cache.
*/
public function setCacheBackend(CacheBackendInterface $cache_backend, LanguageManager $language_manager, $cache_key_prefix) {
public function setCacheBackend(CacheBackendInterface $cache_backend, LanguageManager $language_manager, $cache_key_prefix, array $cache_tags = array()) {
$this->languageManager = $language_manager;
$this->cacheBackend = $cache_backend;
$this->cacheKeyPrefix = $cache_key_prefix;
$this->cacheKey = $cache_key_prefix . ':' . $language_manager->getLanguage(Language::TYPE_INTERFACE)->id;
$this->cacheTags = $cache_tags;
}
/**
@ -169,12 +180,18 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt
*/
public function clearCachedDefinitions() {
if ($this->cacheBackend) {
$cache_keys = array();
// @todo: Use $this->languageManager->languageList() after http://drupal.org/node/1862202 is in.
foreach (language_list() as $langcode => $language) {
$cache_keys[] = $this->cacheKeyPrefix . ':' .$langcode;
if ($this->cacheTags) {
// Use the cache tags to clear the cache.
$this->cacheBackend->deleteTags($this->cacheTags);
}
else {
$cache_keys = array();
// @todo: Use $this->languageManager->languageList() after http://drupal.org/node/1862202 is in.
foreach (language_list() as $langcode => $language) {
$cache_keys[] = $this->cacheKeyPrefix . ':' .$langcode;
}
$this->cacheBackend->deleteMultiple($cache_keys);
}
$this->cacheBackend->deleteMultiple($cache_keys);
}
$this->definitions = NULL;
}
@ -203,7 +220,7 @@ class DefaultPluginManager extends PluginManagerBase implements PluginManagerInt
*/
protected function setCachedDefinitions($definitions) {
if ($this->cacheBackend) {
$this->cacheBackend->set($this->cacheKey, $definitions);
$this->cacheBackend->set($this->cacheKey, $definitions, CacheBackendInterface::CACHE_PERMANENT, $this->cacheTags);
}
$this->definitions = $definitions;
}

View File

@ -16,4 +16,4 @@ services:
- { name: access_check }
plugin.manager.filter:
class: Drupal\filter\FilterPluginManager
arguments: ['@container.namespaces']
arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@module_handler']

View File

@ -9,7 +9,10 @@ namespace Drupal\filter;
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
@ -20,7 +23,7 @@ use Drupal\Core\Plugin\Factory\ContainerFactory;
*
* @see hook_filter_info_alter()
*/
class FilterPluginManager extends PluginManagerBase {
class FilterPluginManager extends DefaultPluginManager {
/**
* Constructs a FilterPluginManager object.
@ -28,16 +31,18 @@ class FilterPluginManager extends PluginManagerBase {
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Language\LanguageManager $language_manager
* The language manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
*/
public function __construct(\Traversable $namespaces) {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, LanguageManager $language_manager, ModuleHandlerInterface $module_handler) {
$annotation_namespaces = array('Drupal\filter\Annotation' => $namespaces['Drupal\filter']);
$this->discovery = new AnnotatedClassDiscovery('Plugin/Filter', $namespaces, $annotation_namespaces, 'Drupal\filter\Annotation\Filter');
$this->discovery = new AlterDecorator($this->discovery, 'filter_info');
$cache_key = 'filter_plugins:' . language(Language::TYPE_INTERFACE)->id;
$cache_tags = array('filter_formats' => TRUE);
$this->discovery = new CacheDecorator($this->discovery, $cache_key, 'cache', CacheBackendInterface::CACHE_PERMANENT, $cache_tags);
$this->factory = new ContainerFactory($this);
parent::__construct('Plugin/Filter', $namespaces, $annotation_namespaces, 'Drupal\filter\Annotation\Filter');
$this->alterInfo($module_handler, 'filter_info');
$this->setCacheBackend($cache_backend, $language_manager, 'filter_plugins', array('filter_formats' => TRUE));
}
/**

View File

@ -156,4 +156,33 @@ class DefaultPluginManagerTest extends UnitTestCase {
$this->assertEquals($this->expectedDefinitions, $plugin_manager->getDefinitions());
}
/**
* Tests the plugin manager cache clear with tags.
*/
public function testCacheClearWithTags() {
$cid = $this->randomName();
$cache_backend = $this->getMockBuilder('Drupal\Core\Cache\MemoryBackend')
->disableOriginalConstructor()
->getMock();
$cache_backend
->expects($this->once())
->method('deleteTags')
->with(array('tag' => TRUE));
$cache_backend
->expects($this->never())
->method('deleteMultiple');
$language = new Language(array('id' => 'en'));
$language_manager = $this->getMock('Drupal\Core\Language\LanguageManager');
$language_manager->expects($this->once())
->method('getLanguage')
->with(Language::TYPE_INTERFACE)
->will($this->returnValue($language));
$plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions);
$plugin_manager->setCacheBackend($cache_backend, $language_manager, $cid, array('tag' => TRUE));
$plugin_manager->clearCachedDefinitions();
}
}