diff --git a/UPGRADE-3.4.md b/UPGRADE-3.4.md index 0e1e966d6660..503e74922ce4 100644 --- a/UPGRADE-3.4.md +++ b/UPGRADE-3.4.md @@ -83,6 +83,21 @@ TwigBridge * deprecated the `Symfony\Bridge\Twig\Form\TwigRenderer` class, use the `FormRenderer` class from the Form component instead + * deprecated `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability + to pass a command name as first argument + + * deprecated `Symfony\Bridge\Twig\Command\LintCommand::set/getTwigEnvironment` and the ability + to pass a command name as first argument + +TwigBundle +---------- + + * deprecated the `Symfony\Bundle\TwigBundle\Command\DebugCommand` class, use the `DebugCommand` + class from the Twig bridge instead + + * deprecated relying on the `ContainerAwareInterface` implementation for + `Symfony\Bundle\TwigBundle\Command\LintCommand` + Validator --------- diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index 1a7a2542376c..ca1f8f10e193 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -511,6 +511,10 @@ TwigBundle * The `ContainerAwareRuntimeLoader` class has been removed. Use the Twig `Twig_ContainerRuntimeLoader` class instead. + * Removed `DebugCommand` in favor of `Symfony\Bridge\Twig\Command\DebugCommand`. + + * Removed `ContainerAwareInterface` implementation in `Symfony\Bundle\TwigBundle\Command\LintCommand`. + TwigBridge ---------- @@ -550,6 +554,12 @@ TwigBridge * The `TwigRendererEngine::setEnvironment()` method has been removed. Pass the Twig Environment as second argument of the constructor instead. + + * Removed `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability + to pass a command name as first argument. + + * Removed `Symfony\Bridge\Twig\Command\LintCommand::set/getTwigEnvironment` and the ability + to pass a command name as first argument. Validator --------- diff --git a/src/Symfony/Bridge/Twig/CHANGELOG.md b/src/Symfony/Bridge/Twig/CHANGELOG.md index 0cd6c40e60ec..c9cfec886177 100644 --- a/src/Symfony/Bridge/Twig/CHANGELOG.md +++ b/src/Symfony/Bridge/Twig/CHANGELOG.md @@ -5,6 +5,8 @@ CHANGELOG ----- * deprecated `Symfony\Bridge\Twig\Form\TwigRenderer` + * deprecated `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability to pass a command name as first argument + * deprecated `Symfony\Bridge\Twig\Command\LintCommand::set/getTwigEnvironment` and the ability to pass a command name as first argument 3.3.0 ----- diff --git a/src/Symfony/Bridge/Twig/Command/DebugCommand.php b/src/Symfony/Bridge/Twig/Command/DebugCommand.php index e6357df51ea8..a0239b5f42a2 100644 --- a/src/Symfony/Bridge/Twig/Command/DebugCommand.php +++ b/src/Symfony/Bridge/Twig/Command/DebugCommand.php @@ -29,15 +29,27 @@ class DebugCommand extends Command private $twig; /** - * {@inheritdoc} + * @param Environment $twig */ - public function __construct($name = 'debug:twig') + public function __construct($twig = null) { - parent::__construct($name); + parent::__construct(); + + if (!$twig instanceof Environment) { + @trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED); + + $this->setName(null === $twig ? 'debug:twig' : $twig); + + return; + } + + $this->twig = $twig; } public function setTwigEnvironment(Environment $twig) { + @trigger_error(sprintf('Method "%s" is deprecated since version 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED); + $this->twig = $twig; } @@ -46,12 +58,15 @@ public function setTwigEnvironment(Environment $twig) */ protected function getTwigEnvironment() { + @trigger_error(sprintf('Method "%s" is deprecated since version 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED); + return $this->twig; } protected function configure() { $this + ->setName('debug:twig') ->setDefinition(array( new InputArgument('filter', InputArgument::OPTIONAL, 'Show details for all entries matching this filter'), new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (text or json)', 'text'), @@ -80,9 +95,17 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); - $twig = $this->getTwigEnvironment(); - if (null === $twig) { + // BC to be removed in 4.0 + if (__CLASS__ !== get_class($this)) { + $r = new \ReflectionMethod($this, 'getTwigEnvironment'); + if (__CLASS__ !== $r->getDeclaringClass()->getName()) { + @trigger_error(sprintf('Usage of method "%s" is deprecated since version 3.4 and will no longer be supported in 4.0.', get_class($this).'::getTwigEnvironment'), E_USER_DEPRECATED); + + $this->twig = $this->getTwigEnvironment(); + } + } + if (null === $this->twig) { throw new \RuntimeException('The Twig environment needs to be set.'); } @@ -91,7 +114,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($input->getOption('format') === 'json') { $data = array(); foreach ($types as $type) { - foreach ($twig->{'get'.ucfirst($type)}() as $name => $entity) { + foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) { $data[$type][$name] = $this->getMetadata($type, $entity); } } @@ -105,7 +128,7 @@ protected function execute(InputInterface $input, OutputInterface $output) foreach ($types as $index => $type) { $items = array(); - foreach ($twig->{'get'.ucfirst($type)}() as $name => $entity) { + foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) { if (!$filter || false !== strpos($name, $filter)) { $items[$name] = $name.$this->getPrettyMetadata($type, $entity); } diff --git a/src/Symfony/Bridge/Twig/Command/LintCommand.php b/src/Symfony/Bridge/Twig/Command/LintCommand.php index 01857fc1af08..c9eac4de6058 100644 --- a/src/Symfony/Bridge/Twig/Command/LintCommand.php +++ b/src/Symfony/Bridge/Twig/Command/LintCommand.php @@ -34,15 +34,27 @@ class LintCommand extends Command private $twig; /** - * {@inheritdoc} + * @param Environment $twig */ - public function __construct($name = 'lint:twig') + public function __construct($twig = null) { - parent::__construct($name); + parent::__construct(); + + if (!$twig instanceof Environment) { + @trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED); + + $this->setName(null === $twig ? 'lint:twig' : $twig); + + return; + } + + $this->twig = $twig; } public function setTwigEnvironment(Environment $twig) { + @trigger_error(sprintf('Method "%s" is deprecated since version 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED); + $this->twig = $twig; } @@ -51,12 +63,15 @@ public function setTwigEnvironment(Environment $twig) */ protected function getTwigEnvironment() { + @trigger_error(sprintf('Method "%s" is deprecated since version 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED); + return $this->twig; } protected function configure() { $this + ->setName('lint:twig') ->setDescription('Lints a template and outputs encountered errors') ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt') ->addArgument('filename', InputArgument::IS_ARRAY) @@ -86,7 +101,16 @@ protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); - if (null === $twig = $this->getTwigEnvironment()) { + // BC to be removed in 4.0 + if (__CLASS__ !== get_class($this)) { + $r = new \ReflectionMethod($this, 'getTwigEnvironment'); + if (__CLASS__ !== $r->getDeclaringClass()->getName()) { + @trigger_error(sprintf('Usage of method "%s" is deprecated since version 3.4 and will no longer be supported in 4.0.', get_class($this).'::getTwigEnvironment'), E_USER_DEPRECATED); + + $this->twig = $this->getTwigEnvironment(); + } + } + if (null === $this->twig) { throw new \RuntimeException('The Twig environment needs to be set.'); } @@ -102,20 +126,20 @@ protected function execute(InputInterface $input, OutputInterface $output) $template .= fread(STDIN, 1024); } - return $this->display($input, $output, $io, array($this->validate($twig, $template, uniqid('sf_', true)))); + return $this->display($input, $output, $io, array($this->validate($template, uniqid('sf_', true)))); } - $filesInfo = $this->getFilesInfo($twig, $filenames); + $filesInfo = $this->getFilesInfo($filenames); return $this->display($input, $output, $io, $filesInfo); } - private function getFilesInfo(Environment $twig, array $filenames) + private function getFilesInfo(array $filenames) { $filesInfo = array(); foreach ($filenames as $filename) { foreach ($this->findFiles($filename) as $file) { - $filesInfo[] = $this->validate($twig, file_get_contents($file), $file); + $filesInfo[] = $this->validate(file_get_contents($file), $file); } } @@ -133,17 +157,17 @@ protected function findFiles($filename) throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename)); } - private function validate(Environment $twig, $template, $file) + private function validate($template, $file) { - $realLoader = $twig->getLoader(); + $realLoader = $this->twig->getLoader(); try { $temporaryLoader = new ArrayLoader(array((string) $file => $template)); - $twig->setLoader($temporaryLoader); - $nodeTree = $twig->parse($twig->tokenize(new Source($template, (string) $file))); - $twig->compile($nodeTree); - $twig->setLoader($realLoader); + $this->twig->setLoader($temporaryLoader); + $nodeTree = $this->twig->parse($this->twig->tokenize(new Source($template, (string) $file))); + $this->twig->compile($nodeTree); + $this->twig->setLoader($realLoader); } catch (Error $e) { - $twig->setLoader($realLoader); + $this->twig->setLoader($realLoader); return array('template' => $template, 'file' => $file, 'line' => $e->getTemplateLine(), 'valid' => false, 'exception' => $e); } diff --git a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php index ef7698c39c83..db87d6e131fd 100644 --- a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php @@ -68,15 +68,30 @@ public function testLintFileCompileTimeException() $this->assertRegExp('/ERROR in \S+ \(line /', trim($tester->getDisplay())); } + /** + * @group legacy + * @expectedDeprecation Passing a command name as the first argument of "Symfony\Bridge\Twig\Command\LintCommand::__construct" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead. + * @expectedException \RuntimeException + * @expectedExceptionMessage The Twig environment needs to be set. + */ + public function testLegacyLintCommand() + { + $command = new LintCommand(); + + $application = new Application(); + $application->add($command); + $command = $application->find('lint:twig'); + + $tester = new CommandTester($command); + $tester->execute(array()); + } + /** * @return CommandTester */ private function createCommandTester() { - $twig = new Environment(new FilesystemLoader()); - - $command = new LintCommand(); - $command->setTwigEnvironment($twig); + $command = new LintCommand(new Environment(new FilesystemLoader())); $application = new Application(); $application->add($command); diff --git a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md index 9f666dbc29dd..58a6a7229653 100644 --- a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +3.4.0 +----- + + * deprecated `Symfony\Bundle\TwigBundle\Command\DebugCommand`, use `Symfony\Bridge\Twig\Command\DebugCommand` instead + * deprecated relying on the `ContainerAwareInterface` implementation for `Symfony\Bundle\TwigBundle\Command\LintCommand` + 3.3.0 ----- diff --git a/src/Symfony/Bundle/TwigBundle/Command/DebugCommand.php b/src/Symfony/Bundle/TwigBundle/Command/DebugCommand.php index e2f25cc22ed1..de943d2bed76 100644 --- a/src/Symfony/Bundle/TwigBundle/Command/DebugCommand.php +++ b/src/Symfony/Bundle/TwigBundle/Command/DebugCommand.php @@ -11,6 +11,8 @@ namespace Symfony\Bundle\TwigBundle\Command; +@trigger_error(sprintf('The %s class is deprecated since version 3.4 and will be removed in 4.0. Use Symfony\Bridge\Twig\Command\DebugCommand instead.', DebugCommand::class), E_USER_DEPRECATED); + use Symfony\Bridge\Twig\Command\DebugCommand as BaseDebugCommand; use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait; @@ -19,6 +21,8 @@ * Lists twig functions, filters, globals and tests present in the current project. * * @author Jordi Boggiano + * + * @deprecated since version 3.4, to be removed in 4.0. */ final class DebugCommand extends BaseDebugCommand implements ContainerAwareInterface { diff --git a/src/Symfony/Bundle/TwigBundle/Command/LintCommand.php b/src/Symfony/Bundle/TwigBundle/Command/LintCommand.php index b6cbb6be7a29..1474a4de0a3f 100644 --- a/src/Symfony/Bundle/TwigBundle/Command/LintCommand.php +++ b/src/Symfony/Bundle/TwigBundle/Command/LintCommand.php @@ -24,16 +24,9 @@ */ final class LintCommand extends BaseLintCommand implements ContainerAwareInterface { + // BC to be removed in 4.0 use ContainerAwareTrait; - /** - * {@inheritdoc} - */ - protected function getTwigEnvironment() - { - return $this->container->get('twig'); - } - /** * {@inheritdoc} */ diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php index 09daf1322f30..b3cde40b6f14 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php @@ -13,6 +13,7 @@ use Symfony\Bridge\Twig\Extension\WebLinkExtension; use Symfony\Component\Config\FileLocator; +use Symfony\Component\Console\Application; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; @@ -49,6 +50,10 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('templating.xml'); } + if (class_exists(Application::class)) { + $loader->load('console.xml'); + } + if (!interface_exists('Symfony\Component\Translation\TranslatorInterface')) { $container->removeDefinition('twig.translation.extractor'); } diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/console.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/console.xml new file mode 100644 index 000000000000..92347d699c1b --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/console.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + The "%service_id%" service is deprecated since Symfony 3.4 and will be removed in 4.0. Use "twig.command.debug" instead. + + +