public function ReflectionFactory::createInstance

Implements Drupal\Component\Plugin\Factory\FactoryInterface::createInstance().

Overrides DefaultFactory::createInstance

File

drupal/core/lib/Drupal/Component/Plugin/Factory/ReflectionFactory.php, line 22
Definition of Drupal\Component\Plugin\Factory\ReflectionFactory.

Class

ReflectionFactory
A plugin factory that maps instance configuration to constructor arguments.

Namespace

Drupal\Component\Plugin\Factory

Code

public function createInstance($plugin_id, array $configuration) {
  $plugin_class = $this
    ->getPluginClass($plugin_id);

  // 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, $configuration);
    $instance = $reflector
      ->newInstanceArgs($arguments);
  }
  else {
    $instance = new $plugin_class();
  }
  return $instance;
}