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();
}

评论 (写第一个评论)