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\Framework\Containers; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Database; |
19
|
|
|
use O2System\Spl\Containers\DataStructures\SplServiceRegistry; |
20
|
|
|
use O2System\Spl\Containers\SplServiceContainer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Models |
24
|
|
|
* |
25
|
|
|
* @package O2System\Framework\Containers |
26
|
|
|
*/ |
27
|
|
|
class Models extends SplServiceContainer |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Models::$database |
31
|
|
|
* |
32
|
|
|
* @var \O2System\Database\Connections |
33
|
|
|
*/ |
34
|
|
|
public $database; |
35
|
|
|
|
36
|
|
|
// ------------------------------------------------------------------------ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Models::__construct |
40
|
|
|
*/ |
41
|
|
|
public function __construct() |
42
|
|
|
{ |
43
|
|
|
if ($config = config()->loadFile('database', true)) { |
|
|
|
|
44
|
|
|
if ( ! empty($config[ 'default' ][ 'hostname' ]) AND ! empty($config[ 'default' ][ 'username' ])) { |
45
|
|
|
|
46
|
|
|
if (profiler() !== false) { |
47
|
|
|
profiler()->watch('Starting Database Service'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->database = new Database\Connections( |
51
|
|
|
new Database\DataStructures\Config( |
52
|
|
|
$config->getArrayCopy() |
53
|
|
|
) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// ------------------------------------------------------------------------ |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Models::load |
63
|
|
|
* |
64
|
|
|
* @param object|string $model |
65
|
|
|
* @param string|null $offset |
66
|
|
|
*/ |
67
|
|
|
public function load($model, $offset = null) |
68
|
|
|
{ |
69
|
|
|
if (is_string($model)) { |
70
|
|
|
if (class_exists($model)) { |
71
|
|
|
$service = new SplServiceRegistry($model); |
72
|
|
|
} |
73
|
|
|
} elseif ($model instanceof SplServiceRegistry) { |
74
|
|
|
$service = $model; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (isset($service) && $service instanceof SplServiceRegistry) { |
78
|
|
|
if (profiler() !== false) { |
79
|
|
|
profiler()->watch('Load New Model: ' . $service->getClassName()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->register($service, $offset); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// ------------------------------------------------------------------------ |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Models::register |
90
|
|
|
* |
91
|
|
|
* @param SplServiceRegistry $service |
92
|
|
|
* @param string|null $offset |
93
|
|
|
*/ |
94
|
|
|
public function register(SplServiceRegistry $service, $offset = null) |
95
|
|
|
{ |
96
|
|
|
if ($service instanceof SplServiceRegistry) { |
|
|
|
|
97
|
|
|
$offset = isset($offset) |
98
|
|
|
? $offset |
99
|
|
|
: camelcase($service->getParameter()); |
100
|
|
|
|
101
|
|
|
if ($service->isSubclassOf('O2System\Framework\Models\Sql\Model') || |
102
|
|
|
$service->isSubclassOf('O2System\Framework\Models\NoSql\Model') || |
103
|
|
|
$service->isSubclassOf('O2System\Framework\Models\Files\Model') |
104
|
|
|
) { |
105
|
|
|
$this->attach($offset, $service); |
106
|
|
|
|
107
|
|
|
if (profiler() !== false) { |
108
|
|
|
profiler()->watch('Register New Model: ' . $service->getClassName()); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// ------------------------------------------------------------------------ |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Models::add |
118
|
|
|
* |
119
|
|
|
* @param \O2System\Framework\Models\Sql\Model|\O2System\Framework\Models\NoSql\Model|\O2System\Framework\Models\Files\Model $model |
120
|
|
|
* @param null $offset |
|
|
|
|
121
|
|
|
*/ |
122
|
|
|
public function add($model, $offset = null) |
123
|
|
|
{ |
124
|
|
|
if (is_object($model)) { |
125
|
|
|
if ( ! $model instanceof SplServiceRegistry) { |
|
|
|
|
126
|
|
|
$model = new SplServiceRegistry($model); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (profiler() !== false) { |
131
|
|
|
profiler()->watch('Add New Model: ' . $model->getClassName()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->register($model, $offset); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// ------------------------------------------------------------------------ |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Models::autoload |
141
|
|
|
* |
142
|
|
|
* @param string $model |
143
|
|
|
* @param string|null $offset |
144
|
|
|
* |
145
|
|
|
* @return mixed |
146
|
|
|
*/ |
147
|
|
|
public function autoload($model, $offset = null) |
148
|
|
|
{ |
149
|
|
|
if (isset($offset)) { |
150
|
|
|
if ($this->has($offset)) { |
151
|
|
|
return $this->get($offset); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// Try to load |
155
|
|
|
if (is_string($model)) { |
|
|
|
|
156
|
|
|
if ($this->has($model)) { |
157
|
|
|
return $this->get($model); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->load($model, $offset); |
161
|
|
|
|
162
|
|
|
if ($this->has($offset)) { |
163
|
|
|
return $this->get($offset); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} elseif (is_string($model)) { |
|
|
|
|
167
|
|
|
if ($this->has($model)) { |
168
|
|
|
return $this->get($model); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// Try to load |
172
|
|
|
$this->load($model, $model); |
173
|
|
|
|
174
|
|
|
if ($this->has($model)) { |
175
|
|
|
return $this->get($model); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.