8000 [FrameworkBundle] Alter container class instead of kernel name in cache:clear command by nicolas-grekas · Pull Request #20147 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Alter container class instead of kernel name in cache:clear command #20147

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
Oct 5, 2016
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
37 changes: 14 additions & 23 deletions src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,13 @@ protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = tr
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
Expand All @@ -195,8 +186,8 @@ protected function getTempKernel(KernelInterface $parent, $namespace, $parentCla
// 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 = <<<EOF
<?php

Expand All @@ -209,11 +200,6 @@ public function getCacheDir()
return $cacheDir;
}

public function getName()
{
return $name;
}

public function getRootDir()
{
return $rootDir;
Expand All @@ -224,6 +210,11 @@ public function getLogDir()
return $logDir;
}

protected function getContainerClass()
{
return $containerClass;
}

protected function buildContainer()
{
\$container = parent::buildContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
$loader->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'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,6 @@ public function testCacheIsFreshAfterCacheClearedWithWarmup()
}
}
$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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ protected function createContainer(array $data = array())
'kernel.environment' => 'test',
'kernel.name' => 'kernel',
'kernel.root_dir' => __DIR__,
'kernel.container_class' => 'testContainer',
), $data)));
}

Expand Down
0