ReflectionFactory.php in Drupal 8
File
core/lib/Drupal/Component/Plugin/Factory/ReflectionFactory.php
View source
<?php
namespace Drupal\Component\Plugin\Factory;
class ReflectionFactory extends DefaultFactory {
public function createInstance($plugin_id, array $configuration = []) {
$plugin_definition = $this->discovery
->getDefinition($plugin_id);
$plugin_class = static::getPluginClass($plugin_id, $plugin_definition, $this->interface);
$reflector = new \ReflectionClass($plugin_class);
if ($reflector
->hasMethod('__construct')) {
$arguments = $this
->getInstanceArguments($reflector, $plugin_id, $plugin_definition, $configuration);
$instance = $reflector
->newInstanceArgs($arguments);
}
else {
$instance = new $plugin_class();
}
return $instance;
}
protected function getInstanceArguments(\ReflectionClass $reflector, $plugin_id, $plugin_definition, array $configuration) {
$arguments = [];
foreach ($reflector
->getMethod('__construct')
->getParameters() as $param) {
$param_name = $param
->getName();
if ($param_name == 'plugin_id') {
$arguments[] = $plugin_id;
}
elseif ($param_name == 'plugin_definition') {
$arguments[] = $plugin_definition;
}
elseif ($param_name == 'configuration') {
$arguments[] = $configuration;
}
elseif (isset($configuration[$param_name]) || array_key_exists($param_name, $configuration)) {
$arguments[] = $configuration[$param_name];
}
elseif ($param
->isDefaultValueAvailable()) {
$arguments[] = $param
->getDefaultValue();
}
else {
$arguments[] = NULL;
}
}
return $arguments;
}
}
Classes
Name |
Description |
ReflectionFactory |
A plugin factory that maps instance configuration to constructor arguments. |