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 Liip\ImagineBundle\Service\FilterPathContainer; |
18
|
|
|
use Symfony\Component\Console\Command\Command; |
19
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
21
|
|
|
use Symfony\Component\Console\Input\InputOption; |
22
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
23
|
|
|
|
24
|
|
|
class ResolveCacheCommand extends Command |
25
|
|
|
{ |
26
|
|
|
use CacheCommandTrait; |
27
|
|
|
|
28
|
|
|
protected static $defaultName = 'liip:imagine:cache:resolve'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var DataManager |
32
|
|
|
*/ |
33
|
|
|
private $dataManager; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
private $webpGenerate; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var mixed[] |
42
|
|
|
*/ |
43
|
|
|
private $webpOptions; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
DataManager $dataManager, |
47
|
|
|
CacheManager $cacheManager, |
48
|
|
|
FilterManager $filterManager, |
49
|
|
|
bool $webpGenerate, |
50
|
|
|
array $webpOptions |
51
|
|
|
) { |
52
|
|
|
parent::__construct(); |
53
|
|
|
|
54
|
|
|
$this->dataManager = $dataManager; |
55
|
|
|
$this->cacheManager = $cacheManager; |
56
|
|
|
$this->filterManager = $filterManager; |
57
|
|
|
$this->webpGenerate = $webpGenerate; |
58
|
|
|
$this->webpOptions = $webpOptions; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function configure(): void |
62
|
|
|
{ |
63
|
|
|
$this |
64
|
|
|
->setDescription('Warms up the cache for the specified image sources with all or specified filters applied, and prints the list of cache files.') |
65
|
|
|
->addArgument('paths', InputArgument::REQUIRED | InputArgument::IS_ARRAY, |
66
|
|
|
'Image file path(s) for which to generate the cached images.') |
67
|
|
|
->addOption('filter', 'f', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
68
|
|
|
'Filter(s) to use for image resolution; if none explicitly passed, use all filters.') |
69
|
|
|
->addOption('force', 'F', InputOption::VALUE_NONE, |
70
|
|
|
'Force generating the image and writing the cache, regardless of whether a cached version already exists.') |
71
|
|
|
->addOption('no-colors', 'C', InputOption::VALUE_NONE, |
72
|
|
|
'Write only un-styled text output; remove any colors, styling, etc.') |
73
|
|
|
->addOption('as-script', 'S', InputOption::VALUE_NONE, |
74
|
|
|
'Write only machine-readable output; silenced verbose reporting and implies --no-colors.') |
75
|
|
|
->setHelp(<<<'EOF' |
76
|
|
|
The <comment>%command.name%</comment> command resolves the passed image(s) for the resolved |
77
|
|
|
filter(s), outputting results using the following basic format: |
78
|
|
|
<info>image.ext[filter] (resolved|cached|failed): (resolve-image-path|exception-message)</> |
79
|
|
|
|
80
|
|
|
<comment># bin/console %command.name% --filter=thumb1 foo.ext bar.ext</comment> |
81
|
|
|
Resolve <options=bold>both</> <comment>foo.ext</comment> and <comment>bar.ext</comment> images using <options=bold>one</> filter (<comment>thumb1</comment>), outputting: |
82
|
|
|
<info>- foo.ext[thumb1] status: http://localhost/media/cache/thumb1/foo.ext</> |
83
|
|
|
<info>- bar.ext[thumb1] status: http://localhost/media/cache/thumb1/bar.ext</> |
84
|
|
|
|
85
|
|
|
<comment># bin/console %command.name% --filter=thumb1 --filter=thumb3 foo.ext</comment> |
86
|
|
|
Resolve <comment>foo.ext</comment> image using <options=bold>two</> filters (<comment>thumb1</comment> and <comment>thumb3</comment>), outputting: |
87
|
|
|
<info>- foo.ext[thumb1] status: http://localhost/media/cache/thumb1/foo.ext</> |
88
|
|
|
<info>- foo.ext[thumb3] status: http://localhost/media/cache/thumb3/foo.ext</> |
89
|
|
|
|
90
|
|
|
<comment># bin/console %command.name% foo.ext</comment> |
91
|
|
|
Resolve <comment>foo.ext</comment> image using <options=bold>all</> filters (as none were specified), outputting: |
92
|
|
|
<info>- foo.ext[thumb1] status: http://localhost/media/cache/thumb1/foo.ext</> |
93
|
|
|
<info>- foo.ext[thumb2] status: http://localhost/media/cache/thumb2/foo.ext</> |
94
|
|
|
<info>- foo.ext[thumb3] status: http://localhost/media/cache/thumb2/foo.ext</> |
95
|
|
|
|
96
|
|
|
<comment># bin/console %command.name% --force --filter=thumb1 foo.ext</comment> |
97
|
|
|
Resolve <comment>foo.ext</comment> image using <options=bold>one</> filter (<comment>thumb1</comment>) and <options=bold>forcing resolution</> (regardless of cache), outputting: |
98
|
|
|
<info>- foo.ext[thumb1] resolved: http://localhost/media/cache/thumb1/foo.ext</> |
99
|
|
|
|
100
|
|
|
EOF |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
105
|
|
|
{ |
106
|
|
|
$this->setupOutputStyle($input, $output); |
107
|
|
|
$this->outputCommandHeader(); |
108
|
|
|
|
109
|
|
|
$forced = $input->getOption('force'); |
110
|
|
|
[$images, $filters] = $this->resolveInputFiltersAndPaths($input); |
|
|
|
|
111
|
|
|
|
112
|
|
|
foreach ($images as $i) { |
113
|
|
|
foreach ($filters as $f) { |
114
|
|
|
$this->runCacheImageResolve($i, $f, $forced); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->outputCommandResult($images, $filters, 'resolution'); |
119
|
|
|
|
120
|
|
|
return $this->getResultCode(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function runCacheImageResolve(string $image, string $filter, bool $forced): void |
124
|
|
|
{ |
125
|
|
|
if (!$this->outputMachineReadable) { |
126
|
|
|
$this->io->text(' - '); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->io->group($image, $filter, 'blue'); |
130
|
|
|
|
131
|
|
|
try { |
132
|
|
|
$basePathContainer = new FilterPathContainer($image); |
133
|
|
|
$filterPathContainers = [$basePathContainer]; |
134
|
|
|
|
135
|
|
|
if ($this->webpGenerate) { |
136
|
|
|
$filterPathContainers[] = $basePathContainer->createWebp($this->webpOptions); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
foreach ($filterPathContainers as $filterPathContainer) { |
140
|
|
|
if ($forced || !$this->cacheManager->isStored($filterPathContainer->getTarget(), $filter)) { |
141
|
|
|
$binary = $this->dataManager->find($filter, $filterPathContainer->getSource()); |
142
|
|
|
$filteredBinary = $this->filterManager->applyFilter($binary, $filter, $filterPathContainer->getOptions()); |
143
|
|
|
$this->cacheManager->store($filteredBinary, $filterPathContainer->getTarget(), $filter); |
144
|
|
|
|
145
|
|
|
$this->io->status('resolved', 'green'); |
146
|
|
|
} else { |
147
|
|
|
$this->io->status('cached', 'white'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->io->line(sprintf(' %s', $this->cacheManager->resolve($filterPathContainer->getTarget(), $filter))); |
151
|
|
|
} |
152
|
|
|
} catch (\Exception $e) { |
153
|
|
|
++$this->failures; |
154
|
|
|
|
155
|
|
|
$this->io->status('failed', 'red'); |
156
|
|
|
$this->io->line(' %s', [$e->getMessage()]); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
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.