简体   繁体   English

使用Symfony2依赖注入保留自动完成功能

[英]Preserving auto-completion abilities with Symfony2 Dependency Injection

I'm using PHP Storm as my IDE, but I believe that other IDE's such as Netbeans will have the same issue as I'll explain below. 我正在使用PHP Storm作为我的IDE,但我相信其他IDE如Netbeans将会遇到与我将在下面解释的相同的问题。

When using a framework like Symfony2, we have the wonderful world of Dependency Injection added. 当使用像Symfony2这样的框架时,我们添加了依赖注入的精彩世界。 So objects can simply be instantiated using code like the following snippet: 因此,可以使用类似以下代码段的代码简单地实例化对象:

$myThingy = $this->get('some_cool_service');

This is very handy, as objects are already configured beforehand. 这非常方便,因为事先已经配置了对象。 The one problem is, that auto-completion breaks entirely in basically any PHP IDE, as the IDE does not know what type the get() method is returning. 一个问题是,自动完成基本上完全在任何PHP IDE中断,因为IDE不知道get()方法返回什么类型。

Is there a way to preserve auto-completion? 有没有办法保留自动完成? Would creating for example an extension of Controller be the answer? 创建例如Controller的扩展会是答案吗? For example: 例如:

class MyController extends Controller {
    /**
     * @return \MyNamespace\CoolService
     */
    public getSomeCoolService() {
        return new CoolService();
    }
}

and then for application controllers, specify MyController as the base class instead of Controller? 然后对于应用程序控制器,将MyController指定为基类而不是Controller?

What about using a Factory class, or any other possible methods? 使用Factory类或任何其他可能的方法怎么样?

It is more involving, but you can still do this with eclipse PDT: 它涉及更多,但您仍然可以使用eclipse PDT执行此操作:

$myThingy = $this->get('some_cool_service');
/* @var $myThingy \MyNamespace\CoolService */

UPDATE : The example on this page shows you may also use the other way round with phpStorm: 更新此页面上的示例显示您也可以使用与phpStorm相反的方式:

$myThingy = $this->get('some_cool_service');
/* @var \MyNamespace\CoolService $myThingy */

You could define private properties in your controllers 您可以在控制器中定义私有属性

class MyController extends Controller
{
    /**
     * @var \Namespace\To\SomeCoolService;
     */
    private $my_service;

    public function myAction()
    {
        $this->my_service = $this->get('some_cool_service');
        /**
         * enjoy your autocompletion :)
         */
    }
}

I use base Controller class for bundle. 我使用base Controller类作为bundle。 You need to annotate the return in method. 您需要在方法中注释返回。 At least that works on Eclipse. 至少这适用于Eclipse。

/**
 * Gets SomeCoolService
 *
 * @return \Namespace\To\SomeCoolService
 */
protected function getSomeCoolService()
{
    return $this->get('some_cool_service');
}

I don't like /*var ... */, because it gets too much into code. 我不喜欢/ * var ... * /,因为它对代码的影响太大了。 I don't like private properties, because you can wrongly assume that services are already loaded. 我不喜欢私有属性,因为你可能错误地认为服务已经加载。

I use Komodo Studio, and tagging variables with @var, even inside methods, preserves auto completion for me. 我使用Komodo Studio,并使用@var标记变量,甚至在方法内部,为我保留自动完成。

namespace MyProject\MyBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Request;

class WelcomeController extends ContainerAware
{
    public function indexAction()
    {
        /*@var Request*/$request = $this->container->get('request');
        $request->[autocomplete hint list appears here]
    }
}

使用netbeans IDE 7.1.2 PHP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM