View source
<?php
namespace Drupal\service_container\Plugin;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\service_container\DependencyInjection\ContainerAware;
class ContainerAwarePluginManager extends ContainerAware implements PluginManagerInterface {
public function __construct($service_prefix) {
$this->servicePrefix = $service_prefix;
}
public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
return $this->container
->getDefinition($this->servicePrefix . $plugin_id, $exception_on_invalid);
}
public function getDefinitions() {
$definitions = $this->container
->getDefinitions();
$prefix = $this->servicePrefix;
$keys = array_filter(array_keys($definitions), function ($key) use ($prefix) {
return strpos($key, $prefix) === 0;
});
return array_intersect_key($definitions, array_flip($keys));
}
public function hasDefinition($plugin_id) {
return $this->container
->hasDefinition($this->servicePrefix . $plugin_id);
}
public function createInstance($plugin_id, array $configuration = array()) {
$plugin_definition_copy = $plugin_definition = $this
->getDefinition($plugin_id);
$plugin_class = static::getPluginClass($plugin_id, $plugin_definition);
if (is_subclass_of($plugin_class, 'Drupal\\Core\\Plugin\\ContainerFactoryPluginInterface')) {
return $plugin_class::create($this->container, $configuration, $plugin_id, $plugin_definition);
}
$plugin_definition += array(
'arguments' => array(),
);
array_unshift($plugin_definition['arguments'], $plugin_definition_copy);
array_unshift($plugin_definition['arguments'], $plugin_id);
array_unshift($plugin_definition['arguments'], $configuration);
return $this->container
->createInstance($this->servicePrefix . $plugin_id, $plugin_definition);
}
public function getInstance(array $options) {
if (isset($options['id'])) {
return $this
->createInstance($options['id']);
}
return FALSE;
}
public static function getPluginClass($plugin_id, $plugin_definition = NULL, $required_interface = NULL) {
if (empty($plugin_definition['class'])) {
throw new PluginException(sprintf('The plugin (%s) did not specify an instance class.', $plugin_id));
}
$class = $plugin_definition['class'];
if (!class_exists($class)) {
throw new PluginException(sprintf('Plugin (%s) instance class "%s" does not exist.', $plugin_id, $class));
}
if ($required_interface && !is_subclass_of($plugin_definition['class'], $required_interface)) {
throw new PluginException(sprintf('Plugin "%s" (%s) in %s should implement interface %s.', $plugin_id, $plugin_definition['class'], $plugin_definition['provider'], $required_interface));
}
return $class;
}
}