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にDynamicReturnTypePlugindynamicReturnTypeMeta.jsonを使用しており、my に次の構成があります。

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

この構成は次のことを意味します: コード内で型の変数をPimple\Container配列として使用している場合 (これはそのメソッドを呼び出しますArrayAccess::offsetGet)、PhpStorm は戻り値がその配列にアクセスするときにキーとして使用された型であると見なす必要があります。すなわち:

$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 に答える