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

发布于 2024-12-02 02:38:23 字数 618 浏览 1 评论 0原文

我使用 PHP Storm 作为 IDE,但我相信其他 IDE(例如 Netbeans)也会遇到相同的问题,我将在下面解释。

当使用像 Symfony2 这样的框架时,我们添加了依赖注入的美妙世界。因此,可以使用如下代码片段简单地实例化对象:

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

这非常方便,因为对象已经预先配置。一个问题是,自动完成功能在基本上任何 PHP IDE 中都会完全中断,因为 IDE 不知道 get() 方法返回什么类型。

有没有办法保留自动完成功能?例如,创建控制器的扩展是答案吗?例如:

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

然后对于应用程序控制器,指定MyController作为基类而不是Controller?

使用工厂类或任何其他可能的方法怎么样?

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.

When using a framework like Symfony2, we have the wonderful world of Dependency Injection added. 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.

Is there a way to preserve auto-completion? Would creating for example an extension of Controller be the answer? 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?

What about using a Factory class, or any other possible methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏见 2024-12-09 02:38:23

它更复杂,但您仍然可以使用 eclipse PDT 来完成此操作:

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

更新
此页面上的示例显示您也可以使用 phpStorm 的相反方式:

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

It is more involving, but you can still do this with 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:

$myThingy = $this->get('some_cool_service');
/* @var \MyNamespace\CoolService $myThingy */
不忘初心 2024-12-09 02:38:23

您可以在控制器中定义私有属性

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 :)
         */
    }
}

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 :)
         */
    }
}
玩世 2024-12-09 02:38:23

我使用基本控制器类来进行捆绑。您需要在方法中注释 return。至少在 Eclipse 上是有效的。

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

我不喜欢 /*var ... */,因为它在代码中包含了太多内容。
我不喜欢私有属性,因为您可能会错误地认为服务已经加载。

I use base Controller class for bundle. You need to annotate the return in method. At least that works on 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.
I don't like private properties, because you can wrongly assume that services are already loaded.

不爱素颜 2024-12-09 02:38:23

我使用 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]
    }
}

I use Komodo Studio, and tagging variables with @var, even inside methods, preserves auto completion for me.

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]
    }
}
耀眼的星火 2024-12-09 02:38:23

使用 netbeans IDE 7.1.2 PHP

working with netbeans IDE 7.1.2 PHP

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文