-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAdapter.php
348 lines (308 loc) · 8.26 KB
/
Adapter.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
namespace AC\Transcoding;
use AC\Transcoding\Event\MessageEvent;
use AC\Transcoding\Event\TranscodeEvents;
/**
* This is the base adapter class all other should extend.
*
* @package Transcoding
* @author Evan Villemez
*/
abstract class Adapter
{
/**
* Machine key for this adapter. Should be lower cased with underscores.
*
* @var string
*/
protected $key = false;
/**
* Human readable name of adapter.
*
* @var string
*/
protected $name = false;
/**
* Human-readable description of adapter
*
* @var string
*/
protected $description = false;
/**
* Instance of FileDefinitionHandler used to restrict input formats.
*
* @var \AC\Transcoding\FileDefinitionHandler
*/
protected $inputDefinition = false;
/**
* Instance of FileDefinitionHandler used to define allowed output formats.
*
* @var \AC\Transcoding\FileDefinitionHandler
*/
protected $outputDefinition = false;
/**
* Boolean whether or not the Adapter has been verified - determined automatically in Adapter::verify()
*
* @var boolean
*/
private $verified = null;
/**
* Error string from exception caught during Adapter::verify()
*
* @var string
*/
private $verificationError = false;
/**
* Private instance of Transcoder for sending messages
*
* @var Transcoder
*/
private $transcoder;
/**
* Return a custom FileHandlerDefinition for input restriction.
*
* @return AC\Transcoding\FileHandlerDefinition
*/
protected function buildInputDefinition()
{
return new FileHandlerDefinition;
}
/**
* Return a custom FileHandlerDefinition for output restriction.
*
* @return AC\Transcoding\FileHandlerDefinition
*/
protected function buildOutputDefinition()
{
return new FileHandlerDefinition;
}
/**
* Get the input FileHandlerDefinition
*
* @return AC\Transcoding\FileHandlerDefinition
*/
public function getInputDefinition()
{
if (!$this->inputDefinition) {
$this->inputDefinition = $this->buildInputDefinition();
}
return $this->inputDefinition;
}
/**
* Get the output FileHandlerDefinition
*
* @return AC\Transcoding\FileHandlerDefinition
*/
public function getOutputDefinition()
{
if (!$this->outputDefinition) {
$this->outputDefinition = $this->buildOutputDefinition();
}
return $this->outputDefinition;
}
/**
* Transcodes a file, given a preset and an output path.
*
* @param File $file - instance of AC\Transcoding\File
* @param Preset $preset - instance of AC\Transcoding\Preset
* @param string $outFilePath - string output file path (must be valid)
* @return AC\Transcoding\File on success, exception thrown otherwise
*/
public function transcodeFile(File $file, Preset $preset, $outFilePath)
{
throw new \RuntimeException("Adapter::transcodeFile must be implemented by an extending class.");
}
/**
* Gets called by the Transcoder when a transcode process fails.
*
* @param string $outFilePath
*/
public function cleanFailedTranscode($outFilePath)
{
return;
}
/**
* Implement custom Preset validation logic here - this is called by the Transcoder before any transcode process runs.
*
* @param Preset $preset
* @return boolean true on success, throw exception otherwise.
*/
public function validatePreset(Preset $preset)
{
return true;
}
/**
* Uses input FileHandlerDefinition to validate input file.
*
* @param File $file
* @return true on success, throws exception otherwise.
*/
public function validateInputFile(File $file)
{
$this->getInputDefinition()->validateFile($file);
return true;
}
/**
* Uses output FileHandlerDefinition to validate output file.
*
* @param File $file
* @return true on success, throws exception otherwise.
*/
public function validateOutputFile(File $file)
{
$this->getOutputDefinition()->validateFile($file);
return true;
}
/**
* Returns true/false if given file is acceptible input
*
* @param File $file
* @return true on success, false otherwise
*/
public function acceptsInputFile(File $file)
{
return $this->getInputDefinition()->acceptsFile($file);
}
/**
* Returns true/false if given file is acceptible output
*
* @param File $file
* @return true on success, false otherwise
*/
public function acceptsOutputFile(File $file)
{
return $this->getOutputDefinition()->acceptsFile($file);
}
/**
* Called by the Transcoder to make the Adapter verify it's current status in the environment.
*
* @return true on success, false otherwise
*/
public function verify()
{
if (is_null($this->verified)) {
try {
$this->verified = (bool) $this->verifyEnvironment();
if (!$this->verified) {
throw new Exception\EnvironmentException("The adapter did not properly verify.");
}
} catch (\Exception $e) {
$this->verificationError = $e->getMessage();
$this->verified = false;
}
}
return $this->verified;
}
/**
* Get string verification error message from any exception caught during the verification process.
*
* @return false if verified, otherwise string error message
*/
public function getVerificationError()
{
return $this->verificationError;
}
/**
* For extending classes to implement custom environment validation logic. Should throw exceptions on failure, or return true on success.
*
* @return boolean
*/
protected function verifyEnvironment()
{
return true;
}
/**
* Return the key for this adapter
*
* @return string
*/
public function getKey()
{
return $this->key;
}
/**
* Return string name of this adapter, the key will be returned if a name is not defined.
*
* @return string
*/
public function getName()
{
if (!$this->name) {
return $this->key;
}
return $this->name;
}
/**
* Return human readable description of what this adapter is generally for.
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Called by the Transcoder to enable the Adapter to dispatch messages
*
* @param Transcoder $t
* @return void
*/
public function setTranscoder(Transcoder $t = null)
{
$this->transcoder = $t;
}
/**
* Send a debug message.
*
* @param string $msg
* @return void
*/
public function debug($msg)
{
$this->message($msg, MessageEvent::DEBUG);
}
/**
* Send an info message
*
* @param string $msg
* @return void
*/
public function info($msg)
{
$this->message($msg, MessageEvent::INFO);
}
/**
* Send a warning message
*
* @param string $msg
* @return void
*/
public function warn($msg)
{
$this->message($msg, MessageEvent::WARN);
}
/**
* Send an error message. Note that if an error is bad enough that a process can't continue, you should
* throw an exception with your error message instead of calling this.
*
* @param string $msg
* @return void
*/
public function error($msg)
{
$this->message($msg, MessageEvent::ERROR);
}
/**
* Used internally by the Adapter to dispatch sent messages via the Transcoder
*
* @param string $msg
* @param string $level
* @return void
*/
private function message($msg, $level)
{
if ($this->transcoder) {
$this->transcoder->dispatch(TranscodeEvents::MESSAGE, new MessageEvent($msg, $level, $this));
}
}
}