diff --git a/src/Symfony/Bridge/Twig/Command/DebugCommand.php b/src/Symfony/Bridge/Twig/Command/DebugCommand.php
index 34dc160b3456..618dce76c5a7 100644
--- a/src/Symfony/Bridge/Twig/Command/DebugCommand.php
+++ b/src/Symfony/Bridge/Twig/Command/DebugCommand.php
@@ -12,6 +12,7 @@
namespace Symfony\Bridge\Twig\Command;
use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
@@ -100,6 +101,7 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
+ $decorated = $io->isDecorated();
// BC to be removed in 4.0
if (__CLASS__ !== \get_class($this)) {
@@ -114,29 +116,35 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \RuntimeException('The Twig environment needs to be set.');
}
+ $filter = $input->getArgument('filter');
$types = ['functions', 'filters', 'tests', 'globals'];
if ('json' === $input->getOption('format')) {
$data = [];
foreach ($types as $type) {
foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) {
- $data[$type][$name] = $this->getMetadata($type, $entity);
+ if (!$filter || false !== strpos($name, $filter)) {
+ $data[$type][$name] = $this->getMetadata($type, $entity);
+ }
}
}
- $data['tests'] = array_keys($data['tests']);
+
+ if (isset($data['tests'])) {
+ $data['tests'] = array_keys($data['tests']);
+ }
+
$data['loader_paths'] = $this->getLoaderPaths();
- $io->writeln(json_encode($data));
+ $data = json_encode($data, JSON_PRETTY_PRINT);
+ $io->writeln($decorated ? OutputFormatter::escape($data) : $data);
return 0;
}
- $filter = $input->getArgument('filter');
-
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);
}
}
@@ -262,7 +270,7 @@ private function getMetadata($type, $entity)
}
}
- private function getPrettyMetadata($type, $entity)
+ private function getPrettyMetadata($type, $entity, $decorated)
{
if ('tests' === $type) {
return '';
@@ -274,7 +282,7 @@ private function getPrettyMetadata($type, $entity)
return '(unknown?)';
}
} catch (\UnexpectedValueException $e) {
- return ' '.$e->getMessage().'';
+ return sprintf(' %s', $decorated ? OutputFormatter::escape($e->getMessage()) : $e->getMessage());
}
if ('globals' === $type) {
@@ -282,7 +290,9 @@ private function getPrettyMetadata($type, $entity)
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 a3569b5602d7..3b9e479da079 100644
--- a/src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php
+++ b/src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php
@@ -62,7 +62,45 @@ public function testLineSeparatorInLoaderPaths()
$this->assertContains($loaderPaths, trim($tester->getDisplay(true)));
}
- private function createCommandTester(array $paths = [])
+ public function testWithGlobals()
+ {
+ $message = 'foo';
+ $tester = $this->createCommandTester([], ['message' => $message]);
+ $tester->execute([], ['decorated' => true]);
+
+ $display = $tester->getDisplay();
+
+ $this->assertContains(\json_encode($message), $display);
+ }
+
+ public function testWithGlobalsJson()
+ {
+ $globals = ['message' => 'foo'];
+
+ $tester = $this->createCommandTester([], $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 $globals = [])
{
$filesystemLoader = new FilesystemLoader([], \dirname(__DIR__).'/Fixtures');
foreach ($paths as $namespace => $relDirs) {
@@ -70,7 +108,13 @@ private function createCommandTester(array $paths = [])
$filesystemLoader->addPath($relDir, $namespace);
}
}
- $command = new DebugCommand(new Environment($filesystemLoader));
+
+ $environment = new Environment($filesystemLoader);
+ foreach ($globals as $name => $value) {
+ $environment->addGlobal($name, $value);
+ }
+
+ $command = new DebugCommand($environment);
$application = new Application();
$application->add($command);
diff --git a/src/Symfony/Component/Console/Tester/CommandTester.php b/src/Symfony/Component/Console/Tester/CommandTester.php
index f869995eb92d..a1d8ad70a124 100644
--- a/src/Symfony/Component/Console/Tester/CommandTester.php
+++ b/src/Symfony/Component/Console/Tester/CommandTester.php
@@ -87,6 +87,10 @@ public function execute(array $input, array $options = [])
*/
public function getDisplay($normalize = false)
{
+ if (null === $this->output) {
+ throw new \RuntimeException('Output not initialized, did you execute the command before requesting the display?');
+ }
+
rewind($this->output->getStream());
$display = stream_get_contents($this->output->getStream());