[Twig] Decouple Twig commands from the Famework#9855
[Twig] Decouple Twig commands from the Famework#9855GromNaN wants to merge 12 commits intosymfony:masterfrom
Conversation
There was a problem hiding this comment.
why not just Twig_Environment or is the \ also needed here?
There was a problem hiding this comment.
If you remove the \, it becomes a relative class name, and you need to add a use statement for it. and Symfony does not add use statements for classes of the global namespace
|
nice, great move decoupling ftw! 👍 |
There was a problem hiding this comment.
My idea is that someone can extend the command class to customize how the twig instance is located.
There was a problem hiding this comment.
then you should make $this->twig protected and remove this getter, following the Symfony standards
There was a problem hiding this comment.
And I don't even think it make sense. Someone extending the command will still need to call the parent constructor, and so would still need to inject twig through the constructor
|
In addition, I've just added tests for the command. |
|
I think you should add a note on the changelog about the BC break. |
There was a problem hiding this comment.
Happy to see this feature used.
But two little questions: did you try this in a symfony2 project ? And does this make the application slower ?
Because in SF2 project, twig depends of lot of things (like security...).
There was a problem hiding this comment.
This is actually an issue with the commands defined as service (introduced in 2.4): commands are not loaded lazily, so all their dependency graph is instantiated for any unrelated console call
There was a problem hiding this comment.
It would be better if the getDefinition method was static.
|
This one cannot be merged before we find a solution for lazy-loading console command as services. |
|
Couldn't this be done by moving it to the bridge as an abstract class with an abstract method |
|
Whatever we decide to do here should be done also in #9855 |
There was a problem hiding this comment.
Please allow passing the name trough, sometime one wants to rename a command for consistency.
There was a problem hiding this comment.
Actually, it's not possible to set a name in the constructor as it will be overwritten by the method configure(). The only solution is to set the command name explicitly with setName().
There was a problem hiding this comment.
It's very easy to implement: just add a $name parameter here with the standard name as default and pass it to parent::__construct(). After that, remove it from configure
There was a problem hiding this comment.
could you please also add this feature?
|
@fabbot I think something did go wrong, you linked to the same PR... And I think this can be solved by overriding this command in the TwigBundle: namespace Symfony\Bundle\TwigBundle\Command;
use Symfony\Bridge\Twig\Command\LintCommand as BaseLintCommand;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LintCommand extends BaseLintCommand implements ContainerAwareInterface
{
protected $container;
public function __construct()
{
// override constructor to not set the twig environment yet
parent::__construct(new \Twig_Environment());
}
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
$this->twig = $container->get('twig');
}
}This requires the twig property to be protected though. Or we can use the Then we can investigate more time into lazy loading service commands and after that, we can remove this "hack". |
|
Seeing that using "commands as services" requires initializing a deep dependency tree each time the application is run, I can't find a solution to lazy-inject the So, I think that's better not using the "commands as a service" feature; even if this is a very brilliant approach. In my last commit, I've followed the suggestions from @realityking and @wouterj and I've created a class in the bundle that extends the class in the bridge and retrieve the service from the container. |
src/Symfony/Bridge/Twig/CHANGELOG.md
Outdated
There was a problem hiding this comment.
I would say moved instead of added
|
... and tests are broken. |
There was a problem hiding this comment.
Afaik, container can't be null,as it's set by setContainer during initialization?
There was a problem hiding this comment.
Exact, I took that portion of code from ContainerAwareCommand; but it looks like a legacy fallback.
I've removed it.
…ainerAwareInterface instances
|
@fabpot everything as been fixed. |
There was a problem hiding this comment.
I'd make this abstract and remove the setter. This would prevent making the setter non-functioning in the Bundle Command.
There was a problem hiding this comment.
As a developer, I don't want to be forced to create a class to use this command.
I want to use the command
twig:lintin a Silex project.In this PR, I've moved the class
Symfony\Bundle\TwigBundle\Command\LintCommandtoSymfony\Bridge\Twig\Command\LintCommandand removed dependency to theContainerAwareCommand.twig:debugonce merged.