1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palmtree\Container; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Yaml\Yaml; |
6
|
|
|
|
7
|
|
|
class ContainerFactory |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param string $configFile |
11
|
|
|
* |
12
|
|
|
* @return Container |
13
|
|
|
*/ |
14
|
16 |
|
public static function create($configFile) |
15
|
|
|
{ |
16
|
16 |
|
$yaml = static::parseYamlFile($configFile); |
17
|
|
|
|
18
|
16 |
|
if (!isset($yaml['services'])) { |
19
|
|
|
$yaml['services'] = []; |
20
|
|
|
} |
21
|
|
|
|
22
|
16 |
|
if (!isset($yaml['parameters'])) { |
23
|
|
|
$yaml['parameters'] = []; |
24
|
|
|
} |
25
|
|
|
|
26
|
16 |
|
$container = new Container($yaml['services'], $yaml['parameters']); |
27
|
|
|
|
28
|
|
|
// Parse again after the container again to import PHP files |
29
|
16 |
|
static::parseYamlFile($configFile, $container); |
30
|
|
|
|
31
|
16 |
|
$container->instantiateServices(); |
32
|
|
|
|
33
|
16 |
|
return $container; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $file |
38
|
|
|
* @param Container|null $container |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
16 |
|
protected static function parseYamlFile($file, $container = null) |
43
|
|
|
{ |
44
|
16 |
|
$data = Yaml::parse(file_get_contents($file)); |
45
|
|
|
|
46
|
16 |
|
$data = static::parseImports($data, dirname($file), $container); |
47
|
|
|
|
48
|
16 |
|
return $data; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $file |
53
|
|
|
* @param Container $container |
54
|
|
|
*/ |
55
|
16 |
|
protected static function parsePhpFile($file, Container $container) |
56
|
|
|
{ |
57
|
16 |
|
require $file; |
58
|
16 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $data |
62
|
|
|
* @param string $dir |
63
|
|
|
* @param Container|null $container |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
16 |
|
protected static function parseImports($data, $dir, $container = null) |
68
|
|
|
{ |
69
|
16 |
|
foreach ($data as $key => $value) { |
70
|
16 |
|
if (!is_array($value)) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
16 |
|
$imports = null; |
75
|
16 |
|
$root = false; |
76
|
|
|
|
77
|
16 |
|
if ($key === 'imports') { |
78
|
16 |
|
$imports = $value; |
79
|
16 |
|
$root = true; |
80
|
16 |
|
} elseif (isset($value['imports'])) { |
81
|
|
|
$imports = $value['imports']; |
82
|
|
|
} |
83
|
|
|
|
84
|
16 |
|
if ($imports) { |
85
|
16 |
|
foreach ($imports as $importKey => $import) { |
86
|
16 |
|
$resource = $import['resource']; |
87
|
|
|
|
88
|
16 |
|
if (strpos($resource, '/') === false) { |
89
|
16 |
|
$resource = sprintf('%s/%s', $dir, $resource); |
90
|
|
|
} |
91
|
|
|
|
92
|
16 |
|
if ($root) { |
93
|
16 |
|
$reference = &$data; |
94
|
|
|
} else { |
95
|
|
|
$reference = &$data[$key]; |
96
|
|
|
} |
97
|
|
|
|
98
|
16 |
|
$extension = pathinfo($resource, PATHINFO_EXTENSION); |
99
|
|
|
|
100
|
16 |
|
if ($extension === 'yml' || $extension === 'yaml') { |
101
|
16 |
|
$reference = array_replace_recursive($reference, static::parseYamlFile($resource)); |
102
|
16 |
|
unset($reference['imports'][$importKey]); |
103
|
16 |
|
} elseif ($extension === 'php' && $container instanceof Container) { |
104
|
16 |
|
static::parsePhpFile($resource, $container); |
105
|
16 |
|
unset($reference['imports'][$importKey]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
16 |
|
return $data; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|