Hoa central
Core.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Core {
38 
42 !(
43  !defined('HOA') and define('HOA', true)
44 )
45 and
46  exit('Hoa main file (Core.php) must be included once.');
47 
48 (
49  !defined('PHP_VERSION_ID') or PHP_VERSION_ID < 50400
50 )
51 and
52  exit('Hoa needs at least PHP5.4 to work; you have ' . phpversion() . '.');
53 
57 require_once __DIR__ . DIRECTORY_SEPARATOR . 'Consistency.php';
58 
62 //require_once __DIR__ . DIRECTORY_SEPARATOR . 'Event.php';
63 
67 //require_once __DIR__ . DIRECTORY_SEPARATOR . 'Exception.php';
68 
72 require_once __DIR__ . DIRECTORY_SEPARATOR . 'Parameter.php';
73 
77 require_once __DIR__ . DIRECTORY_SEPARATOR . 'Protocol.php';
78 
82 //require_once __DIR__ . DIRECTORY_SEPARATOR . 'Data.php';
83 
92 class Core implements Parameter\Parameterizable
93 {
99  private static $_rsdf = [];
100 
106  private static $_root = null;
107 
113  protected $_parameters = null;
114 
120  private static $_instance = null;
121 
122 
123 
129  private function __construct()
130  {
131  static::_define('SUCCEED', true);
132  static::_define('FAILED', false);
133  static::_define('…', '__hoa_core_fill');
134  static::_define('DS', DIRECTORY_SEPARATOR);
135  static::_define('PS', PATH_SEPARATOR);
136  static::_define('CRLF', "\r\n");
137  static::_define('OS_WIN', defined('PHP_WINDOWS_VERSION_PLATFORM'));
138  static::_define('S_64_BITS', PHP_INT_SIZE == 8);
139  static::_define('S_32_BITS', !S_64_BITS);
140  static::_define('PHP_INT_MIN', ~PHP_INT_MAX);
141  static::_define('PHP_FLOAT_MIN', (float) PHP_INT_MIN);
142  static::_define('PHP_FLOAT_MAX', (float) PHP_INT_MAX);
143  static::_define('π', M_PI);
144  static::_define('void', (unset) null);
145  static::_define('_public', 1);
146  static::_define('_protected', 2);
147  static::_define('_private', 4);
148  static::_define('_static', 8);
149  static::_define('_abstract', 16);
150  static::_define('_pure', 32);
151  static::_define('_final', 64);
152  static::_define('_dynamic', ~_static);
153  static::_define('_concrete', ~_abstract);
154  static::_define('_overridable', ~_final);
155  static::_define('WITH_COMPOSER', class_exists('Composer\Autoload\ClassLoader', false) ||
156  ('cli' === PHP_SAPI &&
157  file_exists(__DIR__ . DS . '..' . DS . '..' . DS . 'autoload.php')));
158 
159  if (false !== $wl = ini_get('suhosin.executor.include.whitelist')) {
160  if (false === in_array('hoa', explode(',', $wl))) {
161  throw new Exception(
162  'The URL scheme hoa:// is not authorized by Suhosin. ' .
163  'You must add this to your php.ini or suhosin.ini: ' .
164  'suhosin.executor.include.whitelist="%s", thanks :-).',
165  0,
166  implode(
167  ',',
168  array_merge(
169  preg_split('#,#', $wl, -1, PREG_SPLIT_NO_EMPTY),
170  ['hoa']
171  )
172  )
173  );
174  }
175  }
176 
177  if (true === function_exists('mb_internal_encoding')) {
178  mb_internal_encoding('UTF-8');
179  }
180 
181  if (true === function_exists('mb_regex_encoding')) {
182  mb_regex_encoding('UTF-8');
183  }
184 
185  return;
186  }
187 
193  public static function getInstance()
194  {
195  if (null === static::$_instance) {
196  static::$_instance = new static();
197  }
198 
199  return static::$_instance;
200  }
201 
208  public function initialize(Array $parameters = [])
209  {
210  $root = dirname(dirname(__DIR__));
211  $cwd =
212  'cli' === PHP_SAPI
213  ? dirname(realpath($_SERVER['argv'][0]))
214  : getcwd();
215  $this->_parameters = new Parameter\Parameter(
216  $this,
217  [
218  'root' => $root,
219  'cwd' => $cwd
220  ],
221  [
222  'root.hoa' => '(:root:)',
223  'root.application' => '(:cwd:h:)',
224  'root.data' => '(:%root.application:h:)' . DS . 'Data' . DS,
225 
226  'protocol.Application' => '(:%root.application:)' . DS,
227  'protocol.Application/Public' => 'Public' . DS,
228  'protocol.Data' => '(:%root.data:)',
229  'protocol.Data/Etc' => 'Etc' . DS,
230  'protocol.Data/Etc/Configuration' => 'Configuration' . DS,
231  'protocol.Data/Etc/Locale' => 'Locale' . DS,
232  'protocol.Data/Library' => 'Library' . DS . 'Hoathis' . DS . ';' .
233  'Library' . DS . 'Hoa' . DS,
234  'protocol.Data/Lost+found' => 'Lost+found' . DS,
235  'protocol.Data/Temporary' => 'Temporary' . DS,
236  'protocol.Data/Variable' => 'Variable' . DS,
237  'protocol.Data/Variable/Cache' => 'Cache' . DS,
238  'protocol.Data/Variable/Database' => 'Database' . DS,
239  'protocol.Data/Variable/Log' => 'Log' . DS,
240  'protocol.Data/Variable/Private' => 'Private' . DS,
241  'protocol.Data/Variable/Run' => 'Run' . DS,
242  'protocol.Data/Variable/Test' => 'Test' . DS,
243  'protocol.Library' => '(:%protocol.Data:)Library' . DS . 'Hoathis' . DS . ';' .
244  '(:%protocol.Data:)Library' . DS . 'Hoa' . DS . ';' .
245  '(:%root.hoa:)' . DS . 'Hoathis' . DS . ';' .
246  '(:%root.hoa:)' . DS . 'Hoa' . DS,
247 
248  'namespace.prefix.*' => '(:%protocol.Data:)Library' . DS . ';(:%root.hoa:)' . DS,
249  'namespace.prefix.Application' => '(:%root.application:h:)' . DS,
250  ]
251  );
252 
253  $this->_parameters->setKeyword('root', $root);
254  $this->_parameters->setKeyword('cwd', $cwd);
255  $this->_parameters->setParameters($parameters);
256  $this->setProtocol();
257 
258  return $this;
259  }
260 
266  public function getParameters()
267  {
268  return $this->_parameters;
269  }
270 
278  public function setProtocol($path = null, $reach = null)
279  {
280  $root = static::getProtocol();
281 
282  if (null === $path && null === $reach) {
283  if (!isset($root['Library'])) {
284  static::$_root = null;
285  $root = static::getProtocol();
286  }
287 
288  $protocol = $this->getParameters()->unlinearizeBranche('protocol');
289 
290  foreach ($protocol as $components => $reach) {
291  $parts = explode('/', trim($components, '/'));
292  $last = array_pop($parts);
293  $handle = $root;
294 
295  foreach ($parts as $part) {
296  $handle = $handle[$part];
297  }
298 
299  if ('Library' === $last) {
300  $handle[] = new Protocol\Library($last, $reach);
301  } else {
302  $handle[] = new Protocol\Generic($last, $reach);
303  }
304  }
305 
306  return;
307  }
308 
309  if ('hoa://' === substr($path, 0, 6)) {
310  $path = substr($path, 6);
311  }
312 
313  $path = trim($path, '/');
314  $parts = explode('/', $path);
315  $handle = $root;
316 
317  foreach ($parts as $part) {
318  $handle = $handle[$part];
319  }
320 
321  $handle->setReach($reach);
322  $root->clearCache();
323  $this->getParameters()->setParameter('protocol.' . $path, $reach);
324 
325  return;
326  }
327 
338  public static function _define($name, $value, $case = false)
339  {
340  if (!defined($name)) {
341  return define($name, $value, $case);
342  }
343 
344  return false;
345  }
346 
352  public static function getProtocol()
353  {
354  if (null === static::$_root) {
355  static::$_root = new Protocol\Root();
356  }
357 
358  return static::$_root;
359  }
360 
367  public static function enableExceptionHandler($enable = true)
368  {
369  if (false === $enable) {
370  return restore_exception_handler();
371  }
372 
373  return set_exception_handler(function ($exception) {
374  return Exception\Idle::uncaught($exception);
375  });
376  }
377 
384  public static function enableErrorHandler($enable = true)
385  {
386  if (false === $enable) {
387  return restore_error_handler();
388  }
389 
390  return set_error_handler(function ($no, $str, $file = null, $line = null, $ctx = null) {
391  return Exception\Idle::error($no, $str, $file, $line, $ctx);
392  });
393  }
394 
404  public static function registerShutdownFunction($class = '', $method = '')
405  {
406  if (!isset(static::$_rsdf[$class][$method])) {
407  static::$_rsdf[$class][$method] = true;
408 
409  return register_shutdown_function([$class, $method]);
410  }
411 
412  return false;
413  }
414 
420  public static function getPHPBinary()
421  {
422  if (defined('PHP_BINARY')) {
423  return PHP_BINARY;
424  }
425 
426  if (isset($_SERVER['_'])) {
427  return $_SERVER['_'];
428  }
429 
430  foreach (['', '.exe'] as $extension) {
431  if (file_exists($_ = PHP_BINDIR . DS . 'php' . $extension)) {
432  return realpath($_);
433  }
434  }
435 
436  return null;
437  }
438 
444  public static function uuid()
445  {
446  return sprintf(
447  '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
448  mt_rand(0, 0xffff),
449  mt_rand(0, 0xffff),
450  mt_rand(0, 0xffff),
451  mt_rand(0, 0x0fff) | 0x4000,
452  mt_rand(0, 0x3fff) | 0x8000,
453  mt_rand(0, 0xffff),
454  mt_rand(0, 0xffff),
455  mt_rand(0, 0xffff)
456  );
457  }
458 
464  public static function ©()
465  {
466  return
467  'Copyright © 2007-2015 Ivan Enderlin. All rights reserved.' . "\n" .
468  'New BSD License.';
469  }
470 }
471 
472 }
473 
474 namespace {
475 
479 class_alias('Hoa\Core\Core', 'Hoa\Core');
480 
489 if (!function_exists('_define')) {
490  function _define($name, $value, $case = false)
491  {
492  return Hoa\Core::_define($name, $value, $case);
493  }
494 }
495 
502 if (!function_exists('event')) {
503  function event($eventId)
504  {
505  return Hoa\Core\Event\Event::getEvent($eventId);
506  }
507 }
508 
512 Hoa\Core::getInstance()->initialize();
513 
514 }
static ©()
Definition: Core.php:464
static getEvent($eventId)
Definition: Event.php:200
static enableErrorHandler($enable=true)
Definition: Core.php:384
static error($errno, $errstr, $errfile, $errline)
Definition: Exception.php:301
static getProtocol()
Definition: Core.php:352
static uncaught($exception)
Definition: Exception.php:271
static _define($name, $value, $case=false)
Definition: Core.php:338
static uuid()
Definition: Core.php:444
static getInstance()
Definition: Core.php:193
setProtocol($path=null, $reach=null)
Definition: Core.php:278
static enableExceptionHandler($enable=true)
Definition: Core.php:367
static getPHPBinary()
Definition: Core.php:420
initialize(Array $parameters=[])
Definition: Core.php:208
static registerShutdownFunction($class= '', $method= '')
Definition: Core.php:404
getParameters()
Definition: Core.php:266