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 Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
class RemoveCacheCommand extends AbstractCacheCommand |
20
|
|
|
{ |
21
|
|
|
protected function configure() |
22
|
|
|
{ |
23
|
|
|
$this |
24
|
|
|
->setName('liip:imagine:cache:remove') |
25
|
|
|
->setDescription('Remove asset caches for the passed asset paths(s) and filter name(s)') |
26
|
|
|
->addArgument('paths', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
27
|
|
|
'Asset paths to remove caches of (passing none will use all paths).') |
28
|
|
|
->addOption('filter', 'f', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, |
29
|
|
|
'Filter name to remove caches of (passing none will use all registered filters)') |
30
|
|
|
->addOption('machine-readable', 'm', InputOption::VALUE_NONE, |
31
|
|
|
'Enable machine parseable output (removing extraneous output and all text styles)') |
32
|
|
|
->addOption('filters', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, |
33
|
|
|
'Deprecated in 1.9.0 and removed in 2.0.0 (use the --filter option instead)') |
34
|
|
|
->setHelp(<<<'EOF' |
35
|
|
|
The <comment>%command.name%</comment> command removes asset cache for the passed image(s) and filter(s). |
36
|
|
|
|
37
|
|
|
For an application that has only the two files "foo.ext" and "bar.ext", and only the filter sets |
38
|
|
|
named "thumb_sm" and "thumb_lg", the following examples will behave as shown. |
39
|
|
|
|
40
|
|
|
<comment># bin/console %command.name% foo.ext</comment> |
41
|
|
|
Removes caches for <options=bold>foo.ext</> using <options=bold>all configured filters</>, outputting: |
42
|
|
|
<info>- foo.ext[thumb_sm] removed</> |
43
|
|
|
<info>- foo.ext[thumb_lg] removed</> |
44
|
|
|
|
45
|
|
|
<comment># bin/console %command.name% --filter=thumb_sm --filter=thumb_lg foo.ext bar.ext</comment> |
46
|
|
|
Removes caches for both <options=bold>foo.ext</> and <options=bold>bar.ext</> using <options=bold>thumb_sm</> and <options=bold>thumb_lg</> filters, outputting: |
47
|
|
|
<info>- foo.ext[thumb_sm] removed</> |
48
|
|
|
<info>- foo.ext[thumb_lg] removed</> |
49
|
|
|
<info>- bar.ext[thumb_sm] removed</> |
50
|
|
|
<info>- bar.ext[thumb_lg] removed</> |
51
|
|
|
|
52
|
|
|
<comment># bin/console %command.name% --filter=thumb_sm</comment> |
53
|
|
|
Removes <options=bold>all caches</> for <options=bold>thumb_sm</> filter, outputting: |
54
|
|
|
<info>- *[thumb_sm] glob-removal</> |
55
|
|
|
|
56
|
|
|
<comment># bin/console %command.name%</comment> |
57
|
|
|
Removes <options=bold>all caches</> for <options=bold>all configured filters</>, removing all cached assets, outputting: |
58
|
|
|
<info>- *[thumb_sm] glob-removal</> |
59
|
|
|
<info>- *[thumb_lg] glob-removal</> |
60
|
|
|
|
61
|
|
|
<comment># bin/console %command.name% --force --filter=thumb_sm foo.ext</comment> |
62
|
|
|
Removing caches for <options=bold>foo.ext</> using <options=bold>thumb_sm</> filter when <options=bold>already removed</> will caused <options=bold>skipping</>, outputting: |
63
|
|
|
<info>- foo.ext[thumb_sm] skipped</> |
64
|
|
|
|
65
|
|
|
<comment># bin/console %command.name% --filter=does_not_exist --filter=thumb_sm foo.ext</comment> |
66
|
|
|
Removes caches for <options=bold>foo.ext</> for <options=bold>thumb_sm</> while <options=bold>failing inline</> on invalid filter (or other errors), outputting: |
67
|
|
|
<info>- foo.ext[does_not_exist] failure: Could not find configuration for a filter: does_not_exist</> |
68
|
|
|
<info>- foo.ext[thumb_sm] removed</> |
69
|
|
|
|
70
|
|
|
EOF |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
75
|
|
|
{ |
76
|
|
|
$this->initializeInstState($input, $output); |
77
|
|
|
$this->writeCommandHeading('remove'); |
78
|
|
|
|
79
|
|
|
$filters = $this->resolveFilters($input); |
80
|
|
|
$targets = $input->getArgument('paths'); |
81
|
|
|
|
82
|
|
|
if (0 === count($targets)) { |
83
|
|
|
$this->doCacheRemoveAsGlobbedFilterName($filters); |
84
|
|
|
} else { |
85
|
|
|
$this->doCacheRemoveAsFiltersAndTargets($filters, $targets); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->getReturnCode(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string[] $filters |
93
|
|
|
*/ |
94
|
|
|
private function doCacheRemoveAsGlobbedFilterName(array $filters) |
95
|
|
|
{ |
96
|
|
|
foreach ($filters as $f) { |
97
|
|
|
$this->doCacheRemove($f); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->writeResultSummary($filters, array(), true); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string[] $filters |
105
|
|
|
* @param string[] $targets |
106
|
|
|
*/ |
107
|
|
|
private function doCacheRemoveAsFiltersAndTargets(array $filters, array $targets) |
108
|
|
|
{ |
109
|
|
|
foreach ($targets as $t) { |
110
|
|
|
foreach ($filters as $f) { |
111
|
|
|
$this->doCacheRemove($f, $t); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->writeResultSummary($filters, $targets); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $filter |
120
|
|
|
* @param string|null $target |
121
|
|
|
*/ |
122
|
|
|
private function doCacheRemove($filter, $target = null) |
123
|
|
|
{ |
124
|
|
|
$this->writeActionStart($filter, $target); |
125
|
|
|
|
126
|
|
|
try { |
127
|
|
|
if (null === $target) { |
128
|
|
|
$this->getCacheManager()->remove(null, $filter); |
129
|
|
|
$this->writeActionResult('glob-removal', false); |
130
|
|
|
} elseif ($this->getCacheManager()->isStored($target, $filter)) { |
131
|
|
|
$this->getCacheManager()->remove($target, $filter); |
132
|
|
|
$this->writeActionResult('removed', false); |
133
|
|
|
} else { |
134
|
|
|
$this->writeActionResult('skipped', false); |
135
|
|
|
} |
136
|
|
|
} catch (\Exception $e) { |
137
|
|
|
$this->writeActionException($e); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|