-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from 1 commit
8861f9c
b8bd4aa
1124bb6
d500749
678eddf
a8fd92e
b008f49
3696d04
c244b14
3d0f93c
08759f0
eb831e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
*/ | ||
class LintCommand extends ContainerAwareCommand | ||
class LintCommand extends Command | ||
{ | ||
private $twig; | ||
|
||
/** | ||
* Constructor for dependency injection. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be changed to just |
||
* | ||
* @param \Twig_Environment $twig | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just Twig_Environment or is the \ also needed here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you remove the |
||
*/ | ||
public function __construct(\Twig_Environment $twig) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
8000
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -57,7 +72,7 @@ protected function configure() | |
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$twig = $this->getContainer()->get('twig'); | ||
$twig = $this->getTwig(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not needed anymore as you have class variable |
||
$template = null; | ||
$filename = $input->getArgument('filename'); | ||
|
||
|
@@ -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'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
@@ -148,4 +163,12 @@ protected function getContext($template, $line, $context = 3) | |
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @return \Twig_Environment | ||
*/ | ||
protected function getTwig() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need this getter IMO There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method removed. |
||
{ | ||
return $this->twig; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Happy to see this feature used. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better if the |
||
<argument type="service" id="twig" /> | ||
</service> | ||
</services> | ||
</container> |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.