class 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

Expanded class hierarchy of ReflectionFactory

3 files declare their use of ReflectionFactory
LayoutManager.php in drupal/core/modules/layout/lib/Drupal/layout/Plugin/Type/LayoutManager.php
Definition of Drupal\layout\Plugin\Type\LayoutManager.
MockBlockManager.php in drupal/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/MockBlockManager.php
Definition of Drupal\plugin_test\Plugin\MockBlockManager.
ResourcePluginManager.php in drupal/core/modules/rest/lib/Drupal/rest/Plugin/Type/ResourcePluginManager.php
Definition of Drupal\rest\Plugin\Type\ResourcePluginManager.

File

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

Namespace

Drupal\Component\Plugin\Factory
View source
class ReflectionFactory extends DefaultFactory {

  /**
   * Implements Drupal\Component\Plugin\Factory\FactoryInterface::createInstance().
   */
  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;
  }

  /**
   * 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 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, array $configuration) {
    $arguments = array();
    foreach ($reflector
      ->getMethod('__construct')
      ->getParameters() as $param) {
      $param_name = $param
        ->getName();
      $param_class = $param
        ->getClass();
      if ($param_name == 'plugin_id') {
        $arguments[] = $plugin_id;
      }
      elseif ($param_name == 'configuration') {
        $arguments[] = $configuration;
      }
      elseif ($param_class && $param_class
        ->isInstance($this->discovery)) {
        $arguments[] = $this->discovery;
      }
      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

Namesort descending Modifiers Type Description Overrides
DefaultFactory::$discovery protected property The object that retrieves the definitions of the plugins that this factory instantiates.
DefaultFactory::getPluginClass protected function Finds the class relevant for a given plugin.
DefaultFactory::__construct public function Constructs a Drupal\Component\Plugin\Factory\DefaultFactory object.
ReflectionFactory::createInstance public function Implements Drupal\Component\Plugin\Factory\FactoryInterface::createInstance(). Overrides DefaultFactory::createInstance
ReflectionFactory::getInstanceArguments protected function Inspects the plugin class and build a list of arguments for the constructor.