/
CacheWarmupCommand.php
91 lines (77 loc) · 2.62 KB
/
CacheWarmupCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?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\Bundle\FrameworkBundle\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Finder\Finder;
/**
* Warmup the cache.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Francis Besset <francis.besset@gmail.com>
*/
class CacheWarmupCommand extends Command
{
protected $cacheDir;
protected $kernelTmp;
/**
* @see Command
*/
protected function configure()
{
$this
->setName('cache:warmup')
->setDescription('Warms up an empty cache')
->setDefinition(array(
new InputOption('warmup-dir', '', InputOption::VALUE_OPTIONAL, 'Warms up the cache in a specific directory')
))
->setHelp(<<<EOF
The <info>cache:warmup --warmup-dir=new_cache</info> command warms up the cache.
Before running this command, the cache must be empty if not use warmup-dir option.
EOF
)
;
}
protected function initialize(InputInterface $input, OutputInterface $output)
{
parent::initialize($input, $output);
if ($input->hasOption('warmup-dir')) {
$this->cacheDir = $input->getOption('warmup-dir');
}
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Warming up the cache');
if (!$this->cacheDir) {
$this->warmUp($this->container);
} else {
$this->kernelTmp = new \AppKernel(
$this->container->getParameter('kernel.environment'),
$this->container->getParameter('kernel.debug'),
$this->cacheDir
);
$this->container->get('filesystem')->remove($this->kernelTmp->getCacheDir());
$this->kernelTmp->boot();
unlink($this->kernelTmp->getCacheDir().DIRECTORY_SEPARATOR.$this->kernelTmp->getContainerClass().'.php');
$this->warmUp($this->kernelTmp->getContainer());
}
}
protected function warmUp(ContainerInterface $container)
{
$warmer = $container->get('cache_warmer');
$warmer->enableOptionalWarmers();
$warmer->warmUp($container->getParameter('kernel.cache_dir'));
}
}