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

评论 (写第一个评论)