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