简体   繁体   中英

Symfony 2: dependency injection and traits

I'm trying to find a way to use the Symfony 2 Dependency Injection component with the new PHP 5.4 traits .

To make a long story short (not so short, actually), my project has decoupled View classes that all have their own, specific constructor. Each View can use zero or more view helpers, that are defined as traits:

trait TranslatorHelper
{
    /**
     * @var Translator
     */
    protected $translator;

    /**
     * @param Translator $translator
     */
    protected function setTranslator(Translator $translator)
    {
        $this->translator = $translator;
    }

    /**
     * @param string $text
     * @return string
     */
    public function translate($text)
    {
        return $this->translator->translate($text);
    }
}

-

class UserEditView extends AbstractView
{
    use TranslatorHelper;

    public function __construct(User $user, UserEditForm $form)
    {
        // ...
    }
}

I'd like to have a method in my controller, renderView() , that performs setter injection based on all the traits used by the View class, before rendering the View:

class Controller
{
    public function renderView(View $view)
    {
        // Check what traits are used by $view, and inject their dependencies
        // {...}


        // Then render the View
        return $view->render();
    }
}

Any idea on how to do this with the DependencyInjection component?

The main problem is obviously that the Views won't be created by the DI container, but can be created anywhere in the application flow. It's only before they're rendered that the dependencies need to be injected.

A last note: I'm not tied to the Symfony component. Any lead on another DI container would be appreciated as well.

Symfony 3.3 introduced the idea of autowired services.
All you have to do is create a setter function in your trait and add the @required annotation.

private $entityManager;

/**
 * @required
 * @param EntityManagerInterface $entityManager
 */
public function setEntityManager(EntityManagerInterface $entityManager)
{
    $this->entityManager = $entityManager;
}

Reference: https://symfony.com/doc/current/service_container/autowiring.html#autowiring-other-methods-eg-setters

I think that traits are not meant to be used to do DI in this way. What I would do in a similar scenario is using constructor injection (or even setter would be fine, even tough constructor is better when possible) in the view class that implements traits to inject directly the needed services.

If you think about that the traits implemented by a class are statically defined before the application executes, thus you don't really need to inspect traits to perform a dynamic injection. You will know what services you need before running, just think to trait as if they were interfaces with some concrete method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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