|
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
|
|
|
protected 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 |
|
$type = static::detectType($path); |
|
36
|
1 |
|
$class = static::findClass($type); |
|
37
|
1 |
|
|
|
38
|
1 |
|
#return static::create($builder, $type); |
|
39
|
|
|
|
|
40
|
|
|
$uniqid = $class . ':' . spl_object_hash($builder); |
|
41
|
1 |
|
if (empty(static::$loaders[$uniqid])) { |
|
|
|
|
|
|
42
|
|
|
static::$loaders[$uniqid] = static::create($builder, $type); |
|
43
|
|
|
} |
|
44
|
1 |
|
|
|
45
|
|
|
return static::$loaders[$uniqid]; |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function detectType($path) |
|
49
|
|
|
{ |
|
50
|
1 |
|
if (strncmp(basename($path), '.env.', 5) === 0) { |
|
51
|
|
|
return 'env'; |
|
52
|
|
|
} |
|
53
|
1 |
|
|
|
54
|
|
|
return pathinfo($path, PATHINFO_EXTENSION); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public static function findClass($type) |
|
58
|
|
|
{ |
|
59
|
|
|
if (empty(static::$knownReaders[$type])) { |
|
60
|
|
|
throw new UnsupportedFileTypeException("unsupported file type: $type"); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return static::$knownReaders[$type]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function create(Builder $builder, $type) |
|
67
|
|
|
{ |
|
68
|
|
|
$class = static::findClass($type); |
|
69
|
|
|
|
|
70
|
|
|
return new $class($builder); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|