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\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
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 ContainerAwareCommand |
24
|
|
|
{ |
25
|
|
|
protected function configure() |
26
|
|
|
{ |
27
|
|
|
$this |
28
|
|
|
->setName('liip:imagine:cache:resolve') |
29
|
|
|
->setDescription('Resolve cache for given path and set of filters.') |
30
|
|
|
->setDefinition(array( |
31
|
|
|
new InputArgument('paths', InputArgument::REQUIRED | InputArgument::IS_ARRAY, |
32
|
|
|
'Any number of image paths to act on.'), |
33
|
|
|
new InputOption('filters', ['f'], InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, |
34
|
|
|
'List of filters to apply to passed images.'), |
35
|
|
|
new InputOption('force', ['F'], InputOption::VALUE_NONE, |
36
|
|
|
'Force image resolution regardless of cache.'), |
37
|
|
|
new InputOption('machine', ['m'], InputOption::VALUE_NONE, |
38
|
|
|
'Only print machine-parseable results.'), |
39
|
|
|
)) |
40
|
|
|
->setHelp(<<<'EOF' |
41
|
|
|
The <comment>%command.name%</comment> command resolves the passed image(s) for the resolved |
42
|
|
|
filter(s), outputting results using the following basic format: |
43
|
|
|
<info>- "image.ext[filter]" (resolved|cached) as "path/to/cached/image.ext"</> |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
<comment># bin/console %command.name% --filters=thumb1 foo.ext bar.ext</comment> |
47
|
|
|
Resolve <options=bold>both</> <comment>foo.ext</comment> and <comment>bar.ext</comment> using <comment>thumb1</comment> filter, outputting: |
48
|
|
|
<info>- "foo.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/foo.ext"</> |
49
|
|
|
<info>- "bar.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/bar.ext"</> |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
<comment># bin/console %command.name% --filters=thumb1 --filters=thumb2 foo.ext</comment> |
53
|
|
|
Resolve <comment>foo.ext</comment> using <options=bold>both</> <comment>thumb1</comment> and <comment>thumb2</comment> filters, outputting: |
54
|
|
|
<info>- "foo.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/foo.ext"</> |
55
|
|
|
<info>- "foo.ext[thumb2]" resolved as "http://localhost/media/cache/thumb2/foo.ext"</> |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
<comment># bin/console %command.name% foo.ext</comment> |
59
|
|
|
Resolve <comment>foo.ext</comment> using <options=bold>all configured filters</> (as none are specified), outputting: |
60
|
|
|
<info>- "foo.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/foo.ext"</> |
61
|
|
|
<info>- "foo.ext[thumb2]" resolved as "http://localhost/media/cache/thumb2/foo.ext"</> |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
<comment># bin/console %command.name% --force --filters=thumb1 foo.ext</comment> |
65
|
|
|
Resolve <comment>foo.ext</comment> using <comment>thumb1</comment> and <options=bold>force creation</> regardless of cache, outputting: |
66
|
|
|
<info>- "foo.ext[thumb1]" resolved as "http://localhost/media/cache/thumb1/foo.ext"</> |
67
|
|
|
|
68
|
|
|
EOF |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
73
|
|
|
{ |
74
|
|
|
$imagePaths = $input->getArgument('paths'); |
75
|
|
|
$useFilters = $input->getOption('filters'); |
76
|
|
|
$forced = $input->getOption('force'); |
77
|
|
|
$machine = $input->getOption('machine'); |
78
|
|
|
$failures = 0; |
79
|
|
|
|
80
|
|
|
$filterManager = $this->getFilterManager(); |
81
|
|
|
$dataManager = $this->getDataManager(); |
82
|
|
|
$cacheManager = $this->getCacheManager(); |
83
|
|
|
|
84
|
|
|
if (0 === count($useFilters)) { |
85
|
|
|
$useFilters = array_keys($filterManager->getFilterConfiguration()->all()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!$machine) { |
89
|
|
|
$this->outputTitle($output); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
foreach ($imagePaths as $path) { |
93
|
|
|
foreach ($useFilters as $filter) { |
94
|
|
|
$output->write(sprintf('- "%s[%s]" ', $path, $filter)); |
95
|
|
|
|
96
|
|
|
try { |
97
|
|
|
if ($forced || !$cacheManager->isStored($path, $filter)) { |
98
|
|
|
$cacheManager->store($filterManager->applyFilter($dataManager->find($filter, $path), $filter), $path, $filter); |
99
|
|
|
$output->write('RESOLVED '); |
100
|
|
|
} else { |
101
|
|
|
$output->write('CACHED '); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$output->writeln(sprintf('as "%s"', $cacheManager->resolve($path, $filter))); |
105
|
|
|
} catch (\Exception $e) { |
106
|
|
|
$output->writeln(sprintf('FAILED with exception "%s"', $e->getMessage())); |
107
|
|
|
++$failures; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (!$machine) { |
113
|
|
|
$this->outputSummary($output, $useFilters, $imagePaths, $failures); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return 0 === $failures ? 0 : 255; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param OutputInterface $output |
121
|
|
|
*/ |
122
|
|
|
private function outputTitle(OutputInterface $output) |
123
|
|
|
{ |
124
|
|
|
$title = 'Resolving Imagine Bundle Images'; |
125
|
|
|
$output->writeln(sprintf('<info>%s</info>', $title)); |
126
|
|
|
$output->writeln(str_repeat('=', strlen($title))); |
127
|
|
|
$output->writeln(''); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param OutputInterface $output |
132
|
|
|
* @param string[] $useFilters |
133
|
|
|
* @param string[] $imagePaths |
134
|
|
|
* @param int $failures |
135
|
|
|
*/ |
136
|
|
|
private function outputSummary(OutputInterface $output, $useFilters, $imagePaths, $failures) |
137
|
|
|
{ |
138
|
|
|
$useFiltersCount = count($useFilters); |
139
|
|
|
$imagePathsCount = count($imagePaths); |
140
|
|
|
$totalActionStep = $useFiltersCount * $imagePathsCount; |
141
|
|
|
|
142
|
|
|
$output->writeln(''); |
143
|
|
|
$output->writeln(vsprintf('Completed %d %s (%d %s on %d %s) %s', [ |
144
|
|
|
$totalActionStep, |
145
|
|
|
1 === $totalActionStep ? 'operation' : 'operations', |
146
|
|
|
count($useFilters), |
147
|
|
|
1 === $useFiltersCount ? 'filter' : 'filters', |
148
|
|
|
count($imagePaths), |
149
|
|
|
1 === $totalActionStep ? 'image' : 'images', |
150
|
|
|
0 === $failures ? '' : sprintf('<fg=red>[encountered</> <fg=red;options=bold>%d</> <fg=red> failures]</>', $failures) |
151
|
|
|
])); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return FilterManager |
156
|
|
|
*/ |
157
|
|
|
private function getFilterManager() |
158
|
|
|
{ |
159
|
|
|
return $this->getContainer()->get('liip_imagine.filter.manager'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return DataManager |
164
|
|
|
*/ |
165
|
|
|
private function getDataManager() |
166
|
|
|
{ |
167
|
|
|
return $this->getContainer()->get('liip_imagine.data.manager'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return CacheManager |
172
|
|
|
*/ |
173
|
|
|
private function getCacheManager() |
174
|
|
|
{ |
175
|
|
|
return $this->getContainer()->get('liip_imagine.cache.manager'); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|