devel_wizard-2.x-dev/templates/spell/plugin_manager/manager.php.twig
templates/spell/plugin_manager/manager.php.twig
<?php
declare(strict_types=1);
namespace {{ manager.namespace }};
use Drupal\Component\Annotation\Plugin\Discovery\AnnotationBridgeDecorator;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use {{ annotation.classFqn }};
use Sweetchuck\Utils\Comparer\ComparerInterface;
class {{ manager.class }} extends DefaultPluginManager implements {{ managerInterface.class }} {
protected ComparerInterface $definitionComparer;
public function __construct(
\Traversable $namespaces,
ModuleHandlerInterface $module_handler,
CacheBackendInterface $discovery_cache,
ComparerInterface $definitionComparer,
array $additional_annotation_namespaces = [],
) {
$this->definitionComparer = $definitionComparer;
$this->useCaches = TRUE;
$this->setCacheBackend($discovery_cache, '{{ module }}.{{ object }}.plugin_definitions');
parent::__construct(
'Plugin/{{ moduleUpperCamel }}/{{ objectUpperCamel }}',
$namespaces,
$module_handler,
{{ pluginInterface.class }}::class,
{{ annotation.class }}::class,
$additional_annotation_namespaces,
);
}
/**
* {@inheritdoc}
*/
protected function getDiscovery() {
if (!$this->discovery) {
$discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName, $this->additionalAnnotationNamespaces);
$discovery = new AnnotationBridgeDecorator($discovery, $this->pluginDefinitionAnnotationName);
$discovery = new ContainerDerivativeDiscoveryDecorator($discovery);
$this->discovery = $discovery;
}
return $this->discovery;
}
/**
* {@inheritdoc}
*/
protected function findDefinitions() {
$definitions = parent::findDefinitions();
assert($this->assertDefinitions($definitions));
uasort($definitions, $this->definitionComparer);
return $definitions;
}
/**
* @param array<string, \{{ definition.classFqn }}> $definitions
*/
protected function assertDefinitions(array $definitions): bool {
foreach ($definitions as $definition) {
$this->assertDefinition($definition);
}
return TRUE;
}
protected function assertDefinition({{ definition.class }} $definition): void {
$id = $definition->id();
$prefix_variants = $this->getPrefixVariants($definition->getProvider());
$is_prefix_valid = FALSE;
foreach ($prefix_variants as $prefix_variant) {
if (str_starts_with($id, $prefix_variant)) {
$is_prefix_valid = TRUE;
break;
}
}
assert(
$is_prefix_valid,
"{{ module }}.{{ object }} plugin identifier '$id' should be prefixed with the provider: {$prefix_variants[0]}",
);
}
/**
* @return array<string>
*/
protected function getPrefixVariants(string $full): array {
$variants = [];
$separator = '_';
$parts = explode($separator, $full);
for ($length = count($parts); $length > 0; $length--) {
$variants[] = implode(
$separator,
array_slice($parts, 0, $length),
);
}
return $variants;
}
/**
* {@inheritdoc}
*/
public function getGroupedDefinitions(array $definitions = NULL, $label_key = 'label') {
$definitions = $this->getSortedDefinitions(
$definitions ?? $this->getDefinitions(),
);
$grouped_definitions = [];
foreach ($definitions as $id => $definition) {
$grouped_definitions[(string) $definition->getCategory()][$id] = $definition;
}
return $grouped_definitions;
}
/**
* {@inheritdoc}
*/
public function getCategories() {
$categories = array_unique(array_values(array_map(
function ({{ definition.class }} $definition) {
return $definition->getCategory();
},
$this->getDefinitions(),
)));
natcasesort($categories);
return $categories;
}
/**
* {@inheritdoc}
*/
public function getSortedDefinitions(array $definitions = NULL) {
$definitions = $definitions ?? $this->getDefinitions();
uasort($definitions, $this->definitionComparer);
return $definitions;
}
}
