1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Command; |
13
|
|
|
|
14
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
15
|
|
|
use Liip\ImagineBundle\Imagine\Data\DataManager; |
16
|
|
|
use Liip\ImagineBundle\Imagine\Filter\FilterManager; |
17
|
|
|
use Symfony\Component\Console\Command\Command; |
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
class ResolveCacheCommand extends Command |
24
|
|
|
{ |
25
|
|
|
protected static $defaultName = 'liip:imagine:cache:resolve'; |
26
|
|
|
|
27
|
|
|
use CacheCommandTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var DataManager |
31
|
|
|
*/ |
32
|
|
|
private $dataManager; |
33
|
|
|
|
34
|
|
|
public function __construct(DataManager $dataManager, CacheManager $cacheManager, FilterManager $filterManager) |
35
|
|
|
{ |
36
|
|
|
parent::__construct(); |
37
|
|
|
|
38
|
|
|
$this->dataManager = $dataManager; |
39
|
|
|
$this->cacheManager = $cacheManager; |
40
|
|
|
$this->filterManager = $filterManager; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function configure(): void |
44
|
|
|
{ |
45
|
|
|
$this |
46
|
|
|
->setDescription('Warms up the cache for the specified image sources with all or specified filters applied, and prints the list of cache files.') |
47
|
|
|
->addArgument('path', InputArgument::REQUIRED | InputArgument::IS_ARRAY, |
48
|
|
|
'Image file path(s) for which to generate the cached images.') |
49
|
|
|
->addOption('filter', 'f', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
50
|
|
|
'Filter(s) to use for image resolution; if none explicitly passed, use all filters.') |
51
|
|
|
->addOption('force', 'F', InputOption::VALUE_NONE, |
52
|
|
|
'Force generating the image and writing the cache, regardless of whether a cached version already exists.') |
53
|
|
|
->addOption('no-colors', 'C', InputOption::VALUE_NONE, |
54
|
|
|
'Write only un-styled text output; remove any colors, styling, etc.') |
55
|
|
|
->addOption('as-script', 'S', InputOption::VALUE_NONE, |
56
|
|
|
'Write only machine-readable output; silenced verbose reporting and implies --no-colors.') |
57
|
|
|
->setHelp(<<<'EOF' |
58
|
|
|
The <comment>%command.name%</comment> command resolves the passed image(s) for the resolved |
59
|
|
|
filter(s), outputting results using the following basic format: |
60
|
|
|
<info>image.ext[filter] (resolved|cached|failed): (resolve-image-path|exception-message)</> |
61
|
|
|
|
62
|
|
|
<comment># bin/console %command.name% --filter=thumb1 foo.ext bar.ext</comment> |
63
|
|
|
Resolve <options=bold>both</> <comment>foo.ext</comment> and <comment>bar.ext</comment> images using <options=bold>one</> filter (<comment>thumb1</comment>), outputting: |
64
|
|
|
<info>- foo.ext[thumb1] status: http://localhost/media/cache/thumb1/foo.ext</> |
65
|
|
|
<info>- bar.ext[thumb1] status: http://localhost/media/cache/thumb1/bar.ext</> |
66
|
|
|
|
67
|
|
|
<comment># bin/console %command.name% --filter=thumb1 --filter=thumb3 foo.ext</comment> |
68
|
|
|
Resolve <comment>foo.ext</comment> image using <options=bold>two</> filters (<comment>thumb1</comment> and <comment>thumb3</comment>), outputting: |
69
|
|
|
<info>- foo.ext[thumb1] status: http://localhost/media/cache/thumb1/foo.ext</> |
70
|
|
|
<info>- foo.ext[thumb3] status: http://localhost/media/cache/thumb3/foo.ext</> |
71
|
|
|
|
72
|
|
|
<comment># bin/console %command.name% foo.ext</comment> |
73
|
|
|
Resolve <comment>foo.ext</comment> image using <options=bold>all</> filters (as none were specified), outputting: |
74
|
|
|
<info>- foo.ext[thumb1] status: http://localhost/media/cache/thumb1/foo.ext</> |
75
|
|
|
<info>- foo.ext[thumb2] status: http://localhost/media/cache/thumb2/foo.ext</> |
76
|
|
|
<info>- foo.ext[thumb3] status: http://localhost/media/cache/thumb2/foo.ext</> |
77
|
|
|
|
78
|
|
|
<comment># bin/console %command.name% --force --filter=thumb1 foo.ext</comment> |
79
|
|
|
Resolve <comment>foo.ext</comment> image using <options=bold>one</> filter (<comment>thumb1</comment>) and <options=bold>forcing resolution</> (regardless of cache), outputting: |
80
|
|
|
<info>- foo.ext[thumb1] resolved: http://localhost/media/cache/thumb1/foo.ext</> |
81
|
|
|
|
82
|
|
|
EOF |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
87
|
|
|
{ |
88
|
|
|
$this->setupOutputStyle($input, $output); |
89
|
|
|
$this->outputCommandHeader(); |
90
|
|
|
|
91
|
|
|
$forced = $input->getOption('force'); |
92
|
|
|
[$images, $filters] = $this->resolveInputFiltersAndPaths($input); |
|
|
|
|
93
|
|
|
|
94
|
|
|
foreach ($images as $i) { |
95
|
|
|
foreach ($filters as $f) { |
96
|
|
|
$this->runCacheImageResolve($i, $f, $forced); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->outputCommandResult($images, $filters, 'resolution'); |
101
|
|
|
|
102
|
|
|
return $this->getResultCode(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function runCacheImageResolve(string $image, string $filter, bool $forced): void |
106
|
|
|
{ |
107
|
|
|
if (!$this->outputMachineReadable) { |
108
|
|
|
$this->io->text(' - '); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->io->group($image, $filter, 'blue'); |
112
|
|
|
|
113
|
|
|
try { |
114
|
|
|
if ($forced || !$this->cacheManager->isStored($image, $filter)) { |
115
|
|
|
$this->cacheManager->store($this->filterManager->applyFilter($this->dataManager->find($filter, $image), $filter), $image, $filter); |
116
|
|
|
$this->io->status('resolved', 'green'); |
117
|
|
|
} else { |
118
|
|
|
$this->io->status('cached', 'white'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->io->line(sprintf(' %s', $this->cacheManager->resolve($image, $filter))); |
122
|
|
|
} catch (\Exception $e) { |
123
|
|
|
++$this->failures; |
124
|
|
|
|
125
|
|
|
$this->io->status('failed', 'red'); |
126
|
|
|
$this->io->line(' %s', [$e->getMessage()]); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.