|
1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* The basic class for managing services |
|
5
|
|
|
* |
|
6
|
|
|
* Access services with Service::factory() |
|
7
|
|
|
* |
|
8
|
|
|
* Each service has will be initialiized on first factory() invocation only. |
|
9
|
|
|
* If the service is "disabled" it will not rise exceptions, just not work |
|
10
|
|
|
* |
|
11
|
|
|
* @package Despark/services-manager |
|
12
|
|
|
* @author Ivan Kerin |
|
13
|
|
|
* @copyright (c) 2012 Despark Ltd. |
|
14
|
|
|
* @license http://creativecommons.org/licenses/by-sa/3.0/legalcode |
|
15
|
|
|
*/ |
|
16
|
|
|
class Kohana_Service |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Caching services |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
static public $services = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The cached config file, can be manipulated with the setter |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $_config = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* If this is false the service will not be initialized, but all of it public methods should still be accessible |
|
34
|
|
|
* |
|
35
|
|
|
* @var boolean |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $_enabled = TRUE; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Whether the service has been initialized, based on this attributes runs init() only once |
|
41
|
|
|
* |
|
42
|
|
|
* @var boolean |
|
43
|
|
|
*/ |
|
44
|
|
|
private $_initialized = FALSE; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the service. Configuration is in the services-manager under the same name. |
|
48
|
|
|
* The driver name is named the same as the service name |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $service_name |
|
51
|
|
|
* @return Service |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function factory($service_name) |
|
54
|
|
|
{ |
|
55
|
|
|
if ( ! isset(Service::$services[$service_name])) |
|
56
|
|
|
{ |
|
57
|
|
|
$class = 'Service_'.Service::capitalize_class_name($service_name); |
|
58
|
|
|
|
|
59
|
|
|
if ( ! class_exists($class)) |
|
60
|
|
|
throw new Kohana_Exception('Service :name with class :class does not exist', array(':name' => $service_name, ':class' => $class)); |
|
61
|
|
|
|
|
62
|
|
|
Service::$services[$service_name] = new $class($service_name); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return Service::$services[$service_name]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public static function capitalize_class_name($class_name) |
|
69
|
|
|
{ |
|
70
|
|
|
return str_replace(' ', '_', ucwords(str_replace('_', ' ', $class_name))); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Getter / setter for enabled attribute |
|
75
|
|
|
* |
|
76
|
|
|
* @param bool $enabled |
|
77
|
|
|
* @return bool|$this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function enabled($enabled = NULL) |
|
80
|
|
|
{ |
|
81
|
|
|
if ($enabled !== NULL) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->_enabled = (bool) $enabled; |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->_enabled; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* View::capture clone as it is a protected kohana method |
|
93
|
|
|
* @param string $file the file to render the contents of |
|
94
|
|
|
* @param array $variables array of varaiables to be included as local |
|
95
|
|
|
* @return string the rendered file |
|
96
|
|
|
*/ |
|
97
|
|
|
public function render_file($file, array $variables = array()) |
|
98
|
|
|
{ |
|
99
|
|
|
// Import the view variables to local namespace |
|
100
|
|
|
extract($variables, EXTR_SKIP); |
|
101
|
|
|
|
|
102
|
|
|
// Capture the view output |
|
103
|
|
|
ob_start(); |
|
104
|
|
|
|
|
105
|
|
|
try |
|
106
|
|
|
{ |
|
107
|
|
|
// Load the view within the current scope |
|
108
|
|
|
include $file; |
|
109
|
|
|
} |
|
110
|
|
|
catch (Exception $e) |
|
111
|
|
|
{ |
|
112
|
|
|
// Delete the output buffer |
|
113
|
|
|
ob_end_clean(); |
|
114
|
|
|
|
|
115
|
|
|
// Re-throw the exception |
|
116
|
|
|
throw $e; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// Get the captured output and close the buffer |
|
120
|
|
|
return ob_get_clean(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Getter / setter for config. |
|
125
|
|
|
* If you pass an array, merges it with the current configuraton |
|
126
|
|
|
* If you pass a string returns the config with the specified key |
|
127
|
|
|
* |
|
128
|
|
|
* @param [type] $config [description] |
|
|
|
|
|
|
129
|
|
|
* @return [type] |
|
|
|
|
|
|
130
|
|
|
*/ |
|
131
|
|
|
public function config($config = NULL) |
|
132
|
|
|
{ |
|
133
|
|
|
if (is_string($config)) |
|
134
|
|
|
{ |
|
135
|
|
|
return Arr::path($this->_config, $config); |
|
136
|
|
|
} |
|
137
|
|
|
elseif ($config !== NULL) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->_config = Arr::merge($this->_config, (array) $config); |
|
140
|
|
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $this->_config; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
function __construct($service_name) |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
|
|
$this->_config = Kohana::$config->load('services-manager.services.'.$service_name); |
|
|
|
|
|
|
149
|
|
|
$this->_enabled = Arr::get($this->_config, 'enabled'); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Initialize the service, if it's a php service, the library will be loaded here |
|
154
|
|
|
* |
|
155
|
|
|
* @return NULL |
|
156
|
|
|
*/ |
|
157
|
|
|
public function init() |
|
158
|
|
|
{ |
|
159
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Check if the service has been initialized, and if not, run init(), return FALSE if disabled |
|
164
|
|
|
* |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
|
|
public function initialized() |
|
168
|
|
|
{ |
|
169
|
|
|
if ($this->_initialized) |
|
170
|
|
|
return TRUE; |
|
171
|
|
|
|
|
172
|
|
|
if ($this->enabled()) |
|
173
|
|
|
{ |
|
174
|
|
|
if (PHP_SAPI != 'cli' AND ( ! $this->is_enabled_for_user() OR ! $this->is_enabled_for_robots())) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->enabled(FALSE); |
|
177
|
|
|
|
|
178
|
|
|
return FALSE; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$this->init(); |
|
182
|
|
|
$this->_initialized = TRUE; |
|
183
|
|
|
|
|
184
|
|
|
return TRUE; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return FALSE; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
public function is_enabled_for_robots() |
|
192
|
|
|
{ |
|
193
|
|
|
if (Request::initial() AND Arr::get($this->_config, 'disabled-for-robots')) |
|
194
|
|
|
{ |
|
195
|
|
|
return strpos(Request::initial()->user_agent('robot'), 'Googlebot') === FALSE; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return TRUE; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function is_enabled_for_user() |
|
202
|
|
|
{ |
|
203
|
|
|
if ($role = Arr::get($this->_config, 'disabled-for-role')) |
|
204
|
|
|
{ |
|
205
|
|
|
return ! Auth::instance()->logged_in($role); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
if ($role = Arr::get($this->_config, 'enabled-for-role')) |
|
209
|
|
|
{ |
|
210
|
|
|
return Auth::instance()->logged_in($role); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return TRUE; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
static public function disable_all() |
|
217
|
|
|
{ |
|
218
|
|
|
$services = func_get_args(); |
|
219
|
|
|
|
|
220
|
|
|
if (empty($services)) |
|
221
|
|
|
{ |
|
222
|
|
|
$services = Service::names(); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
foreach ($services as $service_name) |
|
226
|
|
|
{ |
|
227
|
|
|
Service::factory($service_name)->enabled(FALSE); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
static public function enable_all() |
|
232
|
|
|
{ |
|
233
|
|
|
$services = func_get_args(); |
|
234
|
|
|
|
|
235
|
|
|
if (empty($services)) |
|
236
|
|
|
{ |
|
237
|
|
|
$services = Service::names(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
foreach ($services as $service_name) |
|
241
|
|
|
{ |
|
242
|
|
|
Service::factory($service_name)->enabled(TRUE); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
static public function names() |
|
248
|
|
|
{ |
|
249
|
|
|
return array_keys(Kohana::$config->load('services-manager.services')); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Render enabled javascript services, you can specify a list of services to load, otherwise renders all of them |
|
254
|
|
|
* |
|
255
|
|
|
* @return string |
|
256
|
|
|
*/ |
|
257
|
|
|
static public function all_bodies() |
|
258
|
|
|
{ |
|
259
|
|
|
$services = func_get_args(); |
|
260
|
|
|
|
|
261
|
|
|
if (empty($services)) |
|
262
|
|
|
{ |
|
263
|
|
|
$services = Service::names(); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
$bodies = array(); |
|
267
|
|
|
|
|
268
|
|
|
foreach ($services as $service_name) |
|
269
|
|
|
{ |
|
270
|
|
|
$service = Service::factory($service_name); |
|
271
|
|
|
|
|
272
|
|
|
if ($service instanceof Service_Type_Javascript) |
|
273
|
|
|
{ |
|
274
|
|
|
$bodies[] = $service->body(); |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
return implode("\n", array_filter($bodies)); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
static public function all_heads() |
|
282
|
|
|
{ |
|
283
|
|
|
$services = func_get_args(); |
|
284
|
|
|
|
|
285
|
|
|
if (empty($services)) |
|
286
|
|
|
{ |
|
287
|
|
|
$services = Service::names(); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
$headers = array(); |
|
291
|
|
|
|
|
292
|
|
|
foreach ($services as $service_name) |
|
293
|
|
|
{ |
|
294
|
|
|
$service = Service::factory($service_name); |
|
295
|
|
|
|
|
296
|
|
|
if ($service instanceof Service_Type_Javascript) |
|
297
|
|
|
{ |
|
298
|
|
|
$headers[] = $service->head(); |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
return implode("\n", $headers); |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.