-
Notifications
You must be signed in to change notification settings - Fork 380
/
AbstractPostProcessor.php
253 lines (219 loc) · 7.57 KB
/
AbstractPostProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Liip\ImagineBundle\Imagine\Filter\PostProcessor;
use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Binary\FileBinaryInterface;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;
abstract class AbstractPostProcessor implements PostProcessorInterface, ConfigurablePostProcessorInterface
{
/**
* @var string
*/
protected $executablePath;
/**
* @var string|null
*/
protected $temporaryRootPath;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @param string $executablePath
* @param string|null $temporaryRootPath
*/
public function __construct($executablePath, $temporaryRootPath = null)
{
$this->executablePath = $executablePath;
$this->temporaryRootPath = $temporaryRootPath;
$this->filesystem = new Filesystem();
}
/**
* Performs post-process operation on passed binary and returns the resulting binary.
*
* @param BinaryInterface $binary
* @param array $options
*
* @throws ProcessFailedException
*
* @return BinaryInterface
*/
public function process(BinaryInterface $binary /* , array $options = array() */)
{
if (func_num_args() < 2) {
@trigger_error(sprintf(
'Calling the %s::%s() method without a second parameter of options was deprecated in 1.10.0 and '.
'will be removed in 2.0.', get_called_class(), __FUNCTION__
), E_USER_DEPRECATED);
}
return $this->doProcess($binary, func_num_args() >= 2 ? func_get_arg(1) : array());
}
/**
* Performs post-process operation on passed binary and returns the resulting binary.
*
* @deprecated This method was deprecated in 1.10.0 and will be removed in 2.0. Use PostProcessorInterface::process()
* instead.
*
* @param BinaryInterface $binary
* @param array $options
*
* @throws ProcessFailedException
*
* @return BinaryInterface
*/
public function processWithConfiguration(BinaryInterface $binary, array $options)
{
@trigger_error(sprintf(
'The %s::%s() method was deprecated in 1.10.0 and will be removed in 2.0. Use the %s::process() '.
'method instead.', get_called_class(), __FUNCTION__, get_called_class()
), E_USER_DEPRECATED);
return $this->doProcess($binary, $options);
}
/**
* @param BinaryInterface $binary
* @param array $options
*
* @throws ProcessFailedException
*
* @return BinaryInterface
*/
abstract protected function doProcess(BinaryInterface $binary, array $options);
/**
* @param array $arguments
* @param array $options
*
* @return ProcessBuilder
*/
protected function createProcessBuilder(array $arguments = array(), array $options = array())
{
$builder = new ProcessBuilder($arguments);
if (!isset($options['process'])) {
return $builder;
}
if (isset($options['process']['timeout'])) {
$builder->setTimeout($options['process']['timeout']);
}
if (isset($options['process']['prefix'])) {
$builder->setPrefix($options['process']['prefix']);
}
if (isset($options['process']['working_directory'])) {
$builder->setWorkingDirectory($options['process']['working_directory']);
}
if (isset($options['process']['environment_variables']) && is_array($options['process']['environment_variables'])) {
foreach ($options['process']['environment_variables'] as $n => $v) {
$builder->setEnv($n, $v);
}
}
if (isset($options['process']['options']) && is_array($options['process']['options'])) {
foreach ($options['process']['options'] as $n => $v) {
$builder->setOption($n, $v);
}
}
return $builder;
}
/**
* @param BinaryInterface $binary
*
* @return bool
*/
protected function isBinaryTypeJpgImage(BinaryInterface $binary)
{
return $this->isBinaryTypeMatch($binary, array('image/jpeg', 'image/jpg'));
}
/**
* @param BinaryInterface $binary
*
* @return bool
*/
protected function isBinaryTypePngImage(BinaryInterface $binary)
{
return $this->isBinaryTypeMatch($binary, array('image/png'));
}
/**
* @param BinaryInterface $binary
* @param string[] $types
*
* @return bool
*/
protected function isBinaryTypeMatch(BinaryInterface $binary, array $types)
{
return in_array($binary->getMimeType(), $types);
}
/**
* @param BinaryInterface $binary
* @param array $options
* @param null $prefix
*
* @return string
*/
protected function writeTemporaryFile(BinaryInterface $binary, array $options = array(), $prefix = null)
{
$temporary = $this->acquireTemporaryFilePath($options, $prefix);
if ($binary instanceof FileBinaryInterface) {
$this->filesystem->copy($binary->getPath(), $temporary, true);
} else {
$this->filesystem->dumpFile($temporary, $binary->getContent());
}
return $temporary;
}
/**
* @param array $options
* @param string $prefix
*
* @return string
*/
protected function acquireTemporaryFilePath(array $options, $prefix = null)
{
$root = isset($options['temp_dir']) ? $options['temp_dir'] : ($this->temporaryRootPath ?: sys_get_temp_dir());
if (!is_dir($root)) {
try {
$this->filesystem->mkdir($root);
} catch (IOException $exception) {
// ignore failure as "tempnam" function will revert back to system default tmp path as last resort
}
}
if (false === $file = @tempnam($root, $prefix ?: 'post-processor')) {
throw new \RuntimeException(sprintf('Temporary file cannot be created in "%s"', $root));
}
return $file;
}
/**
* @param Process $process
* @param array $validReturns
* @param array $errorStrings
*
* @return bool
*/
protected function isSuccessfulProcess(Process $process, array $validReturns = array(0), array $errorStrings = array('ERROR'))
{
if (count($validReturns) > 0 && !in_array($process->getExitCode(), $validReturns)) {
return false;
}
foreach ($errorStrings as $string) {
if (false !== strpos($process->getOutput(), $string)) {
return false;
}
}
return true;
}
/**
* @param string $method
*/
protected function triggerSetterMethodDeprecation($method)
{
@trigger_error(sprintf('The %s() method was deprecated in 1.10.0 and will be removed in 2.0. You must '
.'setup the class state via its __construct() method. You can still pass filter-specific options to the '.
'process() method to overwrite behavior.', $method), E_USER_DEPRECATED);
}
}