使用 dev:di:info 理解 Magento 依赖注入层

关于 dev:di:info CLI 工具,它可以帮助您轻松导航 Magento 中的依赖注入层。

使用 Magento 时,您可能会遇到一个比较复杂的领域,那就是依赖注入 (DI) 层。该层负责管理对象依赖关系,并在需要时将其提供给类。理解其背后的运作机制可能比较困难,尤其是对象管理器及其背后的奥秘。不过不用担心,Magento 提供了一个鲜为人知的工具,可以帮助您轻松驾驭 DI 层:
dev:di:info

什么是依赖注入层

在深入研究该dev:di:info命令之前,我们有必要先了解依赖注入层是什么以及它为什么如此重要。简单来说,DI 层负责管理 Magento 中不同类之间的关系。当一个类需要使用另一个类时,它可以通过 DI 层进行请求,而不是创建该类本身的新实例。

这种方法有很多好处,包括:

  • 更好的代码组织和可维护性
  • 更容易测试,因为依赖项可以轻松被模拟或替换
  • 提高性能,因为可以重复使用对象,而不必不断创建新对象

然而,DI 层也可能相当复杂,特别是在 Magento 中,其中有许多不同的组件和依赖项需要管理。

使用 dev:di:info?

dev:di:info参数会传递给内置的 CLI 工具bin/magento。它提供了特定类或接口的 DI 配置信息。只需将类或接口作为参数传递给bin/magento dev:di:info命令,即可查看所使用的首选项、构造函数参数、默认值以及任何挂接到该类或接口的插件。这是在 Magento 中执行 CLI 命令时可用的众多实用工具之一。

要使用dev:di:info,请打开终端并运行:

bin/magento dev:di:info "class or interface name"

请务必使用双引号将类或接口名称括起来。例如,如果你想查看该类的 DI 配置
Magento\Catalog\Ui\DataProvider\Product\Listing\DataProvider,请运行:

 bin/magento dev:di:info "Magento\Catalog\Ui\DataProvider\Product\Listing\DataProvider"

输出将显示所有构造函数参数及其默认值、挂接到该类的任何插件以及用于该类或接口的任何首选项。

理解输出

dev di info 命令的输出可能非常详细,但它提供了有关如何为特定类或接口配置 DI 层的大量信息。

以下是您可能在输出中看到的一些关键元素的细分:

  • 构造函数:指定创建类实例所需的依赖项。例如,对于类A depends on a class B,您可以指定B其为 的依赖项A
  • 插件:这些类可以拦截对给定类的方法调用,从而允许您修改或增强基类的行为。
  • 首选项:这些首选项指定应使用哪个具体类来实现特定的接口或抽象类。例如,如果您有一个接口FooInterface和一个具体类,则可以定义一个首选项,以便在每次请求时Bar使用。BarFooInterface

让我们看几个使用 dev:di:info 的示例,通过查看运行上述命令的输出来更好地理解它是如何工作的:

$ bin/magento dev:di:info "Magento\Catalog\Ui\DataProvider\Product\Listing\DataProvider"

DI configuration for the class Magento\Catalog\Ui\DataProvider\Product\Listing\DataProvider in the GLOBAL area

Preference: Magento\Catalog\Ui\DataProvider\Product\Listing\DataProvider

