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
|
6 |
|
* |
33
|
|
|
* @param array|string $item referencing the source for configuration. |
34
|
6 |
|
* |
35
|
6 |
|
* @return $this |
36
|
|
|
*/ |
37
|
6 |
|
public function loadServices($item) : object |
38
|
|
|
{ |
39
|
|
|
if (is_array($item)) { |
40
|
|
|
$this->createServicesFromArray($item, "array"); |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (is_readable($item) && is_file($item)) { |
45
|
|
|
$services = require $item; |
46
|
|
|
$this->createServicesFromArray($services, $item); |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$found = false; |
51
|
6 |
View Code Duplication |
if (is_readable("$item.php") && is_file("$item.php")) { |
|
|
|
|
52
|
|
|
$services = require "$item.php"; |
53
|
6 |
|
$this->createServicesFromArray($services, $item); |
54
|
|
|
$found = true; |
55
|
6 |
|
} |
56
|
6 |
|
|
57
|
6 |
View Code Duplication |
if (is_readable($item) && is_dir($item)) { |
|
|
|
|
58
|
|
|
foreach (glob("$item/*.php") as $file) { |
59
|
|
|
$services = require "$file"; |
60
|
6 |
|
$this->createServicesFromArray($services, $file); |
61
|
6 |
|
$found = true; |
62
|
6 |
|
} |
63
|
6 |
|
} |
64
|
6 |
|
|
65
|
|
|
if (!$found) { |
66
|
|
|
throw new Exception("Item to load configuration from was no file, dir nor an array."); |
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
|
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
|
6 |
|
string $path |
86
|
6 |
|
) : void |
87
|
|
|
{ |
88
|
|
|
if (!isset($services["services"])) { |
89
|
|
|
throw new Exception("The configuration array is missing the key 'services' in file '$path'."); |
90
|
|
|
} |
91
|
6 |
|
|
92
|
|
|
foreach ($services["services"] as $name => $service) { |
93
|
|
|
try { |
94
|
|
|
$this->createService($name, $service); |
95
|
6 |
|
} catch (Exception $e) { |
96
|
|
|
throw new Exception($e->getMessage() . " In configuration file '$path'."); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create a service from a name and an array containing details on |
105
|
|
|
* how to create it. |
106
|
|
|
* |
107
|
|
|
* @param string $name of service. |
108
|
|
|
* @param array $service details to use when creating the service. |
109
|
|
|
* |
110
|
|
|
* @throws Exception when configuration is corrupt. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
protected function createService(string $name, array $service) : void |
115
|
|
|
{ |
116
|
|
|
if (!isset($service["callback"])) { |
117
|
|
|
throw new Exception("The service '$name' is missing a callback."); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (isset($service["shared"]) && $service["shared"]) { |
121
|
|
|
$this->setShared($name, $service["callback"]); |
122
|
|
|
} else { |
123
|
|
|
$this->set($name, $service["callback"]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (isset($service["active"]) && $service["active"]) { |
127
|
|
|
$this->get($name); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.