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

评论 (写第一个评论)