BibciteFormatManager.php in Bibliography & Citation 2.0.x
File
src/Plugin/BibciteFormatManager.php
View source
<?php
namespace Drupal\bibcite\Plugin;
use Drupal\bibcite\Plugin\Factory\FormatFactory;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
class BibciteFormatManager extends DefaultPluginManager implements BibciteFormatManagerInterface {
public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend) {
$this->moduleHandler = $module_handler;
$this
->setCacheBackend($cache_backend, 'bibcite_format', [
'bibcite_format',
]);
}
protected function getDiscovery() {
if (!isset($this->discovery)) {
$this->discovery = new YamlDiscovery('bibcite_format', $this->moduleHandler
->getModuleDirectories());
$this->discovery
->addTranslatableProperty('label', 'label_context');
$this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery);
}
return $this->discovery;
}
protected function getFactory() {
if (!$this->factory) {
$this->factory = new FormatFactory($this, $this->pluginInterface);
}
return $this->factory;
}
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
if (empty($definition['id'])) {
throw new PluginException("Property 'id' is required for plugin '{$plugin_id}' definition.");
}
}
protected function filterDefinitionsBySubclass($subclass) {
$definitions = $this
->getDefinitions();
return array_filter($definitions, function ($definition) use ($subclass) {
return is_subclass_of($definition['encoder'], $subclass);
});
}
public function getExportDefinitions() {
return $this
->filterDefinitionsBySubclass(EncoderInterface::class);
}
public function getImportDefinitions() {
return $this
->filterDefinitionsBySubclass(DecoderInterface::class);
}
}