-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
…itialized
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,15 +27,21 @@ class LintCommand extends Command | |
private $twig; | ||
|
||
/** | ||
* Constructor for dependency injection. | ||
* Sets the twig environment | ||
* | ||
* @param \Twig_Environment $twig | ||
*/ | ||
public function __construct(\Twig_Environment $twig) | ||
public function setTwigEnvironment(\Twig_Environment $twig) | ||
{ | ||
$this->twig = $twig; | ||
} | ||
|
||
parent::__construct(); | ||
/** | ||
* @return \Twig_Environment $twig | ||
*/ | ||
protected function getTwigEnvironment() | ||
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. 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a developer, I don't want to be forced to create a class to use this command. |
||
{ | ||
return $this->twig; | ||
} | ||
|
||
protected function configure() | ||
|
@@ -72,6 +78,7 @@ protected function configure() | |
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$twig = $this->getTwigEnvironment(); | ||
$template = null; | ||
$filename = $input->getArgument('filename'); | ||
|
||
|
@@ -84,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
$template .= fread(STDIN, 1024); | ||
} | ||
|
||
return $this->validateTemplate($this->twig, $output, $template); | ||
return $this->validateTemplate($twig, $output, $template); | ||
} | ||
|
||
if (0 !== strpos($filename, '@') && !is_readable($filename)) { | ||
|
@@ -103,7 +110,7 @@ protected function execute(InputInterface $input, OutputInterface $output) | |
|
||
$errors = 0; | ||
foreach ($files as $file) { | ||
$errors += $this->validateTemplate($this->twig, $output, file_get_contents($file), $file); | ||
$errors += $this->validateTemplate($twig, $output, file_get_contents($file), $file); | ||
} | ||
|
||
return $errors > 0 ? 1 : 0; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\TwigBundle\Command; | ||
|
||
use Symfony\Bridge\Twig\Command\LintCommand as BaseLintCommand; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | ||
|
||
class LintCommand extends BaseLintCommand implements ContainerAwareInterface | ||
{ | ||
/** | ||
* @var ContainerInterface|null | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @return ContainerInterface | ||
*/ | ||
protected function getContainer() | ||
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. I would inline this method in the 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. Done. |
||
{ | ||
if (null === $this->container) { | ||
$this->container = $this->getApplication()->getKernel()->getContainer(); | ||
} | ||
|
||
return $this->container; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setContainer(ContainerInterface $container = null) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getTwigEnvironment() | ||
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 should be protected like the one in the Bridge. 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. Fixed. |
||
{ | ||
return $this->getContainer()->get('twig'); | ||
} | ||
} |
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.
why not just Twig_Environment or is the \ also needed here?
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.
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