You are here

PluginManagerBase.php in Service Container 7.2

File

src/Plugin/PluginManagerBase.php
View source
<?php

/**
 * @file
 * Contains \Drupal\service_container\Plugin\PluginManagerBase
 */
namespace Drupal\service_container\Plugin;

use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Plugin\FallbackPluginManagerInterface;
use Drupal\Component\Plugin\PluginManagerInterface;

/**
 * Base class for plugin managers.
 */
abstract class PluginManagerBase implements PluginManagerInterface {

  /**
   * The object that discovers plugins managed by this manager.
   *
   * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
   */
  protected $discovery;

  /**
   * The object that instantiates plugins managed by this manager.
   *
   * @var \Drupal\Component\Plugin\Factory\FactoryInterface
   */
  protected $factory;

  /**
   * {@inheritdoc}
   */
  public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
    return $this->discovery
      ->getDefinition($plugin_id, $exception_on_invalid);
  }

  /**
   * {@inheritdoc}
   */
  public function getDefinitions() {
    return $this->discovery
      ->getDefinitions();
  }

  /**
   * {@inheritdoc}
   */
  public function hasDefinition($plugin_id) {
    return (bool) $this->discovery
      ->hasDefinition($plugin_id);
  }

  /**
   * {@inheritdoc}
   */
  public function createInstance($plugin_id, array $configuration = array()) {

    // If this PluginManager has fallback capabilities catch
    // PluginNotFoundExceptions.
    if ($this instanceof FallbackPluginManagerInterface) {
      try {
        return $this->factory
          ->createInstance($plugin_id, $configuration);
      } catch (PluginNotFoundException $e) {
        $fallback_id = $this
          ->getFallbackPluginId($plugin_id, $configuration);
        return $this->factory
          ->createInstance($fallback_id, $configuration);
      }
    }
    else {
      return $this->factory
        ->createInstance($plugin_id, $configuration);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getInstance(array $options) {

    // 90% of core does not use the generic $mapper functionality, so use a
    // sane default function.
    if (isset($options['id'])) {
      return $this
        ->createInstance($options['id']);
    }
    return FALSE;
  }

}

Classes

Namesort descending Description
PluginManagerBase Base class for plugin managers.