forked from hoaproject/Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Core.php
523 lines (430 loc) · 15.2 KB
/
Core.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?php
/**
* Hoa
*
*
* @license
*
* New BSD License
*
* Copyright © 2007-2014, Ivan Enderlin. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Hoa nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
namespace {
/**
* Check if Hoa was well-included.
*/
!(
!defined('HOA') and define('HOA', true)
)
and
exit('Hoa main file (Core.php) must be included once.');
(
!defined('PHP_VERSION_ID') or PHP_VERSION_ID < 50303
)
and
exit('Hoa needs at least PHP5.3.3 to work; you have ' . phpversion() . '.');
/**
* \Hoa\Core\Consistency
*/
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Consistency.php';
/**
* \Hoa\Core\Event, (hard-preloaded in Hoa\Core\Consistency).
*/
//require_once __DIR__ . DIRECTORY_SEPARATOR . 'Event.php';
/**
* \Hoa\Core\Exception, (hard-preloaded in Hoa\Core\Consistency).
*/
//require_once __DIR__ . DIRECTORY_SEPARATOR . 'Exception.php';
/**
* \Hoa\Core\Parameter
*/
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Parameter.php';
/**
* \Hoa\Core\Protocol
*/
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Protocol.php';
/**
* \Hoa\Core\Data, (hard-preloaded in Hoa\Core\Consistency).
*/
//require_once __DIR__ . DIRECTORY_SEPARATOR . 'Data.php';
}
namespace Hoa\Core {
/**
* Class \Hoa\Core.
*
* \Hoa\Core is the base of all libraries.
*
* @author Ivan Enderlin <[email protected]>
* @copyright Copyright © 2007-2014 Ivan Enderlin.
* @license New BSD License
*/
class Core implements Parameter\Parameterizable {
/**
* Stack of all registered shutdown function.
*
* @var \Hoa\Core array
*/
private static $_rsdf = array();
/**
* Tree of components, starts by the root.
*
* @var \Hoa\Core\Protocol\Root object
*/
private static $_root = null;
/**
* Parameters.
*
* @var \Hoa\Core\Parameter object
*/
protected $_parameters = null;
/**
* Singleton.
*
* @var \Hoa\Core object
*/
private static $_instance = null;
/**
* Singleton.
*
* @access private
* @return void
*/
private function __construct ( ) {
static::_define('SUCCEED', true);
static::_define('FAILED', false);
static::_define('…', '__hoa_core_fill');
static::_define('DS', DIRECTORY_SEPARATOR);
static::_define('PS', PATH_SEPARATOR);
static::_define('CRLF', "\r\n");
static::_define('OS_WIN', defined('PHP_WINDOWS_VERSION_PLATFORM'));
static::_define('S_64_BITS', PHP_INT_SIZE == 8);
static::_define('S_32_BITS', !S_64_BITS);
static::_define('PHP_INT_MIN', ~PHP_INT_MAX);
static::_define('PHP_FLOAT_MIN', (float) PHP_INT_MIN);
static::_define('PHP_FLOAT_MAX', (float) PHP_INT_MAX);
static::_define('π', M_PI);
static::_define('void', (unset) null);
static::_define('_public', 1);
static::_define('_protected', 2);
static::_define('_private', 4);
static::_define('_static', 8);
static::_define('_abstract', 16);
static::_define('_pure', 32);
static::_define('_final', 64);
static::_define('_dynamic', ~_static);
static::_define('_concrete', ~_abstract);
static::_define('_overridable', ~_final);
static::_define('WITH_COMPOSER', class_exists('Composer\Autoload\ClassLoader', false)
|| ('cli' === PHP_SAPI
&& file_exists(__DIR__ . DS . '..' . DS . '..' . DS . 'autoload.php')));
if(false !== $wl = ini_get('suhosin.executor.include.whitelist'))
if(false === in_array('hoa', explode(',', $wl)))
throw new Exception(
'The URL scheme hoa:// is not authorized by Suhosin. ' .
'You must add this to your php.ini or suhosin.ini: ' .
'suhosin.executor.include.whitelist="%s", thanks :-).',
0, implode(',', array_merge(
preg_split('#,#', $wl, -1, PREG_SPLIT_NO_EMPTY),
array('hoa')
)));
$date = ini_get('date.timezone');
if(empty($date))
ini_set('date.timezone', 'Europe/Paris');
mb_internal_encoding('UTF-8');
if (function_exists('mb_regex_encoding')) {
mb_regex_encoding('UTF-8');
}
return;
}
/**
* Singleton.
*
* @access public
* @return \Hoa\Core
*/
public static function getInstance ( ) {
if(null === static::$_instance)
static::$_instance = new static();
return static::$_instance;
}
/**
* Initialize the core.
*
* @access public
* @param array $parameters Parameters of \Hoa\Core.
* @return \Hoa\Core
*/
public function initialize ( Array $parameters = array() ) {
$root = dirname(dirname(__DIR__));
if(WITH_COMPOSER)
$root = dirname(dirname($root));
$cwd = 'cli' === PHP_SAPI
? dirname(realpath($_SERVER['argv'][0]))
: getcwd();
$this->_parameters = new Parameter(
$this,
array(
'root' => $root,
'cwd' => $cwd
),
array(
'root.hoa' => '(:root:)',
'root.application' => '(:cwd:h:)',
'root.data' => '(:%root.application:h:)' . DS . 'Data',
'protocol.Application' => '(:%root.application:)' . DS,
'protocol.Application/Public' => 'Public' . DS,
'protocol.Data' => '(:%root.data:)' . DS,
'protocol.Data/Etc' => 'Etc' . DS,
'protocol.Data/Etc/Configuration' => 'Configuration' . DS,
'protocol.Data/Etc/Locale' => 'Locale' . DS,
'protocol.Data/Library' => 'Library' . DS . 'Hoathis' . DS . ';' .
'Library' . DS . 'Hoa' . DS,
'protocol.Data/Lost+found' => 'Lost+found' . DS,
'protocol.Data/Temporary' => 'Temporary' . DS,
'protocol.Data/Variable' => 'Variable' . DS,
'protocol.Data/Variable/Cache' => 'Cache' . DS,
'protocol.Data/Variable/Database' => 'Database' . DS,
'protocol.Data/Variable/Log' => 'Log' . DS,
'protocol.Data/Variable/Private' => 'Private' . DS,
'protocol.Data/Variable/Run' => 'Run' . DS,
'protocol.Data/Variable/Test' => 'Test' . DS,
'protocol.Library' => '(:%protocol.Data:)Library' . DS . 'Hoathis' . DS . ';' .
'(:%protocol.Data:)Library' . DS . 'Hoa' . DS . ';' .
'(:%root.hoa:)' . DS . 'Hoathis' . DS . ';' .
'(:%root.hoa:)' . DS . 'Hoa' . DS,
'namespace.prefix.*' => '(:%protocol.Data:)Library' . DS . ';(:%root.hoa:)' . DS,
'namespace.prefix.Application' => '(:%root.application:h:)' . DS,
)
);
$this->_parameters->setKeyword('root', $root);
$this->_parameters->setKeyword('cwd', $cwd);
$this->_parameters->setParameters($parameters);
$this->setProtocol();
return $this;
}
/**
* Get parameters.
*
* @access public
* @return \Hoa\Core\Parameter
*/
public function getParameters ( ) {
return $this->_parameters;
}
/**
* Set protocol according to the current parameter.
*
* @access public
* @param string $path Path (e.g. hoa://Data/Temporary).
* @param string $reach Reach value.
* @return void
*/
public function setProtocol ( $path = null, $reach = null ) {
$root = static::getProtocol();
if(null === $path && null === $reach) {
if(!isset($root['Library'])) {
static::$_root = null;
$root = static::getProtocol();
}
$protocol = $this->getParameters()->unlinearizeBranche('protocol');
foreach($protocol as $components => $reach) {
$parts = explode('/', trim($components, '/'));
$last = array_pop($parts);
$handle = $root;
foreach($parts as $part)
$handle = $handle[$part];
if('Library' === $last)
$handle[] = new Protocol\Library($last, $reach);
else
$handle[] = new Protocol\Generic($last, $reach);
}
return;
}
if('hoa://' === substr($path, 0, 6))
$path = substr($path, 6);
$path = trim($path, '/');
$parts = explode('/', $path);
$handle = $root;
foreach($parts as $part)
$handle = $handle[$part];
$handle->setReach($reach);
$root->clearCache();
$this->getParameters()->setParameter('protocol.' . $path, $reach);
return;
}
/**
* Check if a constant is already defined.
* If the constant is defined, this method returns false.
* Else this method declares the constant.
*
* @access public
* @param string $name The name of the constant.
* @param string $value The value of the constant.
* @param bool $case True set the case-insensitive.
* @return bool
*/
public static function _define ( $name, $value, $case = false) {
if(!defined($name))
return define($name, $value, $case);
return false;
}
/**
* Get protocol's root.
*
* @access public
* @return \Hoa\Core\Protocol\Root
*/
public static function getProtocol ( ) {
if(null === static::$_root)
static::$_root = new Protocol\Root();
return static::$_root;
}
/**
* Enable exception handler: catch uncaught exception.
*
* @access public
* @param bool $enable Enable.
* @return mixed
*/
public static function enableExceptionHandler ( $enable = true ) {
if(false === $enable)
return restore_exception_handler();
return set_exception_handler(function ( $exception ) {
return Exception\Idle::uncaught($exception);
});
}
/**
* Enable error handler: transform PHP error into \Hoa\Core\Exception\Error.
*
* @access public
* @param bool $enable Enable.
* @return mixed
*/
public static function enableErrorHandler ( $enable = true ) {
if(false === $enable)
return restore_error_handler();
return set_error_handler(function ( $no, $str, $file = null, $line = null, $ctx = null ) {
return Exception\Idle::error($no, $str, $file, $line, $ctx);
});
}
/**
* Apply and save a register shutdown function.
* It may be analogous to a static __destruct, but it allows us to make more
* that a __destruct method.
*
* @access public
* @param string $class Class.
* @param string $method Method.
* @return bool
*/
public static function registerShutdownFunction ( $class = '', $method = '' ) {
if(!isset(static::$_rsdf[$class][$method])) {
static::$_rsdf[$class][$method] = true;
return register_shutdown_function(array($class, $method));
}
return false;
}
/**
* Get PHP executable.
*
* @access public
* @return string
*/
public static function getPHPBinary ( ) {
if(defined('PHP_BINARY'))
return PHP_BINARY;
if(isset($_SERVER['_']))
return $_SERVER['_'];
foreach(array('', '.exe') as $extension)
if(file_exists($_ = PHP_BINDIR . DS . 'php' . $extension))
return realpath($_);
return null;
}
/**
* Generate an Universal Unique Identifier (UUID).
*
* @access public
* @return void
*/
public static function uuid ( ) {
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
}
/**
* Return the copyright and license of Hoa.
*
* @access public
* @return string
*/
public static function © ( ) {
return 'Copyright © 2007-2014 Ivan Enderlin. All rights reserved.' . "\n" .
'New BSD License.';
}
}
}
namespace {
/**
* Alias.
*/
class_alias('Hoa\Core\Core', 'Hoa\Core');
/**
* Alias of \Hoa\Core::_define().
*
* @access public
* @param string $name The name of the constant.
* @param string $value The value of the constant.
* @param bool $case True set the case-insentisitve.
* @return bool
*/
if(!ƒ('_define')) {
function _define ( $name, $value, $case = false ) {
return \Hoa\Core::_define($name, $value, $case);
}}
/**
* Alias of the \Hoa\Core\Event::getEvent() method.
*
* @access public
* @param string $eventId Event ID.
* @return \Hoa\Core\Event
*/
if(!ƒ('event')) {
function event ( $eventId ) {
return \Hoa\Core\Event\Event::getEvent($eventId);
}}
/**
* Then, initialize Hoa.
*/
\Hoa\Core::getInstance()->initialize();
}