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小节:

评论 (写第一个评论)