1 | <?php |
||
7 | class MediaInfoCommandRunner |
||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | protected $filePath; |
||
13 | |||
14 | /** |
||
15 | * @var Process |
||
16 | */ |
||
17 | protected $process; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $command = 'mediainfo'; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $arguments = ['--OUTPUT=XML', '-f']; |
||
28 | |||
29 | /** |
||
30 | * @param string $filePath |
||
31 | * @param array $arguments |
||
32 | * @param Process $process |
||
33 | */ |
||
34 | 6 | public function __construct( |
|
35 | $filePath, |
||
36 | $command = null, |
||
37 | array $arguments = null, |
||
38 | Process $process = null |
||
39 | ) { |
||
40 | 6 | $this->filePath = $filePath; |
|
41 | 6 | if ($command !== null) { |
|
42 | 2 | $this->command = $command; |
|
43 | 2 | } |
|
44 | |||
45 | 6 | if ($arguments !== null) { |
|
46 | 3 | $this->arguments = $arguments; |
|
47 | 3 | } |
|
48 | |||
49 | 6 | $args = $this->arguments; |
|
50 | 6 | array_unshift($args, $this->filePath); |
|
51 | |||
52 | $env = [ |
||
53 | 6 | 'LANG' => setlocale(LC_CTYPE, 0), |
|
54 | 6 | ]; |
|
55 | 6 | $finalCommand = [$this->command]; |
|
56 | |||
57 | 6 | foreach ($args as $value) { |
|
58 | 6 | $finalCommand[] = $value; |
|
59 | 6 | } |
|
60 | |||
61 | 6 | if (null !== $process) { |
|
62 | 3 | $process->setEnv($env); |
|
63 | 3 | $this->process = $process; |
|
64 | 3 | } else { |
|
65 | 3 | $this->process = new Process($finalCommand, null, $env); |
|
66 | } |
||
67 | 6 | } |
|
68 | |||
69 | /** |
||
70 | * @throws \RuntimeException |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 2 | public function run() |
|
75 | { |
||
76 | 2 | $this->process->run(); |
|
77 | 2 | if (!$this->process->isSuccessful()) { |
|
78 | 1 | throw new \RuntimeException($this->process->getErrorOutput()); |
|
79 | } |
||
80 | |||
81 | 1 | return $this->process->getOutput(); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Asynchronously start mediainfo operation. |
||
86 | * Make call to MediaInfoCommandRunner::wait() afterwards to receive output. |
||
87 | */ |
||
88 | 1 | public function start() |
|
89 | { |
||
90 | // just takes advantage of symfony's underlying Process framework |
||
91 | // process runs in background |
||
92 | 1 | $this->process->start(); |
|
93 | 1 | } |
|
94 | |||
95 | /** |
||
96 | * Blocks until call is complete. |
||
97 | * |
||
98 | * @throws \Exception If this function is called before start() |
||
99 | * @throws \RuntimeException |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 1 | public function wait() |
|
118 | } |
||
119 |