Skip to content

[2.3][Enhancement][FrameworkBundle] Move the warmup logic to the CacheWarmerAggregate #6223

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

Closed
wants to merge 7 commits into from
Closed
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
122 changes: 13 additions & 109 deletions src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,123 +55,27 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir');
$oldCacheDir = $realCacheDir.'_old';
$container = $this->getContainer();

if (!is_writable($realCacheDir)) {
throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $realCacheDir));
$cacheDir = $container->getParameter('kernel.cache_dir');

if (!is_writable($cacheDir)) {
throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $cacheDir));
}

$kernel = $this->getContainer()->get('kernel');
$kernel = $container->get('kernel');
$output->writeln(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));

$this->getContainer()->get('cache_clearer')->clear($realCacheDir);
$container->get('cache_clearer')->clear($cacheDir);

if ($input->getOption('no-warmup')) {
rename($realCacheDir, $oldCacheDir);
$oldCacheDir = $cacheDir.'__old__';
rename($cacheDir, $oldCacheDir);
$container->get('filesystem')->remove($oldCacheDir);
} else {
$warmupDir = $realCacheDir.'_new';

$this->warmup($warmupDir, !$input->getOption('no-optional-warmers'));

rename($realCacheDir, $oldCacheDir);
rename($warmupDir, $realCacheDir);
}

$this->getContainer()->get('filesystem')->remove($oldCacheDir);
}

protected function warmup($warmupDir, $enableOptionalWarmers = true)
{
$this->getContainer()->get('filesystem')->remove($warmupDir);

$parent = $this->getContainer()->get('kernel');
$class = get_class($parent);
$namespace = '';
if (false !== $pos = strrpos($class, '\\')) {
$namespace = substr($class, 0, $pos);
$class = substr($class, $pos + 1);
}

$kernel = $this->getTempKernel($parent, $namespace, $class, $warmupDir);
$kernel->boot();

$warmer = $kernel->getContainer()->get('cache_warmer');

if ($enableOptionalWarmers) {
$warmer->enableOptionalWarmers();
}

$warmer->warmUp($warmupDir);

// fix container files and classes
$regex = '/'.preg_quote($this->getTempKernelSuffix(), '/').'/';
$finder = new Finder();
foreach ($finder->files()->name(get_class($kernel->getContainer()).'*')->in($warmupDir) as $file) {
$content = file_get_contents($file);
$content = preg_replace($regex, '', $content);

// fix absolute paths to the cache directory
$content = preg_replace('/'.preg_quote($warmupDir, '/').'/', preg_replace('/_new$/', '', $warmupDir), $content);

file_put_contents(preg_replace($regex, '', $file), $content);
unlink($file);
}

// fix meta references to the Kernel
foreach ($finder->files()->name('*.meta')->in($warmupDir) as $file) {
$content = preg_replace(
'/C\:\d+\:"'.preg_quote($class.$this->getTempKernelSuffix(), '"/').'"/',
sprintf('C:%s:"%s"', strlen($class), $class),
file_get_contents($file)
);
file_put_contents($file, $content);
$optionalEnabled = !$input->getOption('no-optional-warmers');
$warmer = $container->get('cache_warmer');
$warmer->warmUpForEnv($kernel, $container->get('filesystem'), $cacheDir, $kernel->getEnvironment(), $kernel->isDebug(), $optionalEnabled);
}
}

protected function getTempKernelSuffix()
{
if (null === $this->name) {
$this->name = '__'.uniqid().'__';
}

return $this->name;
}

protected function getTempKernel(KernelInterface $parent, $namespace, $class, $warmupDir)
{
$suffix = $this->getTempKernelSuffix();
$rootDir = $parent->getRootDir();
$code = <<<EOF
<?php

namespace $namespace
{
class $class$suffix extends $class
{
public function getCacheDir()
{
return '$warmupDir';
}

public function getRootDir()
{
return '$rootDir';
}

protected function getContainerClass()
{
return parent::getContainerClass().'$suffix';
}
}
}
EOF;
$this->getContainer()->get('filesystem')->mkdir($warmupDir);
file_put_contents($file = $warmupDir.'/kernel.tmp', $code);
require_once $file;
@unlink($file);
$class = "$namespace\\$class$suffix";

return new $class($parent->getEnvironment(), $parent->isDebug());
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
"symfony/stopwatch": "2.2.*",
"symfony/templating": "2.2.*",
"symfony/translation": "2.2.*",
"symfony/finder": "2.2.*",
"doctrine/common": ">=2.2,<2.4-dev"
},
"require-dev": {
"symfony/finder": "2.2.*"
},
"suggest": {
"symfony/console": "2.2.*",
"symfony/finder": "2.2.*",
"symfony/form": "2.2.*",
"symfony/validator": "2.2.*"
},
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Filesystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* added a delete option for the mirror() method
* added a fluid interface

2.1.0
-----
Expand Down
42 changes: 41 additions & 1 deletion src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Filesystem
* @param string $targetFile The target filename
* @param boolean $override Whether to override an existing file or not
*
* @return Filesystem the current instance
*
* @throws IOException When copy fails
*/
public function copy($originFile, $targetFile, $override = false)
Expand All @@ -48,6 +50,8 @@ public function copy($originFile, $targetFile, $override = false)
throw new IOException(sprintf('Failed to copy %s to %s', $originFile, $targetFile));
}
}

return $this;
}

