8000 [Twig] Decouple Twig commands from the Famework by GromNaN · Pull Request #9855 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Twig] Decouple Twig commands from the Famework #9855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Move the command "tiwg:lint" to the bridge
  • Loading branch information
GromNaN committed Dec 29, 2013
commit 8861f9c411ccecd022b775128928f4ce0ea87dcc
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,35 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\TwigBundle\Command;
namespace Symfony\Bridge\Twig\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Bundle\FrameworkBundle\Console\Application as FrameworkBundleApplication;

/**
* Command that will validate your template syntax and output encountered errors.
*
* @author Marc Weistroff <marc.weistroff@sensiolabs.com>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure this is you @GromNaN ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original command has been written by @marcw.

8000
*/
class LintCommand extends ContainerAwareCommand
class LintCommand extends Command
{
private $twig;

/**
* Constructor for dependency injection.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be changed to just Constructor.

*
* @param \Twig_Environment $twig
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just Twig_Environment or is the \ also needed here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

*/
public function __construct(\Twig_Environment $twig)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please allow passing the name trough, sometime one wants to rename a command for consistency.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please also add this feature?

{
$this->twig = $twig;

parent::__construct();
}

protected function configure()
{
$this
Expand Down Expand Up @@ -57,7 +72,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$twig = $this->getContainer()->get('twig');
$twig = $this->getTwig();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed anymore as you have class variable $this->twig.

$template = null;
$filename = $input->getArgument('filename');

Expand All @@ -82,7 +97,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$files = array($filename);
} elseif (is_dir($filename)) {
$files = Finder::create()->files()->in($filename)->name('*.twig');
} else {
} elseif ($this->getApplication() instanceof FrameworkBundleApplication) {
$dir = $this->getApplication()->getKernel()->locateResource($filename);
$files = Finder::create()->files()->in($dir)->name('*.twig');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this block should be moved to a method too, the last only needs to be done in Bundle command, the rest in the Bridge command.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, indeed. The Bridge command should throw an exception and the bundle should then try to find the resource using the application

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't even need to be an exception. The extended command can just check wether the result is empty and if that's the case do the final check.

Expand Down Expand Up @@ -148,4 +163,12 @@ protected function getContext($template, $line, $context = 3)

return $result;
}

/**
* @return \Twig_Environment
*/
protected function getTwig()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this getter IMO

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea is that someone can extend the command class to customize how the twig instance is located.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then you should make $this->twig protected and remove this getter, following the Symfony standards

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method removed.

{
return $this->twig;
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<parameter key="twig.translation.extractor.class">Symfony\Bridge\Twig\Translation\TwigExtractor</parameter>
<parameter key="twig.exception_listener.class">Symfony\Component\HttpKernel\EventListener\ExceptionListener</parameter>
<parameter key="twig.controller.exception.class">Symfony\Bundle\TwigBundle\Controller\ExceptionController</parameter>
<parameter key="twig.command.lint.class">Symfony\Bridge\Twig\Command\LintCommand</parameter>
</parameters>

<services>
Expand Down Expand Up @@ -131,5 +132,10 @@
<argument type="service" id="twig" />
<argument>%kernel.debug%</argument>
</service>

<service id="twig.command.lint" class="%twig.command.lint.class%">
<tag name="console.command" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
63AF
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if the getDefinition method was static.

<argument type="service" id="twig" />
</service>
</services>
</container>
0