Skip to content

Commit

Permalink
minor #30811 [Bridge][Twig] DebugCommand - fix escaping and filter (S…
Browse files Browse the repository at this point in the history
…pacePossum)

This PR was merged into the 4.2 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

cherry-pick on 4.2, conflicts resolved, original PR  #30660 (comment)

Commits
-------

b7120c5 [Bridge][Twig] DebugCommand - fix escaping and filter
  • Loading branch information
fabpot committed Apr 1, 2019
2 parents 9968443 + b7120c5 commit 81955df
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
20 changes: 13 additions & 7 deletions src/Symfony/Bridge/Twig/Command/DebugCommand.php
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
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

0 comments on commit 81955df

Please sign in to comment.