Symfony2依赖注入自定义/业务类

时间:2014-11-18 08:37:19

标签: php symfony dependency-injection

我没有找到类似的问题/解决方案谷歌或Stackoverflow,所以在这里,我需要帮助。 我将PHP项目从自定义框架移植到Symfony框架(v2.5),该框架具有自定义依赖注入(基于类反射)。许多业务逻辑类都注入了属性,如果有办法在Symfony中做类似的事情而不将这些类声明为服务,我感兴趣,因为有些类用于暂时保存/操作内容来从第三方端点开始,我需要不同的实例。

更确切地说,有一种方法可以将依赖注入与使用new关键字实例化对象结合使用。因此,如果我需要一个依赖于记录器或验证器的新类,我怎么能做这样的事情。

我想解决依赖注入的内容类:


class Content extends SomeObject
{
    private $pictureUrl;
    ...

    public __construct(array $array = null)
    {
        parent::__construct($array);
    }

    public setPicture(...\Picture $picture)
    {
        // what's the best way to inject/enable this into my class
        $errors = $this->get('validator')->validate($picture->getUrl());
        ...
        $this->pictureUrl = $picture->getUrl();
    }

    ...
}

实例化前一个类的类(在这种特殊情况下,这可能是一个服务,但在其他情况下,这是另一个与业务逻辑相关的类):


class EndpointService extends Service
{
    ...

    public fetchContent()
    {
        $dataArray = $this->endpoint->fetch();
        $contentArray = array();

        foreach ($dataArray as $key => $value) {
            $content = new Content(array());
            $content->setPicture($value->picture);
            ...
            $contentArray[$key] = $content;
        }
    }
    ...
}

注意:如果存在逻辑/语法错误,请忽略它们,这是我演示问题的一个示例,并且是我的代码库的远程抽象。

2 个答案:

答案 0 :(得分:0)

通过依赖注入将validator依赖项传递给EndpointService,我看到了一种可以实现的方法。然后,您可以将其传递给新的Content

class EndpointService extends Service
{
    ...
    public function construct($validator)
    {
        $this->validator = $validator;
    }


    public fetchContent()
    {
        $dataArray = $this->endpoint->fetch();
        $contentArray = array();

        foreach ($dataArray as $key => $value) {
            $content = new Content($this->validator, array());
            $content->setPicture($value->picture);
            ...
            $contentArray[$key] = $content;
        }
    }
    ...
}

class Content extends SomeObject
{
    private $pictureUrl;
    ...

    public __construct($validator, array $array = null)
    {
        $this->validator = $validator;

        parent::__construct($array);
    }

    public setPicture(...\Picture $picture)
    {
        // what's the best way to inject/enable this into my class
        $errors = $this->validator->validate($picture->getUrl());
        ...
        $this->pictureUrl = $picture->getUrl();
    }

    ...
}

答案 1 :(得分:0)

我认为您的Content是一个模型,验证应该在EndpointService类中完成。

您可以考虑重构,以便可以将验证程序注入Service继承自EndpointService的父类,如果您不想将其注入EndpointService直接

class EndpointService extends Service
{
    protected $validator;

    public __construct(Validator $validator) {
        $this->validator = $validator;
    }

    ...

    public fetchContent()
    {
        $dataArray = $this->endpoint->fetch();
        $contentArray = array();

        foreach ($dataArray as $key => $value) {
            $content = new Content(array());
            $errors = $this->validator->validate($value->picture->getUrl());
            $content->setPicture($value->picture);
            ...
            $contentArray[$key] = $content;
        }
    }

    ...
}

在你的配置中:

// config.yml
services:
    app.endpoint_service:
        class: Acme\DemoBundle\External\EndpointService
        arguments: [@validator]