1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ICanBoogie package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ICanBoogie\Binding\ActiveRecord; |
13
|
|
|
|
14
|
|
|
use ICanBoogie\ActiveRecord; |
15
|
|
|
use ICanBoogie\ActiveRecord\Connection; |
16
|
|
|
use ICanBoogie\ActiveRecord\ConnectionCollection; |
17
|
|
|
use ICanBoogie\ActiveRecord\Model; |
18
|
|
|
use ICanBoogie\ActiveRecord\ModelCollection; |
19
|
|
|
use ICanBoogie\ActiveRecord\RuntimeActiveRecordCache; |
20
|
|
|
use ICanBoogie\Core; |
21
|
|
|
use ICanBoogie\Validate\ValidationErrors; |
22
|
|
|
|
23
|
|
|
class Hooks |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Synthesizes a config from a namespace. |
27
|
|
|
* |
28
|
|
|
* The config fragments found in the namespace are merged with `array_merge()`. |
29
|
|
|
* |
30
|
|
|
* @param array $fragments |
31
|
|
|
* @param string $namespace |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
static private function synthesize_config_from_namespace(array $fragments, $namespace) |
36
|
|
|
{ |
37
|
|
|
$config = []; |
38
|
|
|
|
39
|
|
|
foreach ($fragments as $fragment) |
40
|
|
|
{ |
41
|
|
|
if (empty($fragment[$namespace])) |
42
|
|
|
{ |
43
|
|
|
continue; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$config[] = $fragment[$namespace]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $config ? call_user_func_array('array_merge', $config) : []; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Synthesizes the `activerecord_connections` config from `activerecord#connections` fragments. |
54
|
|
|
* |
55
|
|
|
* @param array $fragments |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
static public function synthesize_connections_config(array $fragments) |
60
|
|
|
{ |
61
|
|
|
return self::synthesize_config_from_namespace($fragments, 'connections'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Synthesizes the `activerecord_models` config from `activerecord#models` fragments. |
66
|
|
|
* |
67
|
|
|
* @param array $fragments |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
static public function synthesize_models_config(array $fragments) |
72
|
|
|
{ |
73
|
|
|
return self::synthesize_config_from_namespace($fragments, 'models'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/* |
77
|
|
|
* Events |
78
|
|
|
*/ |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Patches the `get_model()` helper to use the model collection bound to the application. |
82
|
|
|
* |
83
|
|
|
* @param Core\BootEvent $event |
84
|
|
|
* @param Core|CoreBindings $app |
|
|
|
|
85
|
|
|
*/ |
86
|
|
|
static public function on_core_boot(Core\BootEvent $event, Core $app) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
ActiveRecord\Helpers::patch('get_model', function($id) use ($app) { |
89
|
|
|
|
90
|
|
|
return $app->models[$id]; |
91
|
|
|
|
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/* |
96
|
|
|
* Prototypes |
97
|
|
|
*/ |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns a @{link ConnectionCollection} instance configured with |
101
|
|
|
* the `activerecord_connections` config. |
102
|
|
|
* |
103
|
|
|
* @param Core $app |
104
|
|
|
* |
105
|
|
|
* @return ConnectionCollection |
106
|
|
|
*/ |
107
|
|
|
static public function core_lazy_get_connections(Core $app) |
108
|
|
|
{ |
109
|
|
|
static $connections; |
110
|
|
|
|
111
|
|
|
if (!$connections) |
112
|
|
|
{ |
113
|
|
|
$connections = new ConnectionCollection($app->configs['activerecord_connections'] ?: []); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $connections; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns a @{link ModelCollection} instance configured with |
121
|
|
|
* the `activerecord_models` config. |
122
|
|
|
* |
123
|
|
|
* @param Core|CoreBindings $app |
|
|
|
|
124
|
|
|
* |
125
|
|
|
* @return ModelCollection |
126
|
|
|
*/ |
127
|
|
|
static public function core_lazy_get_models(Core $app) |
128
|
|
|
{ |
129
|
|
|
static $models; |
130
|
|
|
|
131
|
|
|
if (!$models) |
132
|
|
|
{ |
133
|
|
|
$models = new ModelCollection($app->connections, $app->configs['activerecord_models'] ?: []); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $models; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Getter for the "primary" database connection. |
141
|
|
|
* |
142
|
|
|
* @param Core|CoreBindings $app |
|
|
|
|
143
|
|
|
* |
144
|
|
|
* @return Connection |
145
|
|
|
*/ |
146
|
|
|
static public function core_lazy_get_db(Core $app) |
147
|
|
|
{ |
148
|
|
|
return $app->connections['primary']; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param ActiveRecord $record |
153
|
|
|
* |
154
|
|
|
* @return ValidationErrors|array |
155
|
|
|
*/ |
156
|
|
|
static public function active_record_validate(ActiveRecord $record) |
157
|
|
|
{ |
158
|
|
|
static $validate; |
159
|
|
|
|
160
|
|
|
if (!$validate) |
161
|
|
|
{ |
162
|
|
|
$validate = new ActiveRecord\Validate\ValidateActiveRecord; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $validate($record); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns the records cache associated with the model. |
170
|
|
|
* |
171
|
|
|
* @param Model $model |
172
|
|
|
* |
173
|
|
|
* @return RuntimeActiveRecordCache |
174
|
|
|
*/ |
175
|
|
|
static public function model_lazy_get_activerecord_cache(Model $model) |
176
|
|
|
{ |
177
|
|
|
return new RuntimeActiveRecordCache($model); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|