10.4 插件管理器

龵龱龰龯龭龪龩龦龢龞龜龚龙龗龓龏龎龍龋龈龅龁齾齻齸齴龜齲齯龵龱齫齧龦龜齥齢齞齛龍齙齗齖齕齫

1齐龗龓龵龱龰龯龭

齌龗龓龩龦龵龱龰龯龭龚齉齅齌龗龓龩龦discovery鼲鼯龚鼮齅齌龗龓龩龦鼪鼩龜(factory)齫鼔鼓鼑齕DefaultPluginManager黿黼黻龜龚黸黼龙黵黳黯黫龏黨黥黣黠齐黻點黚默齯龵龱龋龈黗黔齢黓黑龙龩黐黻黌黈黆齫

2齐龗龓黀麽

龵龱龰龯龭麻麺龗龓麷龩麵黀麽龚麳麰麬黀麽麪麧麣麠plugin.manager龚齧麗麓點麒麎齫龎hello_world.services.yml龗龓龏龩麵龵龱龰龯黀麽:

services:
  plugin.manager.archiver:
    class: Drupal\Core\Archiver\ArchiverManager
    parent: default_plugin_manager

鹹鹷龎鹴鹰

namespace Drupal\Core\Archiver;

use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;

/**
 * Provides an Archiver plugin manager.
 *
 * @see \Drupal\Core\Archiver\Annotation\Archiver
 * @see \Drupal\Core\Archiver\ArchiverInterface
 * @see plugin_api
 */
class ArchiverManager extends DefaultPluginManager {

  /**
   * Constructs a ArchiverManager object.
   *
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
   *   keyed by the corresponding namespace to look for plugin implementations.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke the alter hook with.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct(
      'Plugin/Archiver',
      $namespaces,
      $module_handler,
      'Drupal\Core\Archiver\ArchiverInterface',
      'Drupal\Core\Archiver\Annotation\Archiver'
    );
    $this->alterInfo('archiver_info');
    $this->setCacheBackend($cache_backend, 'archiver_info_plugins');
  }

}

麠鸱齯鹹鹷鸰鸮鸬鸪龚鸧鸥鸣鸡鸝齛龍齥齕齯Plugin/Archiver黚默龜龚鼑齕龙鸚龋龈龵龱龚鸙鸖鸓龵龱鸏鸍齫黑龙齙齗齥齢鸌鸋hook_archiver_info_alter()鸚鸇鸆龵龱龗龓齫鸬鸪鼑齕黣黠id黼archiver_info_plugins鸃麺龗龓鸂鸁黣黠齫

3齐龋龈鷾鷼龭

龋龈鷾鷼龭龪鸃龩龦龗龓龋龈齯龜齯鷾鷼龚龙龁鷻麠鷷龪龩龦龜龚龙龁龈龏鷴鷲鷱鷮齯鼲鼯龚鸙齥齢鷭鷩黑龙齯鼲鼯齢黿龩黐鷨鷦齯鷥龯齫鼑齕齧齴黗黔龁鷻麠龪鸃Drupal齯龵龱黗黔齯鷢鷟龚齧麗齥齢鼑龙鷜鷛鷢鷟龚鷜麣鷘鷖齫

齾龎DefaultPluginManager::getDiscovery()鹰

 protected function getDiscovery() {
    if (!$this->discovery) {
      $discovery = new AnnotatedClassDiscovery($this->subdir, $this->namespaces, $this->pluginDefinitionAnnotationName, $this->additionalAnnotationNamespaces);
      $this->discovery = new ContainerDerivativeDiscoveryDecorator($discovery);
    }
    return $this->discovery;
  }

4齐鼑齕龵龱龰龯龭

鷉龗鸧鸥齯龵龱龰龯龭黵齥齢鼑齕龚鷈鷇鸧鸥鷄鷁齖齕麺龵龱龜:

$type = \Drupal::service(‘plugin.manager.archiver’);

鶷鶶齥齕齯龵龱鶵鶳:

$plugin_definitions = $type->getDefinitions();

鶷鶶鶲龗齯龵龱:

$plugin_definition = $type->getDefinition(‘plugin_id’);

鶯黫龩龦龵龱龁齾:

$plugin = $type->createInstance(‘plugin_id’,[‘of’ => ‘configuration values’]);

本书共79小节:

评论 (写第一个评论)