diff --git a/src/Symfony/Bridge/Twig/Command/DebugCommand.php b/src/Symfony/Bridge/Twig/Command/DebugCommand.php index 0c1e462fcf..2439c5bba6 100644 --- a/src/Symfony/Bridge/Twig/Command/DebugCommand.php +++ b/src/Symfony/Bridge/Twig/Command/DebugCommand.php @@ -13,6 +13,7 @@ namespace Symfony\Bridge\Twig\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Exception\InvalidArgumentException; +use Symfony\Component\Console\Formatter\OutputFormatter; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -188,12 +189,13 @@ EOF private function displayGeneralText(SymfonyStyle $io, string $filter = null) { + $decorated = $io->isDecorated(); $types = ['functions', 'filters', 'tests', 'globals']; foreach ($types as $index => $type) { $items = []; foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) { if (!$filter || false !== strpos($name, $filter)) { - $items[$name] = $name.$this->getPrettyMetadata($type, $entity); + $items[$name] = $name.$this->getPrettyMetadata($type, $entity, $decorated); } } @@ -221,6 +223,7 @@ EOF private function displayGeneralJson(SymfonyStyle $io, $filter) { + $decorated = $io->isDecorated(); $types = ['functions', 'filters', 'tests', 'globals']; $data = []; foreach ($types as $type) { @@ -238,11 +241,12 @@ EOF $data['loader_paths'] = $paths; } - if ($wronBundles = $this->findWrongBundleOverrides()) { - $data['warnings'] = $this->buildWarningMessages($wronBundles); + if ($wrongBundles = $this->findWrongBundleOverrides()) { + $data['warnings'] = $this->buildWarningMessages($wrongBundles); } - $io->writeln(json_encode($data)); + $data = json_encode($data, JSON_PRETTY_PRINT); + $io->writeln($decorated ? OutputFormatter::escape($data) : $data); } private function getLoaderPaths(string $name = null): array @@ -327,7 +331,7 @@ EOF } } - private function getPrettyMetadata($type, $entity) + private function getPrettyMetadata($type, $entity, $decorated) { if ('tests' === $type) { return ''; @@ -339,7 +343,7 @@ EOF return '(unknown?)'; } } catch (\UnexpectedValueException $e) { - return ' '.$e->getMessage().''; + return sprintf(' %s', $decorated ? OutputFormatter::escape($e->getMessage()) : $e->getMessage()); } if ('globals' === $type) { @@ -347,7 +351,9 @@ EOF return ' = object('.\get_class($meta).')'; } - return ' = '.substr(@json_encode($meta), 0, 50); + $description = substr(@json_encode($meta), 0, 50); + + return sprintf(' = %s', $decorated ? OutputFormatter::escape($description) : $description); } if ('functions' === $type) { diff --git a/src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php b/src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php index 76eb10884b..16f61a1e3a 100644 --- a/src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php @@ -289,7 +289,38 @@ TXT $this->assertContains('[OK]', $tester->getDisplay()); } - private function createCommandTester(array $paths = [], array $bundleMetadata = [], string $defaultPath = null, string $rootDir = null, bool $useChainLoader = false): CommandTester + public function testWithGlobals() + { + $message = 'foo'; + $tester = $this->createCommandTester([], [], null, null, false, ['message' => $message]); + $tester->execute([], ['decorated' => true]); + $display = $tester->getDisplay(); + $this->assertContains(\json_encode($message), $display); + } + + public function testWithGlobalsJson() + { + $globals = ['message' => 'foo']; + $tester = $this->createCommandTester([], [], null, null, false, $globals); + $tester->execute(['--format' => 'json'], ['decorated' => true]); + $display = $tester->getDisplay(); + $display = \json_decode($display, true); + $this->assertSame($globals, $display['globals']); + } + + public function testWithFilter() + { + $tester = $this->createCommandTester(); + $tester->execute(['--format' => 'json'], ['decorated' => false]); + $display = $tester->getDisplay(); + $display1 = \json_decode($display, true); + $tester->execute(['--filter' => 'date', '--format' => 'json'], ['decorated' => false]); + $display = $tester->getDisplay(); + $display2 = \json_decode($display, true); + $this->assertNotSame($display1, $display2); + } + + private function createCommandTester(array $paths = [], array $bundleMetadata = [], string $defaultPath = null, string $rootDir = null, bool $useChainLoader = false, array $globals = []): CommandTester { $projectDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures'; $loader = new FilesystemLoader([], $projectDir); @@ -305,8 +336,13 @@ TXT $loader = new ChainLoader([$loader]); } + $environment = new Environment($loader); + foreach ($globals as $name => $value) { + $environment->addGlobal($name, $value); + } + $application = new Application(); - $application->add(new DebugCommand(new Environment($loader), $projectDir, $bundleMetadata, $defaultPath, $rootDir)); + $application->add(new DebugCommand($environment, $projectDir, $bundleMetadata, $defaultPath, $rootDir)); $command = $application->find('debug:twig'); return new CommandTester($command);