1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\DI; |
4
|
|
|
|
5
|
|
|
use Anax\DI\Exception\Exception; |
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* DI factory class creating a set of default services by loading |
10
|
|
|
* them from a configuration array, file and/or directory. |
11
|
|
|
* |
12
|
|
|
* The general configuration array for a service looks like this. |
13
|
|
|
* [ |
14
|
|
|
* "services" => [ |
15
|
|
|
* "request" => [ |
16
|
|
|
* "active" => false, |
17
|
|
|
* "shared" => false, |
18
|
|
|
* "callback" => function () { |
19
|
|
|
* $object = new \stdClass(); |
20
|
|
|
* return $object; |
21
|
|
|
* } |
22
|
|
|
* ], |
23
|
|
|
* ], |
24
|
|
|
* ] |
25
|
|
|
*/ |
26
|
|
|
class DIFactoryConfig extends DI implements ContainerInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Create services by using $item as a reference to find a |
30
|
|
|
* configuration for the services. The $item can be an array, |
31
|
|
|
* a file.php, or an directory containing files named *.php. |
32
|
|
|
* |
33
|
|
|
* @param array|string $item referencing the source for configuration. |
34
|
|
|
* |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
7 |
|
public function loadServices($item) : object |
38
|
|
|
{ |
39
|
7 |
|
if (is_array($item)) { |
40
|
3 |
|
$this->createServicesFromArray($item, "array"); |
41
|
1 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
if (is_readable($item) && is_file($item)) { |
45
|
1 |
|
$services = require $item; |
46
|
1 |
|
$this->createServicesFromArray($services, $item); |
47
|
1 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
$found = false; |
51
|
3 |
|
if (is_readable("$item.php") && is_file("$item.php")) { |
52
|
2 |
|
$services = require "$item.php"; |
53
|
2 |
|
$this->createServicesFromArray($services, $item); |
54
|
2 |
|
$found = true; |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
if (is_readable($item) && is_dir($item)) { |
58
|
2 |
|
foreach (glob("$item/*.php") as $file) { |
59
|
2 |
|
$services = require "$file"; |
60
|
2 |
|
$this->createServicesFromArray($services, $file); |
61
|
2 |
|
$found = true; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
if (!$found) { |
66
|
1 |
|
throw new Exception("Item to load configuration from was no file, dir nor an array."); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create services from an array containing a list of services. |
76
|
|
|
* |
77
|
|
|
* @param array $service details to use when creating the service. |
78
|
|
|
* |
79
|
|
|
* @throws Exception when configuration is corrupt. |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
6 |
|
protected function createServicesFromArray( |
84
|
|
|
array $services, |
85
|
|
|
string $path |
86
|
|
|
) : void { |
87
|
6 |
|
if (!isset($services["services"])) { |
88
|
1 |
|
throw new Exception("The configuration array is missing the key 'services' in file '$path'."); |
89
|
|
|
} |
90
|
|
|
|
91
|
5 |
|
foreach ($services["services"] as $name => $service) { |
92
|
|
|
try { |
93
|
5 |
|
$this->createService($name, $service); |
94
|
1 |
|
} catch (Exception $e) { |
95
|
5 |
|
throw new Exception($e->getMessage() . " In configuration file '$path'."); |
96
|
|
|
} |
97
|
|
|
} |
98
|
4 |
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Create a service from a name and an array containing details on |
104
|
|
|
* how to create it. |
105
|
|
|
* |
106
|
|
|
* @param string $name of service. |
107
|
|
|
* @param array $service details to use when creating the service. |
108
|
|
|
* |
109
|
|
|
* @throws Exception when configuration is corrupt. |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
5 |
|
protected function createService(string $name, array $service) : void |
114
|
|
|
{ |
115
|
5 |
|
if (!isset($service["callback"])) { |
116
|
1 |
|
throw new Exception("The service '$name' is missing a callback."); |
117
|
|
|
} |
118
|
|
|
|
119
|
4 |
|
if (isset($service["shared"]) && $service["shared"]) { |
120
|
3 |
|
$this->setShared($name, $service["callback"]); |
121
|
|
|
} else { |
122
|
4 |
|
$this->set($name, $service["callback"]); |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
if (isset($service["active"]) && $service["active"]) { |
126
|
1 |
|
$this->get($name); |
127
|
|
|
} |
128
|
4 |
|
} |
129
|
|
|
} |
130
|
|
|
|