1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palmtree\Container; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Yaml\Yaml; |
6
|
|
|
|
7
|
|
|
class ContainerFactory |
8
|
|
|
{ |
9
|
|
|
/** @var Container */ |
10
|
|
|
private $container; |
11
|
|
|
/** @var array */ |
12
|
|
|
private $phpImports = []; |
13
|
|
|
|
14
|
24 |
|
private function __construct($configFile) |
15
|
|
|
{ |
16
|
24 |
|
$yaml = $this->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 |
|
$this->container = new Container($yaml['services'], $yaml['parameters']); |
27
|
|
|
|
28
|
24 |
|
foreach ($this->phpImports as $file) { |
29
|
21 |
|
self::requirePhpFile($file, $this->container); |
30
|
|
|
} |
31
|
|
|
|
32
|
24 |
|
$this->container->instantiateServices(); |
33
|
24 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $configFile |
37
|
|
|
* |
38
|
|
|
* @return Container |
39
|
|
|
*/ |
40
|
24 |
|
public static function create($configFile) |
41
|
|
|
{ |
42
|
24 |
|
$factory = new self($configFile); |
43
|
|
|
|
44
|
24 |
|
return $factory->container; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $file |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
24 |
|
private function parseYamlFile($file) |
53
|
|
|
{ |
54
|
24 |
|
$data = Yaml::parseFile($file); |
|
|
|
|
55
|
|
|
|
56
|
24 |
|
if (isset($data['imports'])) { |
57
|
21 |
|
$data = $this->parseImports($data, dirname($file)); |
58
|
|
|
} |
59
|
|
|
|
60
|
24 |
|
return $data; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $data |
65
|
|
|
* @param string $dir |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
21 |
|
private function parseImports($data, $dir) |
70
|
|
|
{ |
71
|
21 |
|
foreach ($data['imports'] as $key => $import) { |
72
|
21 |
|
$resource = self::getImportResource($dir, $import); |
73
|
|
|
|
74
|
21 |
|
$extension = pathinfo($resource, PATHINFO_EXTENSION); |
75
|
|
|
|
76
|
21 |
|
if ($extension === 'yml' || $extension === 'yaml') { |
77
|
21 |
|
$data = array_replace_recursive($data, $this->parseYamlFile($resource)); |
78
|
21 |
|
unset($data['imports'][$key]); |
79
|
21 |
|
} elseif ($extension === 'php') { |
80
|
21 |
|
$this->phpImports[] = $resource; |
81
|
21 |
|
unset($data['imports'][$key]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
21 |
|
return $data; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $file |
90
|
|
|
* @param Container $container |
91
|
|
|
*/ |
92
|
21 |
|
private static function requirePhpFile($file, $container) |
93
|
|
|
{ |
94
|
21 |
|
require $file; |
95
|
21 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $dir |
99
|
|
|
* @param array $import |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
21 |
|
private static function getImportResource($dir, $import) |
104
|
|
|
{ |
105
|
21 |
|
$resource = $import['resource']; |
106
|
|
|
|
107
|
|
|
// Prefix the directory if resource is not an absolute path |
108
|
21 |
|
if ($resource[0] !== DIRECTORY_SEPARATOR && !preg_match('~\A[A-Z]:(?![^/\\\\])~i', $resource)) { |
109
|
21 |
|
$resource = "$dir/$resource"; |
110
|
|
|
} |
111
|
|
|
|
112
|
21 |
|
return $resource; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.