私は 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"];
}
}