|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Ghostscript package |
|
4
|
|
|
* |
|
5
|
|
|
* @author Daniel Schröder <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace GravityMedia\Ghostscript\Device; |
|
9
|
|
|
|
|
10
|
|
|
use GravityMedia\Ghostscript\Device\CommandLineParameters\EpsTrait; |
|
11
|
|
|
use GravityMedia\Ghostscript\Device\CommandLineParameters\FontTrait; |
|
12
|
|
|
use GravityMedia\Ghostscript\Device\CommandLineParameters\IccColorTrait; |
|
13
|
|
|
use GravityMedia\Ghostscript\Device\CommandLineParameters\InteractionTrait; |
|
14
|
|
|
use GravityMedia\Ghostscript\Device\CommandLineParameters\OtherTrait; |
|
15
|
|
|
use GravityMedia\Ghostscript\Device\CommandLineParameters\OutputSelectionTrait; |
|
16
|
|
|
use GravityMedia\Ghostscript\Device\CommandLineParameters\PageTrait; |
|
17
|
|
|
use GravityMedia\Ghostscript\Device\CommandLineParameters\RenderingTrait; |
|
18
|
|
|
use GravityMedia\Ghostscript\Device\CommandLineParameters\ResourceTrait; |
|
19
|
|
|
use GravityMedia\Ghostscript\Ghostscript; |
|
20
|
|
|
use GravityMedia\Ghostscript\Input; |
|
21
|
|
|
use GravityMedia\Ghostscript\Process\Argument; |
|
22
|
|
|
use GravityMedia\Ghostscript\Process\Arguments; |
|
23
|
|
|
use Symfony\Component\Process\Process; |
|
24
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The abstract device class |
|
28
|
|
|
* |
|
29
|
|
|
* @package GravityMedia\Ghostscript\Devices |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class AbstractDevice |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Use command line options |
|
35
|
|
|
*/ |
|
36
|
|
|
use CommandLineParametersTrait; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Use rendering parameters |
|
40
|
|
|
*/ |
|
41
|
|
|
use RenderingTrait; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Use page parameters |
|
45
|
|
|
*/ |
|
46
|
|
|
use PageTrait; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Use font-related parameters |
|
50
|
|
|
*/ |
|
51
|
|
|
use FontTrait; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Use resource-related parameters |
|
55
|
|
|
*/ |
|
56
|
|
|
use ResourceTrait; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Use interaction parameters |
|
60
|
|
|
*/ |
|
61
|
|
|
use InteractionTrait; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Use device and output selection parameters |
|
65
|
|
|
*/ |
|
66
|
|
|
use OutputSelectionTrait; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Use EPS parameters |
|
70
|
|
|
*/ |
|
71
|
|
|
use EpsTrait; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Use ICC color parameters |
|
75
|
|
|
*/ |
|
76
|
|
|
use IccColorTrait; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Use other parameters |
|
80
|
|
|
*/ |
|
81
|
|
|
use OtherTrait; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* The Ghostscript object |
|
85
|
|
|
* |
|
86
|
|
|
* @var Ghostscript |
|
87
|
|
|
*/ |
|
88
|
|
|
private $ghostscript; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* The arguments object |
|
92
|
|
|
* |
|
93
|
|
|
* @var Arguments |
|
94
|
|
|
*/ |
|
95
|
|
|
private $arguments; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Create abstract device object |
|
99
|
|
|
* |
|
100
|
|
|
* @param Ghostscript $ghostscript |
|
101
|
|
|
* @param Arguments $arguments |
|
102
|
|
|
*/ |
|
103
|
20 |
|
public function __construct(Ghostscript $ghostscript, Arguments $arguments) |
|
104
|
|
|
{ |
|
105
|
20 |
|
$this->ghostscript = $ghostscript; |
|
106
|
20 |
|
$this->arguments = $arguments; |
|
107
|
20 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get Argument |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $name |
|
113
|
|
|
* |
|
114
|
|
|
* @return null|Argument |
|
115
|
|
|
*/ |
|
116
|
6 |
|
protected function getArgument($name) |
|
117
|
|
|
{ |
|
118
|
6 |
|
return $this->arguments->getArgument($name); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Whether argument is set |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $name |
|
125
|
|
|
* |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
2 |
|
protected function hasArgument($name) |
|
129
|
|
|
{ |
|
130
|
2 |
|
return null !== $this->getArgument($name); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get argument value |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $name |
|
137
|
|
|
* |
|
138
|
|
|
* @return null|string |
|
139
|
|
|
*/ |
|
140
|
2 |
|
protected function getArgumentValue($name) |
|
141
|
|
|
{ |
|
142
|
2 |
|
$argument = $this->getArgument($name); |
|
143
|
2 |
|
if (null === $argument) { |
|
144
|
2 |
|
return null; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
2 |
|
return $argument->getValue(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Set argument |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $argument |
|
154
|
|
|
* |
|
155
|
|
|
* @return $this |
|
156
|
|
|
*/ |
|
157
|
2 |
|
protected function setArgument($argument) |
|
158
|
|
|
{ |
|
159
|
2 |
|
$this->arguments->setArgument($argument); |
|
160
|
|
|
|
|
161
|
2 |
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Sanitize input. |
|
166
|
|
|
* |
|
167
|
|
|
* @param mixed $input |
|
168
|
|
|
* |
|
169
|
|
|
* @return Input |
|
170
|
|
|
*/ |
|
171
|
12 |
|
protected function sanitizeInput($input) |
|
172
|
|
|
{ |
|
173
|
12 |
|
if (null === $input) { |
|
174
|
2 |
|
$input = $this->ghostscript->getOption('input', new Input()); |
|
175
|
1 |
|
} |
|
176
|
|
|
|
|
177
|
12 |
|
if ($input instanceof Input) { |
|
178
|
6 |
|
return $input; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
6 |
|
$instance = new Input(); |
|
182
|
|
|
|
|
183
|
6 |
|
if (is_resource($input)) { |
|
184
|
2 |
|
return $instance->setProcessInput($input); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
4 |
|
if (file_exists($input)) { |
|
188
|
2 |
|
return $instance->addFile($input); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
2 |
|
return $instance->setPostScriptCode((string)$input); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Create process object |
|
196
|
|
|
* |
|
197
|
|
|
* @param mixed $input |
|
198
|
|
|
* |
|
199
|
|
|
* @throws \RuntimeException |
|
200
|
|
|
* |
|
201
|
|
|
* @return Process |
|
202
|
|
|
*/ |
|
203
|
12 |
|
public function createProcess($input = null) |
|
204
|
|
|
{ |
|
205
|
12 |
|
$input = $this->sanitizeInput($input); |
|
206
|
12 |
|
$arguments = array_values($this->arguments->toArray()); |
|
207
|
|
|
|
|
208
|
12 |
|
if (null !== $input->getPostScriptCode()) { |
|
209
|
4 |
|
array_push($arguments, '-c', $input->getPostScriptCode()); |
|
210
|
2 |
|
} |
|
211
|
|
|
|
|
212
|
12 |
|
if (count($input->getFiles()) > 0) { |
|
213
|
6 |
|
array_push($arguments, '-f'); |
|
214
|
6 |
|
foreach ($input->getFiles() as $file) { |
|
215
|
6 |
|
if (!is_file($file)) { |
|
216
|
2 |
|
throw new \RuntimeException('Input file does not exist'); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
4 |
|
array_push($arguments, $file); |
|
220
|
2 |
|
} |
|
221
|
2 |
|
} |
|
222
|
|
|
|
|
223
|
10 |
|
if (null !== $input->getProcessInput()) { |
|
224
|
4 |
|
array_push($arguments, '-'); |
|
225
|
2 |
|
} |
|
226
|
|
|
|
|
227
|
10 |
|
$processBuilder = new ProcessBuilder($arguments); |
|
228
|
10 |
|
$processBuilder->setPrefix($this->ghostscript->getOption('bin', Ghostscript::DEFAULT_BINARY)); |
|
229
|
10 |
|
$processBuilder->addEnvironmentVariables($this->ghostscript->getOption('env', [])); |
|
230
|
10 |
|
$processBuilder->setTimeout($this->ghostscript->getOption('timeout', 60)); |
|
231
|
10 |
|
$processBuilder->setInput($input->getProcessInput()); |
|
232
|
|
|
|
|
233
|
10 |
|
return $processBuilder->getProcess(); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|