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
Prev Previous commit
Next Next commit
Remove command as a service as it requires too many services to be in…
…itialized
  • Loading branch information
GromNaN committed Dec 30, 2013
commit 678eddfe7337981ad44c1dcd093914082de7331b
17 changes: 12 additions & 5 deletions src/Symfony/Bridge/Twig/Command/LintCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ class LintCommand extends Command
private $twig;

/**
* Constructor for dependency injection.
* Sets the twig environment
*
* @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)
public function setTwigEnvironment(\Twig_Environment $twig)
{
$this->twig = $twig;
}

parent::__construct();
/**
* @return \Twig_Environment $twig
*/
protected function getTwigEnvironment()
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

The 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()
Expand Down Expand Up @@ -72,6 +78,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$twig = $this->getTwigEnvironment();
$template = null;
$filename = $input->getArgument('filename');

Expand All @@ -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)) {
Expand All @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions src/Symfony/Bundle/TwigBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
CHANGELOG
=========

2.5.0
-----

* moved `LintCommand` to the Twig bridge and registered it as a service

2.3.0
-----

Expand Down
52 changes: 52 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Command/LintCommand.php
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()
Copy link
Member

Choose a reason for hiding this comment

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

I would inline this method in the getTwigEnvironment one.

Copy link
Member Author

Choose a reason for hiding this comment

The 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()
Copy link
Member

Choose a reason for hiding this comment

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

This should be protected like the one in the Bridge.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

{
return $this->getContainer()->get('twig');
}
}
6 changes: 0 additions & 6 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<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 @@ -132,10 +131,5 @@
<argument type="service" id="twig" />
<argument>%kernel.debug%</argument>
</service>

<service id="twig.command.lint" class="%twig.command.lint.class%">
<tag name="console.command" />
<argument type="service" id="twig" />
</service>
</services>
</container>
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy