11.4 依赖注入和插件

插件

龸龷龵龴龲龮龪龩龨龧龤龡龟龞龷龜龙龕龸龷龒龑龎龮龪龩龨龊龆龃龁齽齺齷拷齯齫齩龆齦齥齡龜龕齝龸龷齛齙齕齒齏齎齊龟齈龜齽齅 Blocks龊齛齙齕 BlockBase BlockBase 鼯鼭齕 BlockPluginInterface 龜

龮龪龩龨鼌鼋龴龲鼇鼄鼀龸龷黾龊黺龒龵黶齈黾鼀龜黴黰黯黫黩黦黥黣黠黜黙龸龷龴龲龮龪龩龨龊黖黓黒龸龷龃龊黐龒黌龵龑龎齒齏龮龪龩龨龜

鼯鼭 ContainerFactoryPluginInterface

龸龷齒齏龮龪龩龨龟黄黁龵鼯鼭 ContainerFactoryPluginInterface 龜麽龸龷麺黖黓龃龊麷麳麱麯麮麫龸龷龵麨鼯鼭齕黴鼄齎齊龜齅麥鼯鼭龊齒齏 create() __construct() 麜麚龊齅麥麖鼯鼭龊麓麒齒齏 __construct() 麜麚龜

<?php
/**
 * @file
 * Contains \Drupal\Core\Plugin\Factory\ContainerFactory.
 */

namespace Drupal\Core\Plugin\Factory;

use Drupal\Component\Plugin\Factory\DefaultFactory;

/**
 * Plugin factory which passes a container to a create method.
 */
class ContainerFactory extends DefaultFactory {

  /**
   * {@inheritdoc}
   */
  public function createInstance($plugin_id, array $configuration = array()) {
    $plugin_definition = $this->discovery->getDefinition($plugin_id);
    $plugin_class = static::getPluginClass($plugin_id, $plugin_definition, $this->interface);

    // If the plugin provides a factory method, pass the container to it.
    if (is_subclass_of($plugin_class, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) {
      return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
    }

    // Otherwise, create the plugin directly.
    return new $plugin_class($configuration, $plugin_id, $plugin_definition);
  }

}

黖黓麐鼄麌麉

鼭鼇黩黦麇黖黓麐鼄麌麉龊麃黥齈麂鹿黩黦龟鹾鹼鹺龜黩黦鹸鹶鼯鼭 ContainerFactoryPluginInterface 龴龲龮龪龩龨龜齒齏黩黦鹳鹱龟 create() __construct() 麜麚龜鹮鹭龸龷鹩龆鹧鹦龟鹥齝龊黩黦黐鹢黴鹡鹥齝鹝鹙鹗鹓鹒鹎鼄鹍鹊龜__construct() 黐鹆齏齕鹃齈鹀鸼鹺龊鸻麥鹃齈鹀鸼鹺鸺鸷龟麃黥麺鸶鸴龜

<?php

/**
 * @file
 * Contains \Drupal\di_example\Plugin\Block\DIExample.
 */

namespace Drupal\di_example\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\di_example\DITalk;

/**
 * Provides a block to that shows a conversation about mood.
 *
 * @Block(
 *   id = "di_example_conversation_mood",
 *   admin_label = @Translation("DI Example: Conversation about mood")
 * )
 */
class DIExample extends BlockBase implements ContainerFactoryPluginInterface {
  /**
   * @var $dITalk \Drupal\di_example\DITalk
   */
  protected $dITalk;

  /**
   * @param array $configuration
   * @param string $plugin_id
   * @param mixed $plugin_definition
   * @param \Drupal\di_example\DITalk $DITalk
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, DITalk $DITalk) {
    // Call parent construct method.
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    // Store our dependency.
    $this->dITalk = $DITalk;
  }

  /**
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   * @param array $configuration
   * @param string $plugin_id
   * @param mixed $plugin_definition
   * @return static
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {

    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('di_example.talk')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    // We use the injected service to get the message.
    $message = $this->dITalk->getResponseToMood();

    // We return a render array of the message.
    return [
      '#type' => 'markup',
      '#markup' => '<p>' . $this->t($message) . '</p>',
    ];
  }
}

当你不能使用依赖注入时

鼇麐鼄鸳鸰鹓鸬鸫鸪鸨龒黥齒齏龮龪龩龨龜黴黰黯黫麺鹆齏龟鹍鹊龒鼇齈鸪龊鸥鸣鸨鸢鼇齒齏麐鼄齈龟鸳鸰鹍鹊龜鹮鹭黩黦鸠龆麐鼄鼯齽鸜鸚鸗齷黐龒鸔鹆齏 __construct() 鹍鹊龊黐龒黥齒齏 $this龊鹮鹭鸏龒鸔鸎黠黜黙鸌鸋龜

鸳鸰鹓鸬鸫龕齝鼇 .module 鸫龷鷿龜齽齅黩黦鼇 module 鷿鷾鸚黴鷻齒齏鹓鷹龟鷸鷴龜

di_example.module 鷪

<?php

/**
* Implements hook_entity_load().
*
* @param $entities
* @param $entity_type
*/
function di_example_entity_load($entities, $entity_type) {
  /**
   * Because we are not in a class, we cannot use dependency injection.
   *
   * @var $mood_ring_service \Drupal\di_example\DIMoodRing
   */
  $mood_ring_service = \Drupal::service('di_example.mood_ring');

  $mood = $mood_ring_service->getMood();
}

评论 (写第一个评论)