Merge branch '2.7' into 2.8

* 2.7:
  [FrameworkBundle] Alter container class instead of kernel name in cache:clear command
  [Console] Fix validation of null values using SymfonyStyle::ask()
  [Console] Escape default value and question in SymfonyStyle::ask()
This commit is contained in:
Nicolas Grekas 2016-10-05 10:09:05 +02:00
commit ed8ccd1689
6 changed files with 55 additions and 34 deletions

View File

@ -163,22 +163,13 @@ EOF
file_put_contents($file, $content); file_put_contents($file, $content);
} }
// fix references to kernel/container related classes // fix references to container's class
$fileSearch = $tempKernel->getName().ucfirst($tempKernel->getEnvironment()).'*'; $tempContainerClass = get_class($tempKernel->getContainer());
$search = array( $realContainerClass = get_class($realKernel->getContainer());
$tempKernel->getName().ucfirst($tempKernel->getEnvironment()), foreach (Finder::create()->files()->name($tempContainerClass.'*')->in($warmupDir) as $file) {
sprintf('\'kernel.name\' => \'%s\'', $tempKernel->getName()), $content = str_replace($tempContainerClass, $realContainerClass, file_get_contents($file));
sprintf('key="kernel.name">%s<', $tempKernel->getName()), file_put_contents($file, $content);
); rename($file, str_replace(DIRECTORY_SEPARATOR.$tempContainerClass, DIRECTORY_SEPARATOR.$realContainerClass, $file));
$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);
} }
// remove temp kernel file after cache warmed up // remove temp kernel file after cache warmed up
@ -201,8 +192,8 @@ EOF
// the temp kernel class name must have the same length than the real one // the temp kernel class name must have the same length than the real one
// to avoid the many problems in serialized resources files // to avoid the many problems in serialized resources files
$class = substr($parentClass, 0, -1).'_'; $class = substr($parentClass, 0, -1).'_';
// the temp kernel name must be changed too // the temp container class must be changed too
$name = var_export(substr($parent->getName(), 0, -1).'_', true); $containerClass = var_export(substr(get_class($parent->getContainer()), 0, -1).'_', true);
$code = <<<EOF $code = <<<EOF
<?php <?php
@ -215,11 +206,6 @@ namespace $namespace
return $cacheDir; return $cacheDir;
} }
public function getName()
{
return $name;
}
public function getRootDir() public function getRootDir()
{ {
return $rootDir; return $rootDir;
@ -230,6 +216,11 @@ namespace $namespace
return $logDir; return $logDir;
} }
protected function getContainerClass()
{
return $containerClass;
}
protected function buildContainer() protected function buildContainer()
{ {
\$container = parent::buildContainer(); \$container = parent::buildContainer();

View File

@ -371,7 +371,7 @@ class FrameworkExtension extends Extension
$loader->load('routing.xml'); $loader->load('routing.xml');
$container->setParameter('router.resource', $config['resource']); $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'); $router = $container->findDefinition('router.default');
$argument = $router->getArgument(2); $argument = $router->getArgument(2);
$argument['strict_requirements'] = $config['strict_requirements']; $argument['strict_requirements'] = $config['strict_requirements'];

View File

@ -81,6 +81,6 @@ class CacheClearCommandTest extends TestCase
} }
} }
$this->assertTrue($found, 'Kernel file should present as resource'); $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');
} }
} }

View File

@ -518,6 +518,7 @@ abstract class FrameworkExtensionTest extends TestCase
'kernel.environment' => 'test', 'kernel.environment' => 'test',
'kernel.name' => 'kernel', 'kernel.name' => 'kernel',
'kernel.root_dir' => __DIR__, 'kernel.root_dir' => __DIR__,
'kernel.container_class' => 'testContainer',
), $data))); ), $data)));
} }

View File

@ -18,6 +18,7 @@ use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Formatter\OutputFormatter;
/** /**
* Symfony Style Guide compliant question helper. * Symfony Style Guide compliant question helper.
@ -35,11 +36,11 @@ class SymfonyQuestionHelper extends QuestionHelper
$question->setValidator(function ($value) use ($validator) { $question->setValidator(function ($value) use ($validator) {
if (null !== $validator) { if (null !== $validator) {
$value = $validator($value); $value = $validator($value);
} } else {
// make required
// make required if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) {
if (!is_array($value) && !is_bool($value) && 0 === strlen($value)) { throw new LogicException('A value is required.');
throw new LogicException('A value is required.'); }
} }
return $value; return $value;
@ -53,7 +54,7 @@ class SymfonyQuestionHelper extends QuestionHelper
*/ */
protected function writePrompt(OutputInterface $output, Question $question) protected function writePrompt(OutputInterface $output, Question $question)
{ {
$text = $question->getQuestion(); $text = OutputFormatter::escape($question->getQuestion());
$default = $question->getDefault(); $default = $question->getDefault();
switch (true) { switch (true) {
@ -75,18 +76,18 @@ class SymfonyQuestionHelper extends QuestionHelper
$default[$key] = $choices[trim($value)]; $default[$key] = $choices[trim($value)];
} }
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, implode(', ', $default)); $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
break; break;
case $question instanceof ChoiceQuestion: case $question instanceof ChoiceQuestion:
$choices = $question->getChoices(); $choices = $question->getChoices();
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $choices[$default]); $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default]));
break; break;
default: default:
$text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, $default); $text = sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
} }
$output->writeln($text); $output->writeln($text);

View File

@ -6,6 +6,7 @@ use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\SymfonyQuestionHelper; use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
use Symfony\Component\Console\Output\StreamOutput; use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\ChoiceQuestion;
/** /**
@ -73,6 +74,33 @@ class SymfonyQuestionHelperTest extends \PHPUnit_Framework_TestCase
$this->assertOutputContains('What is your favorite superhero? [Superman, Batman]', $output); $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));
}
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) protected function getInputStream($input)
{ {
$stream = fopen('php://memory', 'r+', false); $stream = fopen('php://memory', 'r+', false);