Skip to content
Snippets Groups Projects
Select Git revision
  • a7b67e7af18b6a91ac4187c439f28d63a6fc1113
  • 11.x default protected
  • 11.2.x protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

bootstrap.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    DefaultFactory.php 2.32 KiB
    <?php
    /**
     * @file
     * Definition of Drupal\Component\Plugin\Factory\DefaultFactory.
     */
    
    namespace Drupal\Component\Plugin\Factory;
    
    use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
    use Drupal\Component\Plugin\Exception\PluginException;
    use Drupal\Component\Plugin\Derivative\DerivativeInterface;
    
    /**
     * Default plugin factory.
     *
     * Instantiates plugin instances by passing the full configuration array as a
     * single constructor argument. Plugin types wanting to support plugin classes
     * with more flexible constructor signatures can do so by using an alternate
     * factory such as Drupal\Component\Plugin\Factory\ReflectionFactory.
     */
    class DefaultFactory implements FactoryInterface {
    
      /**
       * The object that retrieves the definitions of the plugins that this factory instantiates.
       *
       * The plugin definition includes the plugin class and possibly other
       * information necessary for proper instantiation.
       *
       * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
       */
      protected $discovery;
    
      /**
       * Constructs a Drupal\Component\Plugin\Factory\DefaultFactory object.
       */
      public function __construct(DiscoveryInterface $discovery) {
        $this->discovery = $discovery;
      }
    
      /**
       * Implements Drupal\Component\Plugin\Factory\FactoryInterface::createInstance().
       */
      public function createInstance($plugin_id, array $configuration = array()) {
        $plugin_definition = $this->discovery->getDefinition($plugin_id);
        $plugin_class = static::getPluginClass($plugin_id, $plugin_definition);
        return new $plugin_class($configuration, $plugin_id, $plugin_definition);
      }
    
      /**
       * Finds the class relevant for a given plugin.
       *
       * @param string $plugin_id
       *   The id of a plugin.
       * @param array $plugin_definition
       *   The plugin definition associated to the plugin_id.
       *
       * @return string
       *   The appropriate class name.
       */
      public static function getPluginClass($plugin_id, array $plugin_definition = 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));
        }
    
        return $class;
      }
    }