PluginTypeManager.php in Plugin 8.2
File
src/PluginType/PluginTypeManager.php
View source
<?php
namespace Drupal\plugin\PluginType;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PluginTypeManager implements PluginTypeManagerInterface {
protected $container;
protected $moduleHandler;
protected $pluginTypes;
public function __construct(ContainerInterface $container, ModuleHandlerInterface $module_handler) {
$this->container = $container;
$this->moduleHandler = $module_handler;
}
public function hasPluginType($id) {
return isset($this
->getPluginTypes()[$id]);
}
public function getPluginType($id) {
$plugin_types = $this
->getPluginTypes();
if (isset($plugin_types[$id])) {
return $plugin_types[$id];
}
else {
throw new \InvalidArgumentException(sprintf('Plugin type "%s" is unknown.', $id));
}
}
public function getPluginTypes() {
if (is_array($this->pluginTypes)) {
return $this->pluginTypes;
}
$this->pluginTypes = [];
$providers = array_map(function (Extension $module) {
return $module
->getName();
}, $this->moduleHandler
->getModuleList());
$providers[] = 'core';
$files = $this
->findFiles();
$providers_by_file = array_flip($files);
$file_cache = FileCacheFactory::get('plugin:plugin_type:' . hash('sha256', serialize($providers)));
foreach ($file_cache
->getMultiple($files) as $file => $plugin_types_by_file) {
$this->pluginTypes = array_merge($this->pluginTypes, $plugin_types_by_file);
unset($providers_by_file[$file]);
}
foreach ($providers_by_file as $file => $provider) {
$plugin_type_definitions = Yaml::decode(file_get_contents($file)) ?: [];
$plugin_type_definition_defaults = [
'class' => PluginType::class,
'provider' => $provider,
];
$plugin_type_definitions = array_map(function ($plugin_type_id, array $plugin_type_definition) use ($plugin_type_definition_defaults) {
$plugin_type_definition['id'] = $plugin_type_id;
return $plugin_type_definition + $plugin_type_definition_defaults;
}, array_keys($plugin_type_definitions), $plugin_type_definitions);
$plugin_type_definitions = array_filter($plugin_type_definitions, function (array $plugin_type_definition) use ($providers) {
return in_array($plugin_type_definition['provider'], $providers);
});
$file_plugin_types = [];
foreach ($plugin_type_definitions as $plugin_type_definition) {
$class = $plugin_type_definition['class'];
$plugin_type = $class::createFromDefinition($this->container, $plugin_type_definition);
$file_plugin_types[$plugin_type
->getId()] = $plugin_type;
}
$this->pluginTypes += $file_plugin_types;
$file_cache
->set($file, $file_plugin_types);
}
return $this->pluginTypes;
}
protected function findFiles() {
$files = [];
foreach ($this->moduleHandler
->getModuleDirectories() as $provider => $directory) {
$file = $directory . '/' . $provider . '.plugin_type.yml';
if (file_exists($file)) {
$files[$provider] = $file;
}
}
return $files;
}
}