TamperManager.php in Tamper 8
File
src/TamperManager.php
View source
<?php
namespace Drupal\tamper;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
class TamperManager extends DefaultPluginManager implements TamperManagerInterface {
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/Tamper', $namespaces, $module_handler, 'Drupal\\tamper\\TamperInterface', 'Drupal\\tamper\\Annotation\\Tamper');
$this
->alterInfo('tamper_info');
$this
->setCacheBackend($cache_backend, 'tamper_info_plugins');
}
public function createInstance($plugin_id, array $configuration = []) {
$plugin_definition = $this
->getDefinition($plugin_id);
$plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
if (is_subclass_of($plugin_class, 'Drupal\\Core\\Plugin\\ContainerFactoryPluginInterface')) {
return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
}
return new $plugin_class($configuration, $plugin_id, $plugin_definition, $configuration['source_definition']);
}
public function getCategories() {
$categories = array_unique(array_values(array_map(function ($definition) {
return $definition['category'];
}, $this
->getDefinitions())));
natcasesort($categories);
return $categories;
}
public function getGroupedDefinitions(array $definitions = NULL) {
$definitions = $this
->getSortedDefinitions(isset($definitions) ? $definitions : $this
->getDefinitions());
$grouped_definitions = [];
foreach ($definitions as $id => $definition) {
$grouped_definitions[(string) $definition['category']][$id] = $definition;
}
return $grouped_definitions;
}
public function getSortedDefinitions(array $definitions = NULL) {
$definitions = isset($definitions) ? $definitions : $this
->getDefinitions();
uasort($definitions, function ($a, $b) {
if ($a['category'] != $b['category']) {
return strnatcasecmp($a['category'], $b['category']);
}
return strnatcasecmp($a['label'], $b['label']);
});
return $definitions;
}
}