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 Imagine\Exception\RuntimeException; |
15
|
|
|
use Liip\ImagineBundle\Component\Console\Style\ImagineStyle; |
16
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
17
|
|
|
use Liip\ImagineBundle\Imagine\Filter\FilterManager; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @internal |
23
|
|
|
*/ |
24
|
|
|
trait CacheCommandTrait |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var CacheManager |
28
|
|
|
*/ |
29
|
|
|
private $cacheManager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var FilterManager |
33
|
|
|
*/ |
34
|
|
|
private $filterManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ImagineStyle |
38
|
|
|
*/ |
39
|
|
|
private $io; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
private $outputMachineReadable; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
private $failures = 0; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param InputInterface $input |
53
|
|
|
* @param OutputInterface $output |
54
|
|
|
*/ |
55
|
|
|
private function setupOutputStyle(InputInterface $input, OutputInterface $output): void |
56
|
|
|
{ |
57
|
|
|
$this->outputMachineReadable = $input->getOption('as-script'); |
58
|
|
|
$this->io = new ImagineStyle($input, $output, $this->outputMachineReadable ? false : !$input->getOption('no-colors')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param InputInterface $input |
63
|
|
|
* |
64
|
|
|
* @return array[] |
65
|
|
|
*/ |
66
|
|
|
private function resolveInputFiltersAndPaths(InputInterface $input): array |
67
|
|
|
{ |
68
|
|
|
return [ |
69
|
|
|
$input->getArgument('path'), |
70
|
|
|
$this->normalizeFilterList($input->getOption('filter')), |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string[] $filters |
76
|
|
|
* |
77
|
|
|
* @return string[] |
78
|
|
|
*/ |
79
|
|
|
private function normalizeFilterList(array $filters): array |
80
|
|
|
{ |
81
|
|
|
if (0 < count($filters)) { |
82
|
|
|
return $filters; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (0 < count($filters = array_keys((array) $this->filterManager->getFilterConfiguration()->all()))) { |
86
|
|
|
return $filters; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
throw new RuntimeException('No filters have been defined in the active configuration!'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function outputCommandHeader(): void |
93
|
|
|
{ |
94
|
|
|
if (!$this->outputMachineReadable) { |
95
|
|
|
$this->io->title($this->getName(), 'liip/imagine-bundle'); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string[] $images |
101
|
|
|
* @param string[] $filters |
102
|
|
|
*/ |
103
|
|
|
private function outputCommandResult(array $images, array $filters): void |
104
|
|
|
{ |
105
|
|
|
if (!$this->outputMachineReadable) { |
106
|
|
|
$wordPluralizer = function (int $count, string $singular) { |
107
|
|
|
return 1 === $count ? $singular : sprintf('%ss', $singular); |
108
|
|
|
}; |
109
|
|
|
$rootTextFormat = 'Completed %d %s (%d %s, %d %s)'; |
110
|
|
|
$imagePathsSize = count($images); |
111
|
|
|
$filterSetsSize = count($filters); |
112
|
|
|
$allActionsSize = ($filterSetsSize * $imagePathsSize) - $this->failures; |
113
|
|
|
$allActionsWord = $wordPluralizer($allActionsSize, 'operation'); |
114
|
|
|
|
115
|
|
|
switch (substr(__CLASS__, -18, 6)) { |
116
|
|
|
case 'Remove': |
117
|
|
|
$allActionsWord = $wordPluralizer($allActionsSize, 'removal'); |
118
|
|
|
break; |
119
|
|
|
|
120
|
|
|
case 'esolve': |
121
|
|
|
$allActionsWord = $wordPluralizer($allActionsSize, 'resolution'); |
122
|
|
|
break; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$rootTextOutput = vsprintf($rootTextFormat, [ |
126
|
|
|
$allActionsSize, |
127
|
|
|
$allActionsWord, |
128
|
|
|
$imagePathsSize, |
129
|
|
|
$wordPluralizer($imagePathsSize, 'image'), |
130
|
|
|
$filterSetsSize, |
131
|
|
|
$wordPluralizer($filterSetsSize, 'filter'), |
132
|
|
|
]); |
133
|
|
|
|
134
|
|
|
if ($this->failures) { |
135
|
|
|
$this->io->critBlock(sprintf('%s %%s', $rootTextOutput), [ |
136
|
|
|
sprintf('[encountered %d failures]', $this->failures) |
137
|
|
|
]); |
138
|
|
|
} else { |
139
|
|
|
$this->io->okayBlock($rootTextOutput); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return int |
146
|
|
|
*/ |
147
|
|
|
private function getResultCode(): int |
148
|
|
|
{ |
149
|
|
|
return 0 === $this->failures ? 0 : 255; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.