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
|
24 |
|
public static function create($configFile) |
15
|
|
|
{ |
16
|
24 |
|
$yaml = static::parseYamlFile($configFile); |
17
|
|
|
|
18
|
24 |
|
if (!isset($yaml['services'])) { |
19
|
2 |
|
$yaml['services'] = []; |
20
|
|
|
} |
21
|
|
|
|
22
|
24 |
|
if (!isset($yaml['parameters'])) { |
23
|
2 |
|
$yaml['parameters'] = []; |
24
|
|
|
} |
25
|
|
|
|
26
|
24 |
|
$container = new Container($yaml['services'], $yaml['parameters']); |
27
|
|
|
|
28
|
|
|
// Parse again after the container again to import PHP files |
29
|
24 |
|
static::parseYamlFile($configFile, $container); |
30
|
|
|
|
31
|
24 |
|
$container->instantiateServices(); |
32
|
|
|
|
33
|
24 |
|
return $container; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $file |
38
|
|
|
* @param Container|null $container |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
24 |
|
private static function parseYamlFile($file, $container = null) |
43
|
|
|
{ |
44
|
24 |
|
$data = Yaml::parse(file_get_contents($file)); |
45
|
|
|
|
46
|
24 |
|
$data = static::parseImports($data, dirname($file), $container); |
47
|
|
|
|
48
|
24 |
|
return $data; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $file |
53
|
|
|
* @param Container $container |
54
|
|
|
*/ |
55
|
21 |
|
private static function parsePhpFile($file, Container $container) |
56
|
|
|
{ |
57
|
21 |
|
require $file; |
58
|
21 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $data |
62
|
|
|
* @param string $dir |
63
|
|
|
* @param Container|null $container |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
24 |
|
private static function parseImports($data, $dir, $container = null) |
68
|
|
|
{ |
69
|
24 |
|
if (!$data) { |
|
|
|
|
70
|
1 |
|
return $data; |
71
|
|
|
} |
72
|
|
|
|
73
|
23 |
|
foreach ($data as $key => $imports) { |
74
|
23 |
|
if ($key !== 'imports' || !$imports || !is_array($imports)) { |
75
|
23 |
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
21 |
|
foreach ($imports as $importKey => $import) { |
79
|
21 |
|
$resource = $import['resource']; |
80
|
|
|
|
81
|
|
|
// Prefix the directory if resource is not an absolute path |
82
|
21 |
|
if ($resource[0] !== DIRECTORY_SEPARATOR && !preg_match('~\A[A-Z]:(?![^/\\\\])~i', $resource)) { |
83
|
21 |
|
$resource = "$dir/$resource"; |
84
|
|
|
} |
85
|
|
|
|
86
|
21 |
|
$reference = &$data; |
87
|
|
|
|
88
|
21 |
|
$extension = pathinfo($resource, PATHINFO_EXTENSION); |
89
|
|
|
|
90
|
21 |
|
if ($extension === 'yml' || $extension === 'yaml') { |
91
|
21 |
|
$reference = array_replace_recursive($reference, static::parseYamlFile($resource)); |
92
|
21 |
|
unset($reference['imports'][$importKey]); |
93
|
21 |
|
} elseif ($extension === 'php' && $container instanceof Container) { |
94
|
21 |
|
static::parsePhpFile($resource, $container); |
95
|
21 |
|
unset($reference['imports'][$importKey]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
23 |
|
return $data; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.