|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Common\Config; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Loader\FileLoader; |
|
6
|
|
|
use Symfony\Component\Config\Exception\FileLoaderLoadException; |
|
7
|
|
|
use Symfony\Component\Config\Definition\Processor; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class PHPConfigLoader |
|
11
|
|
|
* |
|
12
|
|
|
* @package App\Common\Config |
|
13
|
|
|
*/ |
|
14
|
|
|
class PHPConfigLoader extends FileLoader |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var FileLocator |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $locator; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Import all loadable resources |
|
23
|
|
|
* |
|
24
|
|
|
* @return array |
|
25
|
|
|
* @throws FileLoaderLoadException |
|
26
|
|
|
*/ |
|
27
|
|
|
public function importAll() |
|
28
|
|
|
{ |
|
29
|
|
|
// in order to import all we need to have locator to locate these "all" |
|
30
|
|
|
// locator need |
|
31
|
|
|
if (empty($this->locator)) { |
|
32
|
|
|
throw new FileLoaderLoadException('importAll()'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// locator must be capable of locating all |
|
36
|
|
|
if (!is_callable([$this->locator, 'locateAll'])) { |
|
37
|
|
|
throw new FileLoaderLoadException('locateAll()'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
// build list of 'all' files |
|
41
|
|
|
$files = $this->locator->locateAll(); |
|
42
|
|
|
|
|
43
|
|
|
// and now try to import configuration from this list of 'all' files |
|
44
|
|
|
$res = []; |
|
45
|
|
|
foreach ($files as $file) { |
|
46
|
|
|
if ($this->supports($file)) { |
|
47
|
|
|
// file is supported - load/import it |
|
48
|
|
|
$config = $this->load($file); |
|
49
|
|
|
|
|
50
|
|
|
if (!empty($config['definition'])) { |
|
51
|
|
|
// loaded config can be verified |
|
52
|
|
|
$processor = new Processor(); |
|
53
|
|
|
$configDefinition = new $config['definition'](); |
|
54
|
|
|
// process config according to specified definition |
|
55
|
|
|
$config = $processor->processConfiguration( |
|
56
|
|
|
$configDefinition, |
|
57
|
|
|
[$config] |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// combine all configs into one "unified general config" |
|
62
|
|
|
$res = array_merge($res, $config); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// array of configuration |
|
67
|
|
|
return $res; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @inheritdoc |
|
72
|
|
|
* @return array|\StdClass loaded object |
|
73
|
|
|
*/ |
|
74
|
|
|
public function load($resource, $type = null) |
|
75
|
|
|
{ |
|
76
|
|
|
// for PHP file load process is quite simple - just return config file content |
|
77
|
|
|
return require_once $resource; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns whether current loader supports specified resource load |
|
82
|
|
|
* |
|
83
|
|
|
* @param mixed $resource path to config file |
|
84
|
|
|
* @param null $type unsused |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function supports($resource, $type = null) |
|
88
|
|
|
{ |
|
89
|
|
|
// $resource is expected to be path to config file |
|
90
|
|
|
if (!is_string($resource)) { |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// specified config file should be readable |
|
95
|
|
|
if (!@is_file($resource) || !@is_readable($resource)) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// simple check - PHP loader accepts PHP files, thus let's check file extension to be 'php' |
|
100
|
|
|
|
|
101
|
|
|
// fetch config file extension |
|
102
|
|
|
$extension = pathinfo($resource, PATHINFO_EXTENSION); |
|
103
|
|
|
|
|
104
|
|
|
// PHP loader accepts PHP files |
|
105
|
|
|
return $extension == 'php'; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|