TagPluginManager.php in Extensible BBCode 8.3
File
src/TagPluginManager.php
View source
<?php
namespace Drupal\xbbcode;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\FallbackPluginManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\xbbcode\Annotation\XBBCodeTag;
use Drupal\xbbcode\Plugin\TagPluginInterface;
use Traversable;
class TagPluginManager extends DefaultPluginManager implements FallbackPluginManagerInterface {
protected $ids;
protected $defaultCollection;
protected $defaultConfiguration;
protected $defaultNameCollisions;
public function __construct(Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/XBBCode', $namespaces, $module_handler, TagPluginInterface::class, XBBCodeTag::class);
$this
->alterInfo('xbbcode_info');
$this
->setCacheBackend($cache_backend, 'xbbcode_tag_plugins');
}
public function getFallbackPluginId($plugin_id, array $configuration = []) : string {
return 'null';
}
public function getDefinedIds() : array {
if (!$this->ids) {
$ids = array_keys($this
->getDefinitions());
$this->ids = array_combine($ids, $ids);
unset($this->ids['null']);
}
return $this->ids;
}
public function clearCachedDefinitions() : void {
parent::clearCachedDefinitions();
$this->ids = NULL;
$this->defaultConfiguration = NULL;
$this->defaultNameCollisions = NULL;
if ($this->defaultCollection) {
$this->defaultCollection
->setConfiguration($this
->getDefaultConfiguration());
}
}
public function getDefaultCollection() : TagPluginCollection {
if (!$this->defaultCollection) {
$configurations = $this
->getDefaultConfiguration();
$this->defaultCollection = new TagPluginCollection($this, $configurations);
}
return $this->defaultCollection;
}
protected function getDefaultConfiguration() : array {
if ($this->defaultConfiguration === NULL || $this->defaultNameCollisions === NULL) {
$this->defaultConfiguration = [];
$this->defaultNameCollisions = [];
foreach ($this
->getDefinedIds() as $plugin_id) {
try {
$plugin = $this
->createInstance($plugin_id);
$name = $plugin
->getName();
$this->defaultConfiguration[$name]['id'] = $plugin_id;
if (!isset($this->defaultNameCollisions[$name])) {
$this->defaultNameCollisions[$name] = [];
}
$this->defaultNameCollisions[$name][] = $plugin_id;
} catch (PluginException $exception) {
watchdog_exception('xbbcode', $exception);
}
}
foreach ($this->defaultNameCollisions as $name => $ids) {
if (count($ids) < 2) {
unset($this->defaultNameCollisions[$name]);
}
}
}
return $this->defaultConfiguration;
}
public function getDefaultNameCollisions() : array {
if ($this->defaultNameCollisions === NULL) {
$this
->getDefaultConfiguration();
}
return $this->defaultNameCollisions;
}
}