|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Kernel\Containers; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Spl\Containers\DataStructures\SplServiceRegistry; |
|
19
|
|
|
use O2System\Spl\Containers\SplServiceContainer; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class Services |
|
23
|
|
|
* |
|
24
|
|
|
* @package O2System\Framework |
|
25
|
|
|
*/ |
|
26
|
|
|
class Services extends SplServiceContainer |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Services::load |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $className |
|
32
|
|
|
* @param string|null $offset |
|
33
|
|
|
*/ |
|
34
|
|
|
public function load($className, $offset = null) |
|
35
|
|
|
{ |
|
36
|
|
|
if (is_string($className)) { |
|
|
|
|
|
|
37
|
|
|
$className = str_replace([ |
|
38
|
|
|
'O2System\Framework\\', |
|
39
|
|
|
'O2System\Reactor\\', |
|
40
|
|
|
'O2System\Kernel\\', |
|
41
|
|
|
'App\\', |
|
42
|
|
|
], '', |
|
43
|
|
|
ltrim($className, '\\') |
|
44
|
|
|
); |
|
45
|
|
|
|
|
46
|
|
|
if (class_exists($className)) { |
|
47
|
|
|
$service = new SplServiceRegistry($className); |
|
48
|
|
|
} else { |
|
49
|
|
|
if (is_object(kernel()->modules)) { |
|
|
|
|
|
|
50
|
|
|
if ($module = kernel()->modules->top()) { |
|
51
|
|
|
if (class_exists($serviceClassName = $module->getNamespace() . $className)) { |
|
52
|
|
|
$service = new SplServiceRegistry($serviceClassName); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (empty($service)) { |
|
58
|
|
|
if (class_exists($serviceClassName = 'App\\' . $className)) { |
|
59
|
|
|
$service = new SplServiceRegistry($serviceClassName); |
|
60
|
|
|
} elseif (class_exists($serviceClassName = 'O2System\Framework\\' . $className)) { |
|
61
|
|
|
$service = new SplServiceRegistry($serviceClassName); |
|
62
|
|
|
} elseif (class_exists($serviceClassName = 'O2System\Reactor\\' . $className)) { |
|
63
|
|
|
$service = new SplServiceRegistry($serviceClassName); |
|
64
|
|
|
} elseif (class_exists($serviceClassName = 'O2System\Kernel\\' . $className)) { |
|
65
|
|
|
$service = new SplServiceRegistry($serviceClassName); |
|
66
|
|
|
} elseif(class_exists($className)) { |
|
67
|
|
|
$service = new SplServiceRegistry($serviceClassName); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (isset($service)) { |
|
74
|
|
|
if ($service instanceof SplServiceRegistry) { |
|
|
|
|
|
|
75
|
|
|
if (profiler() !== false) { |
|
76
|
|
|
profiler()->watch('Load New Service: ' . $service->getClassName()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->register($service, $offset); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// ------------------------------------------------------------------------ |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Services::register |
|
88
|
|
|
* |
|
89
|
|
|
* @param SplServiceRegistry $service |
|
90
|
|
|
* @param string|null $offset |
|
91
|
|
|
*/ |
|
92
|
|
|
public function register(SplServiceRegistry $service, $offset = null) |
|
93
|
|
|
{ |
|
94
|
|
|
if ($service instanceof SplServiceRegistry) { |
|
|
|
|
|
|
95
|
|
|
$offset = isset($offset) |
|
96
|
|
|
? $offset |
|
97
|
|
|
: camelcase($service->getParameter()); |
|
98
|
|
|
|
|
99
|
|
|
$this->attach($offset, $service); |
|
100
|
|
|
|
|
101
|
|
|
if (profiler() !== false) { |
|
102
|
|
|
profiler()->watch('Register New Service: ' . $service->getClassName()); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// ------------------------------------------------------------------------ |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Services::add |
|
111
|
|
|
* |
|
112
|
|
|
* @param object $service |
|
113
|
|
|
* @param string|null $offset |
|
114
|
|
|
*/ |
|
115
|
|
|
public function add($service, $offset = null) |
|
116
|
|
|
{ |
|
117
|
|
|
if (is_object($service)) { |
|
118
|
|
|
if ( ! $service instanceof SplServiceRegistry) { |
|
119
|
|
|
$service = new SplServiceRegistry($service); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if (profiler() !== false) { |
|
124
|
|
|
profiler()->watch('Add New Service: ' . $service->getClassName()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->register($service, $offset); |
|
128
|
|
|
} |
|
129
|
|
|
} |