From eed3cc5b5272dc2105b66483853d495613e20613 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Sun, 18 Sep 2016 15:25:59 +0200 Subject: [PATCH 1/3] [Console] Escape default value and question in SymfonyStyle::ask() --- .../Console/Helper/SymfonyQuestionHelper.php | 9 +++++---- .../Helper/SymfonyQuestionHelperTest.php | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php b/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php index 8abec46007..3634495340 100644 --- a/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php @@ -17,6 +17,7 @@ use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Console\Formatter\OutputFormatter; /** * Symfony Style Guide compliant question helper. @@ -52,7 +53,7 @@ class SymfonyQuestionHelper extends QuestionHelper */ protected function writePrompt(OutputInterface $output, Question $question) { - $text = $question->getQuestion(); + $text = OutputFormatter::escape($question->getQuestion()); $default = $question->getDefault(); switch (true) { @@ -74,18 +75,18 @@ class SymfonyQuestionHelper extends QuestionHelper $default[$key] = $choices[trim($value)]; } - $text = sprintf(' %s [%s]:', $text, implode(', ', $default)); + $text = sprintf(' %s [%s]:', $text, OutputFormatter::escape(implode(', ', $default))); break; case $question instanceof ChoiceQuestion: $choices = $question->getChoices(); - $text = sprintf(' %s [%s]:', $text, $choices[$default]); + $text = sprintf(' %s [%s]:', $text, OutputFormatter::escape($choices[$default])); break; default: - $text = sprintf(' %s [%s]:', $text, $default); + $text = sprintf(' %s [%s]:', $text, OutputFormatter::escape($default)); } $output->writeln($text); diff --git a/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php index 032e153f06..4b49d30978 100644 --- a/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php @@ -6,6 +6,7 @@ use Symfony\Component\Console\Helper\FormatterHelper; use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\SymfonyQuestionHelper; use Symfony\Component\Console\Output\StreamOutput; +use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Question\ChoiceQuestion; /** @@ -73,6 +74,24 @@ class SymfonyQuestionHelperTest extends \PHPUnit_Framework_TestCase $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output); } + public function testAskEscapeDefaultValue() + { + $helper = new SymfonyQuestionHelper(); + $helper->setInputStream($this->getInputStream('\\')); + $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Can I have a backslash?', '\\')); + + $this->assertOutputContains('Can I have a backslash? [\]', $output); + } + + public function testAskEscapeLabel() + { + $helper = new SymfonyQuestionHelper(); + $helper->setInputStream($this->getInputStream('sure')); + $helper->ask($this->createInputInterfaceMock(), $output = $this->createOutputInterface(), new Question('Do you want a \?')); + + $this->assertOutputContains('Do you want a \?', $output); + } + protected function getInputStream($input) { $stream = fopen('php://memory', 'r+', false); From a8b910bec2c5193b6f43c82e4af78ea51f4c815a Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Mon, 3 Oct 2016 19:29:11 +0200 Subject: [PATCH 2/3] [Console] Fix validation of null values using SymfonyStyle::ask() --- .../Component/Console/Helper/SymfonyQuestionHelper.php | 10 +++++----- .../Console/Tests/Helper/SymfonyQuestionHelperTest.php | 10 ++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php b/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php index 8abec46007..9c5c4b7c88 100644 --- a/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/SymfonyQuestionHelper.php @@ -34,11 +34,11 @@ class SymfonyQuestionHelper extends QuestionHelper $question->setValidator(function ($value) use ($validator) { if (null !== $validator) { $value = $validator($value); - } - - // make required - if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) { - throw new \Exception('A value is required.'); + } else { + // make required + if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) { + throw new \Exception('A value is required.'); + } } return $value; diff --git a/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php b/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php index 032e153f06..27e0abd79d 100644 --- a/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php +++ b/src/Symfony/Component/Console/Tests/Helper/SymfonyQuestionHelperTest.php @@ -6,6 +6,7 @@ use Symfony\Component\Console\Helper\FormatterHelper; use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\SymfonyQuestionHelper; use Symfony\Component\Console\Output\StreamOutput; +use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Question\ChoiceQuestion; /** @@ -73,6 +74,15 @@ class SymfonyQuestionHelperTest extends \PHPUnit_Framework_TestCase $this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output); } + public function testAskReturnsNullIfValidatorAllowsIt() + { + $questionHelper = new SymfonyQuestionHelper(); + $questionHelper->setInputStream($this->getInputStream("\n")); + $question = new Question('What is your favorite superhero?'); + $question->setValidator(function ($value) { return $value; }); + $this->assertNull($questionHelper->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question)); + } + protected function getInputStream($input) { $stream = fopen('php://memory', 'r+', false); From 73c96939cdaccf047a59eaddecd952ce1969d21d Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 4 Oct 2016 09:25:27 +0200 Subject: [PATCH 3/3] [FrameworkBundle] Alter container class instead of kernel name in cache:clear command --- .../Command/CacheClearCommand.php | 37 +++++++------------ .../FrameworkExtension.php | 2 +- .../CacheClearCommandTest.php | 2 +- .../FrameworkExtensionTest.php | 1 + 4 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php index 5e98e1d58f..84c2fae7d4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php @@ -157,22 +157,13 @@ EOF file_put_contents($file, $content); } - // fix references to kernel/container related classes - $fileSearch = $tempKernel->getName().ucfirst($tempKernel->getEnvironment()).'*'; - $search = array( - $tempKernel->getName().ucfirst($tempKernel->getEnvironment()), - sprintf('\'kernel.name\' => \'%s\'', $tempKernel->getName()), - sprintf('key="kernel.name">%s<', $tempKernel->getName()), - ); - $replace = array( - $realKernel->getName().ucfirst($realKernel->getEnvironment()), - sprintf('\'kernel.name\' => \'%s\'', $realKernel->getName()), - sprintf('key="kernel.name">%s<', $realKernel->getName()), - ); - foreach (Finder::create()->files()->name($fileSearch)->in($warmupDir) as $file) { - $content = str_replace($search, $replace, file_get_contents($file)); - file_put_contents(str_replace($search, $replace, $file), $content); - unlink($file); + // fix references to container's class + $tempContainerClass = get_class($tempKernel->getContainer()); + $realContainerClass = get_class($realKernel->getContainer()); + foreach (Finder::create()->files()->name($tempContainerClass.'*')->in($warmupDir) as $file) { + $content = str_replace($tempContainerClass, $realContainerClass, file_get_contents($file)); + file_put_contents($file, $content); + rename($file, str_replace(DIRECTORY_SEPARATOR.$tempContainerClass, DIRECTORY_SEPARATOR.$realContainerClass, $file)); } // remove temp kernel file after cache warmed up @@ -195,8 +186,8 @@ EOF // the temp kernel class name must have the same length than the real one // to avoid the many problems in serialized resources files $class = substr($parentClass, 0, -1).'_'; - // the temp kernel name must be changed too - $name = var_export(substr($parent->getName(), 0, -1).'_', true); + // the temp container class must be changed too + $containerClass = var_export(substr(get_class($parent->getContainer()), 0, -1).'_', true); $code = <<load('routing.xml'); $container->setParameter('router.resource', $config['resource']); - $container->setParameter('router.cache_class_prefix', $container->getParameter('kernel.name').ucfirst($container->getParameter('kernel.environment'))); + $container->setParameter('router.cache_class_prefix', $container->getParameter('kernel.container_class')); $router = $container->findDefinition('router.default'); $argument = $router->getArgument(2); $argument['strict_requirements'] = $config['strict_requirements']; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/CacheClearCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/CacheClearCommandTest.php index c351a790ac..f1140e86bb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/CacheClearCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/CacheClearCommandTest.php @@ -83,6 +83,6 @@ class CacheClearCommandTest extends TestCase } } $this->assertTrue($found, 'Kernel file should present as resource'); - $this->assertRegExp(sprintf('/\'kernel.name\'\s*=>\s*\'%s\'/', $this->kernel->getName()), file_get_contents($containerFile), 'kernel.name is properly set on the dumped container'); + $this->assertRegExp(sprintf('/\'kernel.container_class\'\s*=>\s*\'%s\'/', get_class($this->kernel->getContainer())), file_get_contents($containerFile), 'kernel.container_class is properly set on the dumped container'); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 92bd30832a..9d93263963 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -477,6 +477,7 @@ abstract class FrameworkExtensionTest extends TestCase 'kernel.environment' => 'test', 'kernel.name' => 'kernel', 'kernel.root_dir' => __DIR__, + 'kernel.container_class' => 'testContainer', ), $data))); }