123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- <?php
- namespace Hoa\Consistency
- {
- /**
- * Class Hoa\Consistency\Consistency.
- *
- * This class is a collection of tools to ensure foreward and backward
- * compatibility.
- *
- * @copyright Copyright © 2007-2017 Hoa community
- * @license New BSD License
- */
- class Consistency
- {
- /**
- * Check if an entity exists (class, interface, trait…).
- *
- * @param string $entityName Entity name.
- * @param bool $autoloader Run autoloader if necessary.
- * @return bool
- */
- public static function entityExists($entityName, $autoloader = false)
- {
- return
- class_exists($entityName, $autoloader) ||
- interface_exists($entityName, false) ||
- trait_exists($entityName, false);
- }
-
- public static function getEntityShortestName($entityName)
- {
- $parts = explode('\\', $entityName);
- $count = count($parts);
- if (1 >= $count) {
- return $entityName;
- }
- if ($parts[$count - 2] === $parts[$count - 1]) {
- return implode('\\', array_slice($parts, 0, -1));
- }
- return $entityName;
- }
-
- public static function flexEntity($entityName)
- {
- return class_alias(
- $entityName,
- static::getEntityShortestName($entityName),
- false
- );
- }
-
- public static function isKeyword($word)
- {
- static $_list = [
-
- '__halt_compiler',
- 'abstract',
- 'and',
- 'array',
- 'as',
- 'bool',
- 'break',
- 'callable',
- 'case',
- 'catch',
- 'class',
- 'clone',
- 'const',
- 'continue',
- 'declare',
- 'default',
- 'die',
- 'do',
- 'echo',
- 'else',
- 'elseif',
- 'empty',
- 'enddeclare',
- 'endfor',
- 'endforeach',
- 'endif',
- 'endswitch',
- 'endwhile',
- 'eval',
- 'exit',
- 'extends',
- 'false',
- 'final',
- 'float',
- 'for',
- 'foreach',
- 'function',
- 'global',
- 'goto',
- 'if',
- 'implements',
- 'include',
- 'include_once',
- 'instanceof',
- 'insteadof',
- 'int',
- 'interface',
- 'isset',
- 'list',
- 'mixed',
- 'namespace',
- 'new',
- 'null',
- 'numeric',
- 'object',
- 'or',
- 'print',
- 'private',
- 'protected',
- 'public',
- 'require',
- 'require_once',
- 'resource',
- 'return',
- 'static',
- 'string',
- 'switch',
- 'throw',
- 'trait',
- 'true',
- 'try',
- 'unset',
- 'use',
- 'var',
- 'void',
- 'while',
- 'xor',
- 'yield',
-
- '__class__',
- '__dir__',
- '__file__',
- '__function__',
- '__line__',
- '__method__',
- '__namespace__',
- '__trait__'
- ];
- return in_array(strtolower($word), $_list);
- }
-
- public static function isIdentifier($id)
- {
- return 0 !== preg_match(
- '#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x80-\xff]*$#',
- $id
- );
- }
-
- public static function registerShutdownFunction($callable)
- {
- return register_shutdown_function($callable);
- }
-
- public static function getPHPBinary()
- {
- if (defined('PHP_BINARY')) {
- return PHP_BINARY;
- }
- if (isset($_SERVER['_'])) {
- return $_SERVER['_'];
- }
- foreach (['', '.exe'] as $extension) {
- if (file_exists($_ = PHP_BINDIR . DS . 'php' . $extension)) {
- return realpath($_);
- }
- }
- return null;
- }
-
- 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)
- );
- }
- }
- }
- namespace
- {
- if (70000 > PHP_VERSION_ID && false === interface_exists('Throwable', false)) {
- /**
- * Implement a fake Throwable class, introduced in PHP7.0.
- */
- interface Throwable
- {
- public function getMessage();
- public function getCode();
- public function getFile();
- public function getLine();
- public function getTrace();
- public function getPrevious();
- public function getTraceAsString();
- public function __toString();
- }
- }
- if (50600 > PHP_VERSION_ID) {
- $define = function ($constantName, $constantValue, $case = false) {
- if (!defined($constantName)) {
- return define($constantName, $constantValue, $case);
- }
- return false;
- };
- $define('STREAM_CRYPTO_METHOD_TLSv1_0_SERVER', 8);
- $define('STREAM_CRYPTO_METHOD_TLSv1_1_SERVER', 16);
- $define('STREAM_CRYPTO_METHOD_TLSv1_2_SERVER', 32);
- $define('STREAM_CRYPTO_METHOD_ANY_SERVER', 62);
- $define('STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT', 9);
- $define('STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT', 17);
- $define('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT', 33);
- $define('STREAM_CRYPTO_METHOD_ANY_CLIENT', 63);
- }
- if (!function_exists('curry')) {
-
- function curry($callable)
- {
- $arguments = func_get_args();
- array_shift($arguments);
- $ii = array_keys($arguments, …, true);
- return function () use ($callable, $arguments, $ii) {
- return call_user_func_array(
- $callable,
- array_replace($arguments, array_combine($ii, func_get_args()))
- );
- };
- }
- }
- Hoa\Consistency\Consistency::flexEntity('Hoa\Consistency\Consistency');
- }
|