PageRenderTime 72ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php

https://github.com/sellingsource/symfony
PHP | 172 lines | 136 code | 16 blank | 20 comment | 5 complexity | 58b9dc5a03005145901a174f7d4c948e MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Command;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\HttpKernel\KernelInterface;
  15. use Symfony\Component\Finder\Finder;
  16. /**
  17. * Clear and Warmup the cache.
  18. *
  19. * @author Francis Besset <francis.besset@gmail.com>
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class CacheClearCommand extends ContainerAwareCommand
  23. {
  24. protected $name;
  25. /**
  26. * @see Command
  27. */
  28. protected function configure()
  29. {
  30. $this
  31. ->setName('cache:clear')
  32. ->setDefinition(array(
  33. new InputOption('no-warmup', '', InputOption::VALUE_NONE, 'Do not warm up the cache'),
  34. ))
  35. ->setDescription('Clear the cache')
  36. ->setHelp(<<<EOF
  37. The <info>cache:clear</info> command clears the application cache for a given environment
  38. and debug mode:
  39. <info>php app/console cache:clear --env=dev</info>
  40. <info>php app/console cache:clear --env=prod --no-debug</info>
  41. EOF
  42. )
  43. ;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected function execute(InputInterface $input, OutputInterface $output)
  49. {
  50. $realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir');
  51. $oldCacheDir = $realCacheDir.'_old';
  52. if (!is_writable($realCacheDir)) {
  53. throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $realCacheDir));
  54. }
  55. $kernel = $this->getContainer()->get('kernel');
  56. $output->writeln(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
  57. $this->getContainer()->get('cache_clearer')->clear($realCacheDir);
  58. if ($input->getOption('no-warmup')) {
  59. rename($realCacheDir, $oldCacheDir);
  60. } else {
  61. $warmupDir = $realCacheDir.'_new';
  62. $this->warmup($warmupDir);
  63. rename($realCacheDir, $oldCacheDir);
  64. rename($warmupDir, $realCacheDir);
  65. }
  66. $this->getContainer()->get('filesystem')->remove($oldCacheDir);
  67. }
  68. protected function warmup($warmupDir)
  69. {
  70. $this->getContainer()->get('filesystem')->remove($warmupDir);
  71. $parent = $this->getContainer()->get('kernel');
  72. $class = get_class($parent);
  73. $namespace = '';
  74. if (false !== $pos = strrpos($class, '\\')) {
  75. $namespace = substr($class, 0, $pos);
  76. $class = substr($class, $pos + 1);
  77. }
  78. $kernel = $this->getTempKernel($parent, $namespace, $class, $warmupDir);
  79. $kernel->boot();
  80. $warmer = $kernel->getContainer()->get('cache_warmer');
  81. $warmer->enableOptionalWarmers();
  82. $warmer->warmUp($warmupDir);
  83. // fix container files and classes
  84. $regex = '/'.preg_quote($this->getTempKernelSuffix(), '/').'/';
  85. $finder = new Finder();
  86. foreach ($finder->files()->name(get_class($kernel->getContainer()).'*')->in($warmupDir) as $file) {
  87. $content = file_get_contents($file);
  88. $content = preg_replace($regex, '', $content);
  89. // fix absolute paths to the cache directory
  90. $content = preg_replace('/'.preg_quote($warmupDir, '/').'/', preg_replace('/_new$/', '', $warmupDir), $content);
  91. file_put_contents(preg_replace($regex, '', $file), $content);
  92. unlink($file);
  93. }
  94. // fix meta references to the Kernel
  95. foreach ($finder->files()->name('*.meta')->in($warmupDir) as $file) {
  96. $content = preg_replace(
  97. '/C\:\d+\:"'.preg_quote($class.$this->getTempKernelSuffix(), '"/').'"/',
  98. sprintf('C:%s:"%s"', strlen($class), $class),
  99. file_get_contents($file)
  100. );
  101. file_put_contents($file, $content);
  102. }
  103. }
  104. protected function getTempKernelSuffix()
  105. {
  106. if (null === $this->name) {
  107. $this->name = '__'.uniqid().'__';
  108. }
  109. return $this->name;
  110. }
  111. protected function getTempKernel(KernelInterface $parent, $namespace, $class, $warmupDir)
  112. {
  113. $suffix = $this->getTempKernelSuffix();
  114. $rootDir = $parent->getRootDir();
  115. $code = <<<EOF
  116. <?php
  117. namespace $namespace
  118. {
  119. class $class$suffix extends $class
  120. {
  121. public function getCacheDir()
  122. {
  123. return '$warmupDir';
  124. }
  125. public function getRootDir()
  126. {
  127. return '$rootDir';
  128. }
  129. protected function getContainerClass()
  130. {
  131. return parent::getContainerClass().'$suffix';
  132. }
  133. }
  134. }
  135. EOF;
  136. $this->getContainer()->get('filesystem')->mkdir($warmupDir);
  137. file_put_contents($file = $warmupDir.'/kernel.tmp', $code);
  138. require_once $file;
  139. @unlink($file);
  140. $class = "$namespace\\$class$suffix";
  141. return new $class($parent->getEnvironment(), $parent->isDebug());
  142. }
  143. }