/**
Expand All @@ -56,6 +60,8 @@ public function copy($originFile, $targetFile, $override = false)
* @param string|array|\Traversable $dirs The directory path
* @param integer $mode The directory mode
*
* @return Filesystem the current instance
*
* @throws IOException On any directory creation failure
*/
public function mkdir($dirs, $mode = 0777)
Expand All @@ -69,6 +75,8 @@ public function mkdir($dirs, $mode = 0777)
throw new IOException(sprintf('Failed to create %s', $dir));
}
}

return $this;
}

/**
Expand Down Expand Up @@ -96,6 +104,8 @@ public function exists($files)
* @param integer $time The touch time as a unix timestamp
* @param integer $atime The access time as a unix timestamp
*
* @return Filesystem the current instance
*
* @throws IOException When touch fails
*/
public function touch($files, $time = null, $atime = null)
Expand All @@ -109,13 +119,17 @@ public function touch($files, $time = null, $atime = null)
throw new IOException(sprintf('Failed to touch %s', $file));
}
}

return $this;
}

/**
* Removes files or directories.
*
* @param string|array|\Traversable $files A filename, an array of files, or a \Traversable instance to remove
*
* @return Filesystem the current instance
*
* @throws IOException When removal fails
*/
public function remove($files)
Expand Down Expand Up @@ -146,6 +160,8 @@ public function remove($files)
}
}
}

return $this;
}

/**
Expand All @@ -156,6 +172,8 @@ public function remove($files)
* @param integer $umask The mode mask (octal)
* @param Boolean $recursive Whether change the mod recursively or not
*
* @return Filesystem the current instance
*
* @throws IOException When the change fail
*/
public function chmod($files, $mode, $umask = 0000, $recursive = false)
Expand All @@ -168,6 +186,8 @@ public function chmod($files, $mode, $umask = 0000, $recursive = false)
throw new IOException(sprintf('Failed to chmod file %s', $file));
}
}

return $this;
}

/**
Expand All @@ -177,6 +197,8 @@ public function chmod($files, $mode, $umask = 0000, $recursive = false)
* @param string $user The new owner user name
* @param Boolean $recursive Whether change the owner recursively or not
*
* @return Filesystem the current instance
*
* @throws IOException When the change fail
*/
public function chown($files, $user, $recursive = false)
Expand All @@ -195,6 +217,8 @@ public function chown($files, $user, $recursive = false)
}
}
}

return $this;
}

/**
Expand All @@ -204,6 +228,8 @@ public function chown($files, $user, $recursive = false)
* @param string $group The group name
* @param Boolean $recursive Whether change the group recursively or not
*
* @return Filesystem the current instance
*
* @throws IOException When the change fail
*/
public function chgrp($files, $group, $recursive = false)
Expand All @@ -222,6 +248,8 @@ public function chgrp($files, $group, $recursive = false)
}
}
}

return $this;
}

/**
Expand All @@ -230,6 +258,8 @@ public function chgrp($files, $group, $recursive = false)
* @param string $origin The origin filename
* @param string $target The new filename
*
* @return Filesystem the current instance
*
* @throws IOException When target file already exists
* @throws IOException When origin cannot be renamed
*/
Expand All @@ -243,6 +273,8 @@ public function rename($origin, $target)
if (true !== @rename($origin, $target)) {
throw new IOException(sprintf('Cannot rename "%s" to "%s".', $origin, $target));
}

return $this;
}

/**
Expand All @@ -252,14 +284,16 @@ public function rename($origin, $target)
* @param string $targetDir The symbolic link name
* @param Boolean $copyOnWindows Whether to copy files if on Windows
*
* @return Filesystem the current instance
*
* @throws IOException When symlink fails
*/
public function symlink($originDir, $targetDir, $copyOnWindows = false)
{
if (!function_exists('symlink') && $copyOnWindows) {
$this->mirror($originDir, $targetDir);

return;
return $this;
}

$this->mkdir(dirname($targetDir));
Expand All @@ -284,6 +318,8 @@ public function symlink($originDir, $targetDir, $copyOnWindows = false)
throw new IOException(sprintf('Failed to create symbolic link from %s to %s', $originDir, $targetDir));
}
}

return $this;
}

/**
Expand Down Expand Up @@ -338,6 +374,8 @@ public function makePathRelative($endPath, $startPath)
* - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink())
* - $options['delete'] Whether to delete files that are not in the source directory (defaults to false)
*
* @return Filesystem the current instance
*
* @throws IOException When file type is unknown
*/
public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array())
Expand Down Expand Up @@ -393,6 +431,8 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
}
}
}

return $this;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Finder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* added Finder::path() and Finder::notPath() methods
* added finder adapters to improve performance on specific platforms
* added SplFileInfo::putContents()

2.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
*
* @author Jean-François Simon <contact@jfsimon.fr>
*/
class AdapterFailureException extends \RuntimeException implements ExceptionInterface
class AdapterFailureException extends RuntimeException
{
/**
* @var \Symfony\Component\Finder\Adapter\AdapterInterface
* @var AdapterInterface
*/
private $adapter;

Expand Down
15 changes: 11 additions & 4 deletions src/Symfony/Component/Finder/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Finder\Exception;

/**
* Base ExceptionInterface for the Finder component.
*
* @author Jean-François Simon <contact@jfsimon.fr>
*/
interface ExceptionInterface
{
/**
* @return \Symfony\Component\Finder\Adapter\AdapterInterface
*/
public function getAdapter();
}
Loading