1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Composer plugin for config assembling |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/composer-config-plugin |
6
|
|
|
* @package composer-config-plugin |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2018, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\composer\config\readers; |
12
|
|
|
|
13
|
|
|
use hiqdev\composer\config\Builder; |
14
|
|
|
use hiqdev\composer\config\exceptions\UnsupportedFileTypeException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Reader - helper to load data from files of different types. |
18
|
|
|
* |
19
|
|
|
* @author Andrii Vasyliev <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ReaderFactory |
22
|
|
|
{ |
23
|
|
|
private static $loaders; |
24
|
|
|
|
25
|
|
|
private static $knownReaders = [ |
26
|
|
|
'env' => EnvReader::class, |
27
|
|
|
'php' => PhpReader::class, |
28
|
|
|
'json' => JsonReader::class, |
29
|
|
|
'yaml' => YamlReader::class, |
30
|
|
|
'yml' => YamlReader::class, |
31
|
|
|
]; |
32
|
|
|
|
33
|
1 |
|
public static function get(Builder $builder, $path) |
34
|
|
|
{ |
35
|
1 |
|
$ext = pathinfo($path, PATHINFO_EXTENSION); |
36
|
1 |
|
$class = static::findClass($ext); |
37
|
1 |
|
if (empty(static::$loaders[$class])) { |
|
|
|
|
38
|
1 |
|
static::$loaders[$class] = static::create($builder, $ext); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return static::$loaders[$class]; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public static function findClass($ext) |
45
|
|
|
{ |
46
|
1 |
|
if (empty(static::$knownReaders[$ext])) { |
|
|
|
|
47
|
|
|
throw new UnsupportedFileTypeException("unsupported extension: $ext"); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
return static::$knownReaders[$ext]; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
public static function create(Builder $builder, $ext) |
54
|
|
|
{ |
55
|
1 |
|
$class = static::findClass($ext); |
56
|
|
|
|
57
|
1 |
|
return new $class($builder); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|