minor #30811 [Bridge][Twig] DebugCommand - fix escaping and filter (SpacePossum)
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 https://github.com/symfony/symfony/pull/30660#issuecomment-478468584
Commits
-------
b7120c5e49
[Bridge][Twig] DebugCommand - fix escaping and filter
This commit is contained in:
commit
81955dfac9
@ -13,6 +13,7 @@ namespace Symfony\Bridge\Twig\Command;
|
|||||||
|
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Exception\InvalidArgumentException;
|
use Symfony\Component\Console\Exception\InvalidArgumentException;
|
||||||
|
use Symfony\Component\Console\Formatter\OutputFormatter;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
@ -188,12 +189,13 @@ EOF
|
|||||||
|
|
||||||
private function displayGeneralText(SymfonyStyle $io, string $filter = null)
|
private function displayGeneralText(SymfonyStyle $io, string $filter = null)
|
||||||
{
|
{
|
||||||
|
$decorated = $io->isDecorated();
|
||||||
$types = ['functions', 'filters', 'tests', 'globals'];
|
$types = ['functions', 'filters', 'tests', 'globals'];
|
||||||
foreach ($types as $index => $type) {
|
foreach ($types as $index => $type) {
|
||||||
$items = [];
|
$items = [];
|
||||||
foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) {
|
foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) {
|
||||||
if (!$filter || false !== strpos($name, $filter)) {
|
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)
|
private function displayGeneralJson(SymfonyStyle $io, $filter)
|
||||||
{
|
{
|
||||||
|
$decorated = $io->isDecorated();
|
||||||
$types = ['functions', 'filters', 'tests', 'globals'];
|
$types = ['functions', 'filters', 'tests', 'globals'];
|
||||||
$data = [];
|
$data = [];
|
||||||
foreach ($types as $type) {
|
foreach ($types as $type) {
|
||||||
@ -238,11 +241,12 @@ EOF
|
|||||||
$data['loader_paths'] = $paths;
|
$data['loader_paths'] = $paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($wronBundles = $this->findWrongBundleOverrides()) {
|
if ($wrongBundles = $this->findWrongBundleOverrides()) {
|
||||||
$data['warnings'] = $this->buildWarningMessages($wronBundles);
|
$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
|
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) {
|
if ('tests' === $type) {
|
||||||
return '';
|
return '';
|
||||||
@ -339,7 +343,7 @@ EOF
|
|||||||
return '(unknown?)';
|
return '(unknown?)';
|
||||||
}
|
}
|
||||||
} catch (\UnexpectedValueException $e) {
|
} catch (\UnexpectedValueException $e) {
|
||||||
return ' <error>'.$e->getMessage().'</error>';
|
return sprintf(' <error>%s</error>', $decorated ? OutputFormatter::escape($e->getMessage()) : $e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('globals' === $type) {
|
if ('globals' === $type) {
|
||||||
@ -347,7 +351,9 @@ EOF
|
|||||||
return ' = object('.\get_class($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) {
|
if ('functions' === $type) {
|
||||||
|
@ -289,7 +289,38 @@ TXT
|
|||||||
$this->assertContains('[OK]', $tester->getDisplay());
|
$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';
|
$projectDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
|
||||||
$loader = new FilesystemLoader([], $projectDir);
|
$loader = new FilesystemLoader([], $projectDir);
|
||||||
@ -305,8 +336,13 @@ TXT
|
|||||||
$loader = new ChainLoader([$loader]);
|
$loader = new ChainLoader([$loader]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$environment = new Environment($loader);
|
||||||
|
foreach ($globals as $name => $value) {
|
||||||
|
$environment->addGlobal($name, $value);
|
||||||
|
}
|
||||||
|
|
||||||
$application = new Application();
|
$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');
|
$command = $application->find('debug:twig');
|
||||||
|
|
||||||
return new CommandTester($command);
|
return new CommandTester($command);
|
||||||
|
Reference in New Issue
Block a user