5

我正在使用 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...

PHPStorm 是否有某种方式来查找这些属性并提供自动完成功能?我考虑过使用类似的东西定义所有这些键

define('SOME_KEY', 'some-key');

// ...

$ioc[SOME_KEY] = $ioc->share(/* ... */);

但我想知道是否有更好的方法。

编辑

这是一些示例代码:

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

事实证明,无论我是否在与 $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"];
   }
}
4

2 回答 2

2

我目前使用 PhpStorm 的DynamicReturnTypePlugin并在我的 中有以下配置dynamicReturnTypeMeta.json

{
    "methodCalls": [
        {
            "class": "\\MyOwnWrapperOfDIContainer",
            "method": "get",
            "position": 0
        },
        {
            "class": "\\Pimple\\Container",
            "method": "offsetGet",
            "position": 0
        }
    ]
}

这种配置意味着:每当在代码中我使用任何类型的变量Pimple\Container作为数组(这将调用它的ArrayAccess::offsetGet方法)时,PhpStorm 应该考虑返回值的类型是在访问该数组时用作键的类型。IE:

$c = new Pimple\Container;
$c['MyClass']->/*here autocompletion works*/methodOfMyClass();

我也以这种方式使用它:

$myClassInstance = MyOwnWrapperOfDIContainer::get(MyClass::class);
$myClassInstance->methodOfMyClass(); // autocompletion works here

唯一的问题是当您在 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();

您仍然可以像这样解决它:

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

或者你必须找到解决问题的其他方法......

编辑 1

还可以考虑这篇文章: http ://confluence.jetbrains.com/display/PhpStorm/PhpStorm+Advanced+Metadata

编辑 2

这个插件 https://github.com/Sorien/silex-idea-plugin

编辑 3

也可以像这样解决上面提到的问题:

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

然后dynamicReturnTypeMeta.json应该包含:

{
    "methodCalls": [
        {
            "class": "\\MyPimple",
            "method": "get",
            "position": 0
        }
    ]
}
于 2016-04-20T17:06:56.560 回答
0

将文件“.phpstorm.meta.php”放入存储库的根目录。文件 .phpstorm.meta.php 的内容:

<?php
namespace PHPSTORM_META {
    override( \Container::get(0), map([]));
}

其中 \Container::get - 获取某个对象的方法,例如:

\Container::get('My\Super')

或者

\Container::get(My\Super::class)

现在您可以使用所有类方法的自动完成功能。

于 2018-01-17T09:26:50.250 回答