PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/symfony/2.0.0rc4/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 163 lines | 129 code | 14 blank | 20 comment | 5 complexity | d9d55df26b62f5895705bf2da33c3ac2 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  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>./app/console cache:clear --env=dev</info>
  40. <info>./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. if ($input->getOption('no-warmup')) {
  56. rename($realCacheDir, $oldCacheDir);
  57. } else {
  58. $warmupDir = $realCacheDir.'_new';
  59. $this->warmup($warmupDir);
  60. rename($realCacheDir, $oldCacheDir);
  61. rename($warmupDir, $realCacheDir);
  62. }
  63. $this->getContainer()->get('filesystem')->remove($oldCacheDir);
  64. }
  65. protected function warmup($warmupDir)
  66. {
  67. $this->getContainer()->get('filesystem')->remove($warmupDir);
  68. $parent = $this->getContainer()->get('kernel');
  69. $class = get_class($parent);
  70. $namespace = '';
  71. if (false !== $pos = strrpos($class, '\\')) {
  72. $namespace = substr($class, 0, $pos);
  73. $class = substr($class, $pos + 1);
  74. }
  75. $kernel = $this->getTempKernel($parent, $namespace, $class, $warmupDir);
  76. $kernel->boot();
  77. $warmer = $kernel->getContainer()->get('cache_warmer');
  78. $warmer->enableOptionalWarmers();
  79. $warmer->warmUp($warmupDir);
  80. // fix container files and classes
  81. $regex = '/'.preg_quote($this->getTempKernelSuffix(), '/').'/';
  82. $finder = new Finder();
  83. foreach ($finder->files()->name(get_class($kernel->getContainer()).'*')->in($warmupDir) as $file) {
  84. $content = file_get_contents($file);
  85. $content = preg_replace($regex, '', $content);
  86. file_put_contents(preg_replace($regex, '', $file), $content);
  87. unlink($file);
  88. }
  89. // fix meta references to the Kernel
  90. foreach ($finder->files()->name('*.meta')->in($warmupDir) as $file) {
  91. $content = preg_replace(
  92. '/C\:\d+\:"'.preg_quote($class.$this->getTempKernelSuffix(), '"/').'"/',
  93. sprintf('C:%s:"%s"', strlen($class), $class),
  94. file_get_contents($file)
  95. );
  96. file_put_contents($file, $content);
  97. }
  98. }
  99. protected function getTempKernelSuffix()
  100. {
  101. if (null === $this->name) {
  102. $this->name = '__'.uniqid().'__';
  103. }
  104. return $this->name;
  105. }
  106. protected function getTempKernel(KernelInterface $parent, $namespace, $class, $warmupDir)
  107. {
  108. $suffix = $this->getTempKernelSuffix();
  109. $rootDir = $parent->getRootDir();
  110. $code = <<<EOF
  111. <?php
  112. namespace $namespace
  113. {
  114. class $class$suffix extends $class
  115. {
  116. public function getCacheDir()
  117. {
  118. return '$warmupDir';
  119. }
  120. public function getRootDir()
  121. {
  122. return '$rootDir';
  123. }
  124. protected function getContainerClass()
  125. {
  126. return parent::getContainerClass().'$suffix';
  127. }
  128. }
  129. }
  130. EOF;
  131. $this->getContainer()->get('filesystem')->mkdir($warmupDir);
  132. file_put_contents($file = $warmupDir.'/kernel.tmp', $code);
  133. require_once $file;
  134. @unlink($file);
  135. $class = "$namespace\\$class$suffix";
  136. return new $class($parent->getEnvironment(), $parent->isDebug());
  137. }
  138. }