1 | <?php |
||
2 | |||
3 | namespace Bavix\SDK; |
||
4 | |||
5 | use Bavix\Exceptions\NotFound; |
||
6 | use Bavix\Exceptions; |
||
7 | use Bavix\Helpers\Arr; |
||
8 | use Bavix\Helpers\File; |
||
9 | use Bavix\Helpers\PregMatch; |
||
10 | |||
11 | class FileLoader |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected static $extensions = [ |
||
18 | 'php' => FileLoader\PHPLoader::class, |
||
19 | 'yml' => FileLoader\YamlLoader::class, |
||
20 | 'yaml' => FileLoader\YamlLoader::class, |
||
21 | 'json' => FileLoader\JSONLoader::class, |
||
22 | 'xml' => FileLoader\XmlLoader::class, |
||
23 | 'ini' => FileLoader\IniLoader::class, |
||
24 | ]; |
||
25 | |||
26 | /** |
||
27 | * @return array |
||
28 | * @codeCoverageIgnore |
||
29 | */ |
||
30 | public static function extensions() |
||
31 | { |
||
32 | return Arr::getKeys(static::$extensions); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param string $file |
||
37 | * |
||
38 | * @return FileLoader\DataInterface |
||
39 | * |
||
40 | * @throws NotFound\Path |
||
41 | * @throws Exceptions\PermissionDenied |
||
42 | */ |
||
43 | 6 | public static function load($file) |
|
44 | { |
||
45 | 6 | if (!File::isFile($file)) |
|
46 | { |
||
47 | 1 | throw new NotFound\Path($file); |
|
48 | } |
||
49 | |||
50 | 5 | if (!File::isReadable($file)) |
|
51 | { |
||
52 | 1 | throw new Exceptions\PermissionDenied($file); |
|
53 | } |
||
54 | |||
55 | 4 | $preg = PregMatch::first('~\.(?<ext>\w+)$~', $file); |
|
56 | |||
57 | 4 | $class = Arr::get( |
|
58 | 4 | static::$extensions, |
|
59 | 4 | Arr::get($preg->matches, 'ext', 'php'), |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
60 | 4 | static::$extensions['php'] |
|
61 | ); |
||
62 | |||
63 | 4 | return new $class($file); |
|
64 | } |
||
65 | |||
66 | } |
||
67 |