1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Liip\ImagineBundle\Command; |
4
|
|
|
|
5
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
6
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheWarmer; |
7
|
|
|
use Liip\ImagineBundle\Imagine\Data\DataManager; |
8
|
|
|
use Liip\ImagineBundle\Imagine\Filter\FilterManager; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A command to warm up images cache (i.e. pre-generate thumbnails) |
17
|
|
|
* |
18
|
|
|
* @author Konstantin Tjuterev <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class WarmCacheCommand extends ContainerAwareCommand |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
protected function configure() |
23
|
|
|
{ |
24
|
|
|
$this |
25
|
|
|
->setName('liip:imagine:cache:warm') |
26
|
|
|
->setDescription('Warms cache for paths provided by given warmers (or all warmers, if run w/o params)') |
27
|
|
|
->addOption('chunk-size', 'c', InputOption::VALUE_REQUIRED, 'Chunk size', 100) |
28
|
|
|
->addOption('force', 'f', InputOption::VALUE_NONE, 'Force cache warm up for already cached images') |
29
|
|
|
->addArgument('warmers', InputArgument::OPTIONAL|InputArgument::IS_ARRAY, 'Warmers names') |
30
|
|
|
->setHelp(<<<EOF |
31
|
|
|
The <info>%command.name%</info> command warms up cache by specified parameters. |
32
|
|
|
|
33
|
|
|
A warmer can be configured for one or more filter set. A warmer should return a list of paths. |
34
|
|
|
This command gets the paths from warmer and create cache (i.e. filtered image) for each filter configured for given warmer. |
35
|
|
|
|
36
|
|
|
Warmers should be separated by spaces: |
37
|
|
|
<info>php app/console %command.name% warmer1 warmer2</info> |
38
|
|
|
All cache for a given `warmers` will be warmed up |
39
|
|
|
|
40
|
|
|
<info>php app/console %command.name%</info> |
41
|
|
|
Cache for all warmers will be warmed up when executing this command without parameters. |
42
|
|
|
|
43
|
|
|
Note, that <info>--force</info> option will force regeneration of the cache only if warmer returns the path. |
44
|
|
|
Generally, there should be NO need to use this option, instead, use <info>liip:imagine:cache:remove</info> command to clear cache. |
45
|
|
|
Then run this command to warm-up the cache |
46
|
|
|
EOF |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
51
|
|
|
{ |
52
|
|
|
$warmers = $input->getArgument('warmers'); |
53
|
|
|
|
54
|
|
|
/** @var CacheWarmer $cacheWarmer */ |
55
|
|
|
$cacheWarmer = $this->getContainer()->get('liip_imagine.cache.warmer'); |
56
|
|
|
$cacheWarmer->setLoggerClosure($this->getLoggerClosure($output)); |
57
|
|
|
|
58
|
|
|
if ($chunkSize = $input->getOption('chunk-size')) { |
59
|
|
|
$cacheWarmer->setChunkSize($chunkSize); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$force = false; |
63
|
|
|
if ($input->getOption('force')) { |
64
|
|
|
$force = true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$cacheWarmer->warm($force, $warmers); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns Logger Closure |
72
|
|
|
* |
73
|
|
|
* @return callable |
74
|
|
|
*/ |
75
|
|
|
protected function getLoggerClosure(OutputInterface $output) |
76
|
|
|
{ |
77
|
|
|
$loggerClosure = function ($message, $msgType = 'info') use ($output) { |
78
|
|
|
$time = date('Y-m-d G:i:s'); |
79
|
|
|
$message = sprintf( |
80
|
|
|
'<comment>%s | Mem cur/peak: %dm/%dm </comment> | <' . $msgType . '>%s</' . $msgType . '>', |
81
|
|
|
$time, |
82
|
|
|
round(memory_get_usage(true) / 1024 / 1024, 1), |
83
|
|
|
round(memory_get_peak_usage(true) / 1024 / 1024, 1), |
84
|
|
|
$message |
85
|
|
|
); |
86
|
|
|
$output->writeln($message); |
87
|
|
|
}; |
88
|
|
|
return $loggerClosure; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.