Constructor Parameters:
+-----------------------+-------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
| Name                  | Requested Type                                                    | Configured Value                                                                            |
+-----------------------+-------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
| name                  |                                                                   | {                                                                                           |
|                       |                                                                   |     "_vn_": "string 1"                                                                      |
|                       |                                                                   | }                                                                                           |
| reporting             | Magento\Framework\View\Element\UiComponent\DataProvider\Reporting | {                                                                                           |
|                       |                                                                   |     "_i_": "string Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\Reporting" |
|                       |                                                                   | }                                                                                           |
| searchCriteriaBuilder | Magento\Framework\Api\Search\SearchCriteriaBuilder                | {                                                                                           |
|                       |                                                                   |     "_ins_": "string Magento\\Framework\\Api\\Search\\SearchCriteriaBuilder"                |
|                       |                                                                   | }                                                                                           |
| request               | Magento\Framework\App\RequestInterface                            | {                                                                                           |
|                       |                                                                   |     "_i_": "string Magento\\Framework\\App\\Request\\Http"                                  |
|                       |                                                                   | }                                                                                           |
| filterBuilder         | Magento\Framework\Api\FilterBuilder                               | {                                                                                           |
|                       |                                                                   |     "_ins_": "string Magento\\Framework\\Api\\FilterBuilder"                                |
|                       |                                                                   | }                                                                                           |
| storeManager          | Magento\Store\Model\StoreManager                                  | {                                                                                           |
|                       |                                                                   |     "_i_": "string Magento\\Store\\Model\\StoreManager"                                     |
|                       |                                                                   | }                                                                                           |
| meta                  |                                                                   | {                                                                                           |
|                       |                                                                   |     "_v_": null                                                                             |
|                       |                                                                   | }                                                                                           |
| data                  |                                                                   | {                                                                                           |
|                       |                                                                   |     "_v_": null                                                                             |
|                       |                                                                   | }                                                                                           |
+-----------------------+-------------------------------------------------------------------+---------------------------------------------------------------------------------------------+

Plugins:
+----------------------------------------------------------+---------+-------+
| Plugin                                                   | Method  | Type  |
+----------------------------------------------------------+---------+-------+
| Magento\Tax\Plugin\Ui\DataProvider\TaxSettings           | getData | after |
| Magento\Weee\Plugin\Ui\DataProvider\WeeeSettings         | getData | after |
| Magento\Wishlist\Plugin\Ui\DataProvider\WishlistSettings | getData | after |
+----------------------------------------------------------+---------+-------+

Plugins for the Preference:
+----------------------------------------------------------+---------+-------+
| Plugin                                                   | Method  | Type  |
+----------------------------------------------------------+---------+-------+
| Magento\Tax\Plugin\Ui\DataProvider\TaxSettings           | getData | after |
| Magento\Weee\Plugin\Ui\DataProvider\WeeeSettings         | getData | after |
| Magento\Wishlist\Plugin\Ui\DataProvider\WishlistSettings | getData | after |
+----------------------------------------------------------+---------+-------+

构造函数参数

假设我们想要确认Magento\Catalog\Ui\DataProvider\Product\Listing\DataProvider类的构造函数参数。

运行命令后,输出将显示可用的构造函数参数,包括NameRequested Type和。请注意,它如何让我们知道和的Configured Value值是空数组:metadata

Constructor Parameters:
+-----------------------+-------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
| Name                  | Requested Type                                                    | Configured Value                                                                            |
+-----------------------+-------------------------------------------------------------------+---------------------------------------------------------------------------------------------+
| name                  |                                                                   | {                                                                                           |
|                       |                                                                   |     "_vn_": "string 1"                                                                      |
|                       |                                                                   | }                                                                                           |
| reporting             | Magento\Framework\View\Element\UiComponent\DataProvider\Reporting | {                                                                                           |
|                       |                                                                   |     "_i_": "string Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\Reporting" |
|                       |                                                                   | }                                                                                           |
| searchCriteriaBuilder | Magento\Framework\Api\Search\SearchCriteriaBuilder                | {                                                                                           |
|                       |                                                                   |     "_ins_": "string Magento\\Framework\\Api\\Search\\SearchCriteriaBuilder"                |
|                       |                                                                   | }                                                                                           |
| request               | Magento\Framework\App\RequestInterface                            | {                                                                                           |
|                       |                                                                   |     "_i_": "string Magento\\Framework\\App\\Request\\Http"                                  |
|                       |                                                                   | }                                                                                           |
| filterBuilder         | Magento\Framework\Api\FilterBuilder                               | {                                                                                           |
|                       |                                                                   |     "_ins_": "string Magento\\Framework\\Api\\FilterBuilder"                                |
|                       |                                                                   | }                                                                                           |
| storeManager          | Magento\Store\Model\StoreManager                                  | {                                                                                           |
|                       |                                                                   |     "_i_": "string Magento\\Store\\Model\\StoreManager"                                     |
|                       |                                                                   | }                                                                                           |
| meta                  |                                                                   | {                                                                                           |
|                       |                                                                   |     "_v_": null                                                                             |
|                       |                                                                   | }                                                                                           |
| data                  |                                                                   | {                                                                                           |
|                       |                                                                   |     "_v_": null                                                                             |
|                       |                                                                   | }                                                                                           |
+-----------------------+-------------------------------------------------------------------+---------------------------------------------------------------------------------------------+

