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
|
<?php
namespace Gettext\Extractors;
use Exception; use InvalidArgumentException; use Gettext\Translations;
abstract class Extractor implements ExtractorInterface { /** * {@inheritdoc} */ public static function fromFile($file, Translations $translations, array $options = []) { foreach (self::getFiles($file) as $file) { $options['file'] = $file; static::fromString(self::readFile($file), $translations, $options); } }
/** * Checks and returns all files. * * @param string|array $file The file/s * * @return array The file paths */ protected static function getFiles($file) { if (empty($file)) { throw new InvalidArgumentException('There is not any file defined'); }
if (is_string($file)) { if (!is_file($file)) { throw new InvalidArgumentException("'$file' is not a valid file"); }
if (!is_readable($file)) { throw new InvalidArgumentException("'$file' is not a readable file"); }
return [$file]; }
if (is_array($file)) { $files = [];
foreach ($file as $f) { $files = array_merge($files, self::getFiles($f)); }
return $files; }
throw new InvalidArgumentException('The first argument must be string or array'); }
/** * Reads and returns the content of a file. * * @param string $file * * @return string */ protected static function readFile($file) { $length = filesize($file);
if (!($fd = fopen($file, 'rb'))) { throw new Exception("Cannot read the file '$file', probably permissions"); }
$content = $length ? fread($fd, $length) : ''; fclose($fd);
return $content; } }
|