class ReflectionFactory in Service Container 7
Same name and namespace in other branches
- 7.2 lib/Drupal/Component/Plugin/Factory/ReflectionFactory.php \Drupal\Component\Plugin\Factory\ReflectionFactory
A plugin factory that maps instance configuration to constructor arguments.
Provides logic for any basic plugin type that needs to provide individual plugins based upon some basic logic.
Hierarchy
- class \Drupal\Component\Plugin\Factory\DefaultFactory implements FactoryInterface
- class \Drupal\Component\Plugin\Factory\ReflectionFactory
Expanded class hierarchy of ReflectionFactory
File
- lib/
Drupal/ Component/ Plugin/ Factory/ ReflectionFactory.php, line 15 - Contains \Drupal\Component\Plugin\Factory\ReflectionFactory.
Namespace
Drupal\Component\Plugin\FactoryView source
class ReflectionFactory extends DefaultFactory {
/**
* {@inheritdoc}
*/
public function createInstance($plugin_id, array $configuration = array()) {
$plugin_definition = $this->discovery
->getDefinition($plugin_id);
$plugin_class = static::getPluginClass($plugin_id, $plugin_definition, $this->interface);
// Lets figure out of there's a constructor for this class and pull
// arguments from the $options array if so to populate it.
$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;
}
/**
* Inspects the plugin class and build a list of arguments for the constructor.
*
* This is provided as a helper method so factories extending this class can
* replace this and insert their own reflection logic.
*
* @param \ReflectionClass $reflector
* The reflector object being used to inspect the plugin class.
* @param string $plugin_id
* The identifier of the plugin implementation.
* @param mixed $plugin_definition
* The definition associated with the plugin_id.
* @param array $configuration
* An array of configuration that may be passed to the instance.
*
* @return array
* An array of arguments to be passed to the constructor.
*/
protected function getInstanceArguments(\ReflectionClass $reflector, $plugin_id, $plugin_definition, array $configuration) {
$arguments = array();
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultFactory:: |
protected | property | The object that retrieves the definitions of the plugins that this factory instantiates. | |
DefaultFactory:: |
protected | property | Defines an interface each plugin should implement. | |
DefaultFactory:: |
public static | function | Finds the class relevant for a given plugin. | |
DefaultFactory:: |
public | function | Constructs a Drupal\Component\Plugin\Factory\DefaultFactory object. | |
ReflectionFactory:: |
public | function |
Creates a pre-configured instance of a plugin. Overrides DefaultFactory:: |
|
ReflectionFactory:: |
protected | function | Inspects the plugin class and build a list of arguments for the constructor. |