Skip to content

[Bridge][Twig] DebugCommand - fix escaping and filter #30811

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

Merged
merged 1 commit into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 13 additions & 7 deletions src/Symfony/Bridge/Twig/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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;
Expand Down Expand Up @@ -188,12 +189,13 @@ private function displayPathsJson(SymfonyStyle $io, string $name)

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);
}
}

Expand Down Expand Up @@ -221,6 +223,7 @@ private function displayGeneralText(SymfonyStyle $io, string $filter = null)

private function displayGeneralJson(SymfonyStyle $io, $filter)
{
$decorated = $io->isDecorated();
$types = ['functions', 'filters', 'tests', 'globals'];
$data = [];
foreach ($types as $type) {
Expand All @@ -238,11 +241,12 @@ private function displayGeneralJson(SymfonyStyle $io, $filter)
$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
Expand Down Expand Up @@ -327,7 +331,7 @@ private function getMetadata($type, $entity)
}
}

private function getPrettyMetadata($type, $entity)
private function getPrettyMetadata($type, $entity, $decorated)
{
if ('tests' === $type) {
return '';
Expand All @@ -339,15 +343,17 @@ private function getPrettyMetadata($type, $entity)
return '(unknown?)';
}
} catch (\UnexpectedValueException $e) {
return ' <error>'.$e->getMessage().'</error>';
return sprintf(' <error>%s</error>', $decorated ? OutputFormatter::escape($e->getMessage()) : $e->getMessage());
}

if ('globals' === $type) {
if (\is_object($meta)) {
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) {
Expand Down
40 changes: 38 additions & 2 deletions src/Symfony/Bridge/Twig/Tests/Command/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,38 @@ public function testDebugTemplateNameWithChainLoader()
$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 = '<error>foo</error>';
$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' => '<error>foo</error>'];
$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);
Expand All @@ -305,8 +336,13 @@ private function createCommandTester(array $paths = [], array $bundleMetadata =
$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);
Expand Down