插件

假设我们想看看哪些插件连接到了这个类。

运行相同的命令,我们可以检查输出中是否有该类的任何beforeateraround插件。它还将显示该类的所有首选项插件:

Plugins:
+----------------------------------------------------------+---------+-------+
| Plugin                                                   | Method  | Type  |
+----------------------------------------------------------+---------+-------+
| Magento\Tax\Plugin\Ui\DataProvider\TaxSettings           | getData | after |
| Magento\Weee\Plugin\Ui\DataProvider\WeeeSettings         | getData | after |
| Magento\Wishlist\Plugin\Ui\DataProvider\WishlistSettings | getData | after |
+----------------------------------------------------------+---------+-------+

Plugins for the Preference:
+----------------------------------------------------------+---------+-------+
| Plugin                                                   | Method  | Type  |
+----------------------------------------------------------+---------+-------+
| Magento\Tax\Plugin\Ui\DataProvider\TaxSettings           | getData | after |
| Magento\Weee\Plugin\Ui\DataProvider\WeeeSettings         | getData | after |
| Magento\Wishlist\Plugin\Ui\DataProvider\WishlistSettings | getData | after |
+----------------------------------------------------------+---------+-------+

看起来愿望清单模块有一个名为的插件WishlistSettings

 Magento\Wishlist\Plugin\Ui\DataProvider\WishlistSettings | getData | after |

如果您检查
Magento\Wishlist\Plugin\Ui\DataProvider\WishlistSettings该类,您会注意到它包含一种
afterGetData() 方法,这可以确认该工具正常工作。

界面偏好设置

最后,让我们检查运行相同命令的第一行,但这次传入
WishlistProviderInterfaceMagento_Wishlist 模块

bin/magento dev:di:info "Magento\Wishlist\Controller\WishlistProviderInterface"

以下是运行此命令的输出的第一部分:

$ bin/magento dev:di:info "Magento\Wishlist\Controller\WishlistProviderInterface"

DI configuration for the class Magento\Wishlist\Controller\WishlistProviderInterface in the GLOBAL area

Preference: Magento\Wishlist\Controller\WishlistProvider

Constructor Parameters:
+-----------------+--------------------------------------------+------------------+
| Name            | Requested Type                             | Configured Value |
+-----------------+--------------------------------------------+------------------+
| wishlistFactory | Magento\Wishlist\Model\WishlistFactory     |                  |
| customerSession | Magento\Customer\Model\Session             |                  |
| messageManager  | Magento\Framework\Message\ManagerInterface |                  |
| request         | Magento\Framework\App\RequestInterface     |                  |
+-----------------+--------------------------------------------+------------------+

Plugins:
+--------+--------+------+
| Plugin | Method | Type |
+--------+--------+------+

Plugins for the Preference:
+--------+--------+------+
| Plugin | Method | Type |
+--------+--------+------+

这告诉我们这个接口的作用域在全局区域,并且有一个活动的类首选项正在实现这个接口,也就是这个WishlistProvider类。如果你想了解 Magento 的整体架构,或者你正在处理包含许多不同接口和实现的大型代码库,这将特别有用。

结论

bin/magento dev:di:info命令是理解 Magento 中 DI 层的强大工具。通过检查特定类或接口的依赖项、首选项和插件,它可以帮助您更深入地了解 Magento 的架构工作原理。

发表评论