如何让我的 PHP IDE 理解依赖注入容器?
目前情况:我的项目中有依赖项,我通过使用依赖项注入来解决。我想通过使用依赖项注入容器 (DIC) 来执行下一个逻辑步骤,以简化依赖项的管理并延迟加载类。
我查看了 Bucket,Pimple 和 sfServiceContainer 进行了一些测试,并且确实了解 DIC 的工作方式。我可能会选择 Pimple,因为它简单且强大。如果我没有遇到这个问题:
由于 DIC 提供的抽象,我正在使用的 IDE (PHPStorm) 不再理解我的代码中发生的情况。它不明白 $container['mailer'] 或 $sc->mailer 持有类对象。我也尝试过 Netbeans IDE:同样的问题。
这对我来说确实是一个问题,因为我的 IDE 变得毫无用处。在处理类时,我不想在没有代码提示、自动完成和重构工具的情况下进行编程。我不希望我的 IDE 在验证代码时发现各种误报。
所以我的问题是:有人处理过这个问题并找到解决方案吗?
Current situation: I have dependencies in my project that I solve by using dependency injection. I want to take the next logic step by using a dependency injection container (DIC) to ease the management of my dependencies and to lazy-load classes.
I looked at Bucket, Pimple, and sfServiceContainer, ran some test and really appreciate how DIC’s work. I’d probably go for Pimple because of its simplicity and raw power. If I didn’t have this problem:
Due to the abstraction that DIC’s offer, the IDE I’m using (PHPStorm) no longer understands what’s going on in my code. It doesn’t understand that $container['mailer'] or $sc->mailer is holding a class object. I also tried Netbeans IDE: same problem.
This is really a problem for me because my IDE becomes useless. I don’t want to program without code hints, autocompletion and refactoring tools when dealing with classes. And I don’t want my IDE to find all kinds of false positives when validating code.
So my question is: Has anyone dealt with this problem and found a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以“手动”定义变量的类:
在 PhpStorm 中(以及通过 标准),使用两个星号并在变量名称之前写入数据类型。
您可以编写没有变量名称的数据类型(但不能编写没有数据类型的名称)。
You can define class of the variable 'manually':
In PhpStorm (and by standards), use two asterisks and write the data type before the name of the variable.
You can write the data type without the name of the variable (but not the name without the data type).
虽然您每次访问容器时都可以告诉 IDE 从容器中提取的对象的类型,但最好只执行一次。以下两种解决方案都涉及对容器进行子类化。我刚刚开始使用 Pimple,无论如何都建议这样做。
对于使用通过
->
访问或通过神奇的__get
方法公开的实例成员的容器,您可以告诉 IDE 它们持有什么类型。这很棒,因为它在代码运行时不涉及任何额外的解析——只有 IDE 会受到它的干扰。对于 Pimple 和其他充当数组的容器,您可以为您需要的顶级对象创建访问器函数。虽然这意味着在创建容器时需要进行更多解析,但应该只完成一次并保存在 APC 中。无论如何,我更喜欢方法而不是数组访问,因为它将容易忘记的数组键放在自动完成的方法中。
顺便说一句,对于 NetBeans 中带有
@var
的类型提示内联变量,您需要使用/*
和一个星号。这不是文档块注释,并且不适用于/**
或//
。此外,名称位于类型之前。While you can certainly tell your IDE the type of the object pulled out of your container every time you access it, it's better to do it once. Both of the following solutions involve subclassing the container. I just started using Pimple which recommends doing this anyway.
For containers that use instance members accessed with
->
or exposed via the magic__get
method, you can tell your IDE what type they hold. This is great because it doesn't involve any additional parsing when the code is run--only the IDE is bothered by it.For Pimple and other containers that act as arrays you can create accessor functions for the top-level objects you'll need. While it means more parsing when the container is created, it should be done once and kept in APC. I vastly prefer a method over array access anyway since it places the easy-to-forget array key inside an auto-completed method.
BTW, for type-hinting inline variables with
@var
in NetBeans you need to use/*
with one asterisk. This is not a doc-block comment and doesn't work with/**
or//
. Also, the name comes before the type.由于 IDE 不执行代码,因此它们不知道并且需要您的帮助。我知道这也适用于 Eclipse 和其他 IDE:提示变量的类型。
Netbeans / Phpstorm / PDT / ZendStudio 示例
代码完成再次开始在
$mailer
上运行。对于 PDT,重要的是:
*
开头。替代注释变体
由于它受到了很多讨论,因此它在 IDE 之间可能有所不同。然而,大多数 IDE 支持以上述方式对内联代码变量进行变量提示。因此,根据 IDE,这可能会以不同的方式编写,但类似,就像这里前面有两个星号:
PHPDoc 兼容性
如果您模仿内联代码的类 var doc-comment,PHPDoc 解析器可能会出现问题,如下所示:
该文档通常用于类变量(@var - 记录类变量的数据类型)。 PHPDoc 在注释后缺少类变量的定义,这给 QA 带来了负担。
然而,当以 PHPDoc 类变量风格编写时,某些 IDE 也会为简单变量提供代码完成功能。我不知道这是否会对当前类的代码完成产生副作用,然后作为新成员可能会引入实际上不存在的成员。
As the IDE's do not exectue the code, they do not know and need some help form you. I know this works for Eclipse and other IDEs as well: Hint the variable's type.
Netbeans / Phpstorm / PDT / ZendStudio Example
Code complete starts to work again on
$mailer
.For PDT it's important that:
*
only.Alternative Comment Variants
As it was subject to a lot of discussion, it can differ between IDEs. However most IDEs support variable hinting for inline code variables in the way above. So depending on the IDE this might be written differently but similar, like here with two asterisks in front:
PHPDoc compatibility
PHPDoc parsers can have a problem if you mimic the class var doc-comment for inline code as so:
That documentation is normally used for class variables (@var - Document the data type of a class variable). PHPDoc is then missing the definition of the class variable after the comment which involves a burden for QA.
However some IDEs will offer code completition for simple variables as well when written in PHPDoc clas-variable style. I do not know if that has side-effects for the code-completition of the current class then as a new member might get introduced that actually does not exists.
对于那些从谷歌来到这里的人。
PHPStorm 实际上提供了一种解决此类问题的方法,而不是一遍又一遍地编写 PHPDocs——以 此处描述 获得顺利工作的自动完成和类型检查。
For those who came here from google.
PHPStorm actually provides a way to solve this kind of problem instead of writing PHPDocs over and over again — creating and setting
.phpstorm.meta.php
file in a way described here gains smoothly working autocomplete and type inspections.Pimple只是介绍一下容器构建器的原理。如果你理解了,你就不再需要 Pimple 了:
这样 Pimple 就不会隐藏混合 $c['some key'] 中对象的类型。编辑容器时,您会收到自动完成建议。 Phpstorm 能够自动解析代码中的方法返回类型。你会有透明的容器。您可以覆盖容器:
说实话,用“编程”语言编写的容器是错误的方法。容器的职责是将初始化的对象图带给调用者。访问“编程语言”可以轻松违反该责任。一些用于配置依赖关系的 DSL 更好。此外,大多数原始依赖信息(构造函数的参数类型提示)都会被 Pimple 和 sfDepenencyContainer 忽略,从而使您的配置变得臃肿且脆弱。
Pimple just introduce container builder principe. If you understand it, you don't need Pimple any more:
This way Pimple does not hide type of object in mixed $c['some key']. You would have autocomplete suggestions when edit your container. Phpstorm is able to autoresolve method return type from your code. And you would have clear container. You can ever override container:
To be honest container written in 'programming' lanuage is wrong way to go. Container responsibility is to bring initialized graph of objects to caller. Having access to 'programming language' allows to violate that responsibility with easy. Some DSL for configuring dependency is better. Moreover most of original dependency information (argument typehints of constructors) is just ignored by Pimple and sfDepenencyContainer making your configuration bloated and fragile.
我知道问题仅与 DIC 有关,但有一个 Silex Pimple Dumper 服务提供商它将容器转储到 json 文件。同一作者编写了一个 PHPStorm 插件,它可以读取该文件并使用服务名称打开自动完成功能及其类型(类、字符串等)。我正在使用这两个组件,我可以说它们是 Silex/Pimple 自动完成的不错选择。
I know that the question is about DIC only, but there is a Silex Pimple Dumper service provider which dumps the container to a json file. The same author wrote a plugin for PHPStorm which can read that file and open the autocomplete with the service names and its type (class, string and etc). I'm using those two components and I can say that are good options for auto completion for Silex/Pimple.