transmuter-1.0.0-alpha2/src/Plugin/TransmuterManager.php
src/Plugin/TransmuterManager.php
<?php
namespace Drupal\transmuter\Plugin;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\themespace\Plugin\Discovery\ProviderTypedAttributeClassDiscovery;
use Drupal\themespace\Plugin\Discovery\ProviderTypedDeriverDiscoveryDecorator;
use Drupal\themespace\Plugin\ProviderTypedPluginManagerTrait;
/**
* Plugin manager for theme element transmuter plugins.
*/
class TransmuterManager extends DefaultPluginManager implements TransmuterManagerInterface {
use ProviderTypedPluginManagerTrait {
findDefinitions as findTypedDefinitions;
}
/**
* A single or list of subdirectorie within a namespace to look for plugins.
*
* Set to FALSE if the plugins are in the top level of the namespace.
*
* @var string[]|string|bool
*/
protected array|string|bool $subdirs;
/**
* Create a new instance of the TransmuterManager class.
*
* @param \Traversable $namespaces
* The container namespaces to search for preprocess plugins.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend for plugin definition storage.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
parent::__construct(
'Plugin/Transmuter',
$namespaces,
$module_handler,
'Drupal\transmuter\Plugin\TransmuterInterface',
'Drupal\transmuter\Attribute\Transmuter'
);
$this->subdirs = ['Plugin/Transmuter', 'Preprocess'];
$this->themeHandler = $theme_handler;
$this->alterInfo('transmuter_info');
$this->setCacheBackend($cache_backend, 'transmuter');
}
/**
* {@inheritdoc}
*/
public static function comparePluginPriority(TransmuterDefinition $a, TransmuterDefinition $b): int {
return $a->getPriority() - $b->getPriority();
}
/**
* {@inheritdoc}
*/
public function clearCachedDefinitions(): void {
parent::clearCachedDefinitions();
// If definition caches have been cleared, also remove our currently
// grouped plugin definitions.
unset($this->moduleDefinitions);
unset($this->themeDefinitions);
}
/**
* {@inheritdoc}
*/
protected function getDiscovery(): DiscoveryInterface {
if (!$this->discovery) {
$discovery = new ProviderTypedAttributeClassDiscovery(
$this->subdirs,
$this->namespaces,
$this->pluginDefinitionAttributeName
);
$this->discovery = new ProviderTypedDeriverDiscoveryDecorator($discovery);
}
return $this->discovery;
}
/**
* {@inheritdoc}
*/
protected function findDefinitions(): array {
/** @var \Drupal\transmuter\Plugin\TransmuterDefinition[] $definitions */
$definitions = $this->findTypedDefinitions();
// Sort the plugins by plugin definition priority value.
uasort($definitions, static::comparePluginPriority(...));
return $definitions;
}
}
