rod*_*ira 5 php dependency-injection inversion-of-control phpstorm pimple
我正在使用 Pimple 依赖注入器,每次我使用容器中的依赖项时,我都忍不住要仔细检查用于获取依赖项的键的拼写:
$ioc = new Pimple();
// 1. Define some object
$ioc["some-key"] = $ioc->share(function($c){ /* ... */});
// 2. Use it
$ioc["som... // Open config file and check spelling...
Run Code Online (Sandbox Code Playgroud)
PHPStorm 是否有某种方式来查找这些属性并提供自动完成功能?我考虑过使用类似的东西定义所有这些键
define('SOME_KEY', 'some-key');
// ...
$ioc[SOME_KEY] = $ioc->share(/* ... */);
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更好的方法。
编辑
这是一些示例代码:
// project_root/library/App/Injector/Ioc.php
require_once "Pimple.php";
/** @var array|Pimple $ioc */
$ioc = new Pimple();
$ioc["version"] = "1.0.1650.63";
$ioc["location-service"] = $ioc->share(function ($c) {
return new Application_Service_Location();
}
);
Run Code Online (Sandbox Code Playgroud)
事实证明,无论是否在声明 $ioc的同一文件中的 $ioc 声明之前包含 /** @var array|Pimple $ioc */ ,字符串自动完成都可以正常工作。但是,由于我使用 Zend Framework,因此我通常使用 $ioc:
// project_root/Application/Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initInjector() {
$ioc = null;
require_once LIBRARY_PATH . "/MFM/Injector/ioc.php";
Zend_Registry::set("ioc", $ioc);
}
}
// project_root/Application/Controllers/SomeController.php
class Application_Controller_SomeController extends Zend_Controller_Action {
public function IndexAction() {
/** @var Pimple $ioc */
$ioc = Zend_Registry::get("ioc");
// No IDE assistance for the string "location-service"
$service = $ioc["location-service"];
}
}
Run Code Online (Sandbox Code Playgroud)
我目前使用PhpStorm 的DynamicReturnTypePlugin并在我的 中具有以下配置dynamicReturnTypeMeta.json:
{
"methodCalls": [
{
"class": "\\MyOwnWrapperOfDIContainer",
"method": "get",
"position": 0
},
{
"class": "\\Pimple\\Container",
"method": "offsetGet",
"position": 0
}
]
}
Run Code Online (Sandbox Code Playgroud)
此配置意味着以下内容:每当在代码中我使用任何类型Pimple\Container为数组的变量(这将调用其ArrayAccess::offsetGet方法)时,PhpStorm 应该考虑返回值的类型为访问该数组时用作键的类型。IE:
$c = new Pimple\Container;
$c['MyClass']->/*here autocompletion works*/methodOfMyClass();
Run Code Online (Sandbox Code Playgroud)
我也是这样使用的:
$myClassInstance = MyOwnWrapperOfDIContainer::get(MyClass::class);
$myClassInstance->methodOfMyClass(); // autocompletion works here
Run Code Online (Sandbox Code Playgroud)
唯一的问题是当你在 Pimple 容器中注册一些依赖项时,不是通过它们的类名,而是使用你想要的其他名称。例如,自动补全在以下情况下不起作用:
$c = new Pimple\Container;
$c['my-favourite-var'] = new MyClass(1);
$c[MyClass::class] = new MyClass(2);
$c['my-favourite-var']->/*here autocompletion doesn't work*/methodOfMyClass();
$c['MyClass']->/*here autocompletion works*/methodOfMyClass();
Run Code Online (Sandbox Code Playgroud)
您仍然可以像这样解决它:
class MyFavouriteVar extends MyClass;
$c[MyFavouriteVar::class] = new MyFavouriteVar(2);
// or even like this:
$c[MyFavouriteVar::class] = new MyClass(2);
$c[MyFavouriteVar::class]->/*now autocompletion works fine*/methodOfMyClass();
Run Code Online (Sandbox Code Playgroud)
或者你必须找到其他解决方案来解决你的问题......
还可以考虑这篇文章: http://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata
这个插件 https://github.com/Sorien/silex-idea-plugin
也可以像这样解决上面提到的问题:
class MyPimple extends Pimple\Container
{
public function get($type, $desc = null) {
return $this[$type . (isset($desc) ? ':' . $desc : '')];
}
}
$c = new MyPimple;
$c[MyClass::class] = new MyClass('default');
$c[MyClass::class . ':' . 'my-special-value'] = new MyClass('special');
$c->get(MyClass::class, 'my-special-value')->/*autocompletion should work*/methodOfMyClass();
Run Code Online (Sandbox Code Playgroud)
然后应该dynamicReturnTypeMeta.json包含:
{
"methodCalls": [
{
"class": "\\MyPimple",
"method": "get",
"position": 0
}
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3674 次 |
| 最近记录: |