1 | <?php |
||
9 | class MediaInfoCommandBuilder |
||
10 | { |
||
11 | /** |
||
12 | * @param string $filePath |
||
13 | * @param array $configuration |
||
14 | * |
||
15 | * @throws \Exception |
||
16 | * |
||
17 | * @return MediaInfoCommandRunner |
||
18 | */ |
||
19 | public function buildMediaInfoCommandRunner(string $filePath, array $configuration = []): MediaInfoCommandRunner |
||
20 | { |
||
21 | if (filter_var($filePath, FILTER_VALIDATE_URL) === false) { |
||
22 | $fileSystem = new Filesystem(); |
||
23 | |||
24 | if (!$fileSystem->exists($filePath)) { |
||
25 | throw new \Exception(sprintf('File "%s" does not exist', $filePath)); |
||
26 | } |
||
27 | |||
28 | if (is_dir($filePath)) { |
||
29 | throw new \Exception(sprintf( |
||
30 | 'Expected a filename, got "%s", which is a directory', |
||
31 | $filePath |
||
32 | )); |
||
33 | } |
||
34 | } |
||
35 | |||
36 | $configuration += [ |
||
37 | 'command' => null, |
||
38 | 'use_oldxml_mediainfo_output_format' => true, |
||
39 | ]; |
||
40 | |||
41 | return new MediaInfoCommandRunner($this->buildMediaInfoProcess( |
||
42 | $filePath, |
||
43 | $configuration['command'], |
||
44 | $configuration['use_oldxml_mediainfo_output_format'], |
||
45 | $configuration['urlencode'], |
||
46 | )); |
||
|
|||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param string $filePath |
||
51 | * @param string|null $command |
||
52 | * @param bool $forceOldXmlOutput |
||
53 | * @param bool $urlencode |
||
54 | * |
||
55 | * @return Process |
||
56 | */ |
||
57 | private function buildMediaInfoProcess(string $filePath, string $command = null, bool $forceOldXmlOutput = true, bool $urlencode = false): Process |
||
58 | { |
||
59 | if ($command === null) { |
||
60 | $command = MediaInfoCommandRunner::MEDIAINFO_COMMAND; |
||
61 | } |
||
62 | |||
63 | // arguments are given through ENV vars in order to have system escape them |
||
64 | $arguments = [ |
||
65 | 'MEDIAINFO_VAR_FILE_PATH' => $filePath, |
||
66 | 'MEDIAINFO_VAR_FULL_DISPLAY' => MediaInfoCommandRunner::MEDIAINFO_FULL_DISPLAY_ARGUMENT, |
||
67 | 'MEDIAINFO_VAR_OUTPUT' => MediaInfoCommandRunner::MEDIAINFO_OLDXML_OUTPUT_ARGUMENT, |
||
68 | ]; |
||
69 | |||
70 | if (!$forceOldXmlOutput) { |
||
71 | $arguments['MEDIAINFO_VAR_OUTPUT'] = MediaInfoCommandRunner::MEDIAINFO_XML_OUTPUT_ARGUMENT; |
||
72 | } |
||
73 | |||
74 | if ($urlencode) { |
||
75 | $arguments['MEDIAINFO_VAR_URLENCODE'] = MediaInfoCommandRunner::MEDIAINFO_URLENCODE; |
||
76 | } |
||
77 | |||
78 | $env = $arguments + [ |
||
79 | 'LANG' => setlocale(LC_CTYPE, 0), |
||
80 | ]; |
||
81 | |||
82 | return new Process( |
||
83 | array_merge([$command], array_values($arguments)), |
||
89 |