| @@ -1,101 +1,101 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace FL\Assistant\System\Container; | |
| 4 | - | |
| 5 | -class CachingReflector implements Reflector { | |
| 6 | - | |
| 7 | - const CACHE_KEY_CLASSES = 'auryn.refls.classes.'; | |
| 8 | - const CACHE_KEY_CTORS = 'auryn.refls.ctors.'; | |
| 9 | - const CACHE_KEY_CTOR_PARAMS = 'auryn.refls.ctor-params.'; | |
| 10 | - const CACHE_KEY_FUNCS = 'auryn.refls.funcs.'; | |
| 11 | - const CACHE_KEY_METHODS = 'auryn.refls.methods.'; | |
| 12 | - | |
| 13 | - private $reflector; | |
| 14 | - private $cache; | |
| 15 | - | |
| 16 | - public function __construct( ?Reflector $reflector = null, ?ReflectionCache $cache = null ) { | |
| 17 | - $this->reflector = $reflector ?: new StandardReflector; | |
| 18 | - $this->cache = $cache ?: new ReflectionCacheArray; | |
| 19 | - } | |
| 20 | - | |
| 21 | - public function getClass( $class ) { | |
| 22 | - $cacheKey = self::CACHE_KEY_CLASSES . strtolower( $class ); | |
| 23 | - | |
| 24 | - if ( ( $reflectionClass = $this->cache->fetch( $cacheKey ) ) === false ) { | |
| 25 | - $this->cache->store( $cacheKey, $reflectionClass = $this->reflector->getClass( $class ) ); | |
| 26 | - } | |
| 27 | - | |
| 28 | - return $reflectionClass; | |
| 29 | - } | |
| 30 | - | |
| 31 | - public function getCtor( $class ) { | |
| 32 | - $cacheKey = self::CACHE_KEY_CTORS . strtolower( $class ); | |
| 33 | - | |
| 34 | - if ( ( $reflectedCtor = $this->cache->fetch( $cacheKey ) ) === false ) { | |
| 35 | - $this->cache->store( $cacheKey, $reflectedCtor = $this->reflector->getCtor( $class ) ); | |
| 36 | - } | |
| 37 | - | |
| 38 | - return $reflectedCtor; | |
| 39 | - } | |
| 40 | - | |
| 41 | - public function getCtorParams( $class ) { | |
| 42 | - $cacheKey = self::CACHE_KEY_CTOR_PARAMS . strtolower( $class ); | |
| 43 | - | |
| 44 | - if ( ( $reflectedCtorParams = $this->cache->fetch( $cacheKey ) ) === false ) { | |
| 45 | - $this->cache->store( $cacheKey, $reflectedCtorParams = $this->reflector->getCtorParams( $class ) ); | |
| 46 | - } | |
| 47 | - | |
| 48 | - return $reflectedCtorParams; | |
| 49 | - } | |
| 50 | - | |
| 51 | - public function getParamTypeHint( \ReflectionFunctionAbstract $function, \ReflectionParameter $param ) { | |
| 52 | - $lowParam = strtolower( $param->name ); | |
| 53 | - | |
| 54 | - if ( $function instanceof \ReflectionMethod ) { | |
| 55 | - $lowClass = strtolower( $function->class ); | |
| 56 | - $lowMethod = strtolower( $function->name ); | |
| 57 | - $paramCacheKey = self::CACHE_KEY_CLASSES . "{$lowClass}.{$lowMethod}.param-{$lowParam}"; | |
| 58 | - } else { | |
| 59 | - $lowFunc = strtolower( $function->name ); | |
| 60 | - $paramCacheKey = ( strpos( $lowFunc, '{closure}' ) === false ) | |
| 61 | - ? self::CACHE_KEY_FUNCS . ".{$lowFunc}.param-{$lowParam}" | |
| 62 | - : null; | |
| 63 | - } | |
| 64 | - | |
| 65 | - $typeHint = ( $paramCacheKey === null ) ? false : $this->cache->fetch( $paramCacheKey ); | |
| 66 | - | |
| 67 | - if ( false === $typeHint ) { | |
| 68 | - $typeHint = $this->reflector->getParamTypeHint( $function, $param ); | |
| 69 | - if ( $paramCacheKey !== null ) { | |
| 70 | - $this->cache->store( $paramCacheKey, $typeHint ); | |
| 71 | - } | |
| 72 | - } | |
| 73 | - | |
| 74 | - return $typeHint; | |
| 75 | - } | |
| 76 | - | |
| 77 | - public function getFunction( $functionName ) { | |
| 78 | - $lowFunc = strtolower( $functionName ); | |
| 79 | - $cacheKey = self::CACHE_KEY_FUNCS . $lowFunc; | |
| 80 | - | |
| 81 | - if ( ( $reflectedFunc = $this->cache->fetch( $cacheKey ) ) === false ) { | |
| 82 | - $this->cache->store( $cacheKey, $reflectedFunc = $this->reflector->getFunction( $functionName ) ); | |
| 83 | - } | |
| 84 | - | |
| 85 | - return $reflectedFunc; | |
| 86 | - } | |
| 87 | - | |
| 88 | - public function getMethod( $classNameOrInstance, $methodName ) { | |
| 89 | - $className = is_string( $classNameOrInstance ) | |
| 90 | - ? $classNameOrInstance | |
| 91 | - : get_class( $classNameOrInstance ); | |
| 92 | - | |
| 93 | - $cacheKey = self::CACHE_KEY_METHODS . strtolower( $className ) . '.' . strtolower( $methodName ); | |
| 94 | - | |
| 95 | - if ( ( $reflectedMethod = $this->cache->fetch( $cacheKey ) ) === false ) { | |
| 96 | - $this->cache->store( $cacheKey, $reflectedMethod = $this->reflector->getMethod( $classNameOrInstance, $methodName ) ); | |
| 97 | - } | |
| 98 | - | |
| 99 | - return $reflectedMethod; | |
| 100 | - } | |
| 101 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace FL\Assistant\System\Container; | |
| 4 | + | |
| 5 | +class CachingReflector implements Reflector { | |
| 6 | + | |
| 7 | + const CACHE_KEY_CLASSES = 'auryn.refls.classes.'; | |
| 8 | + const CACHE_KEY_CTORS = 'auryn.refls.ctors.'; | |
| 9 | + const CACHE_KEY_CTOR_PARAMS = 'auryn.refls.ctor-params.'; | |
| 10 | + const CACHE_KEY_FUNCS = 'auryn.refls.funcs.'; | |
| 11 | + const CACHE_KEY_METHODS = 'auryn.refls.methods.'; | |
| 12 | + | |
| 13 | + private $reflector; | |
| 14 | + private $cache; | |
| 15 | + | |
| 16 | + public function __construct( Reflector $reflector = null, ReflectionCache $cache = null ) { | |
| 17 | + $this->reflector = $reflector ?: new StandardReflector; | |
| 18 | + $this->cache = $cache ?: new ReflectionCacheArray; | |
| 19 | + } | |
| 20 | + | |
| 21 | + public function getClass( $class ) { | |
| 22 | + $cacheKey = self::CACHE_KEY_CLASSES . strtolower( $class ); | |
| 23 | + | |
| 24 | + if ( ( $reflectionClass = $this->cache->fetch( $cacheKey ) ) === false ) { | |
| 25 | + $this->cache->store( $cacheKey, $reflectionClass = $this->reflector->getClass( $class ) ); | |
| 26 | + } | |
| 27 | + | |
| 28 | + return $reflectionClass; | |
| 29 | + } | |
| 30 | + | |
| 31 | + public function getCtor( $class ) { | |
| 32 | + $cacheKey = self::CACHE_KEY_CTORS . strtolower( $class ); | |
| 33 | + | |
| 34 | + if ( ( $reflectedCtor = $this->cache->fetch( $cacheKey ) ) === false ) { | |
| 35 | + $this->cache->store( $cacheKey, $reflectedCtor = $this->reflector->getCtor( $class ) ); | |
| 36 | + } | |
| 37 | + | |
| 38 | + return $reflectedCtor; | |
| 39 | + } | |
| 40 | + | |
| 41 | + public function getCtorParams( $class ) { | |
| 42 | + $cacheKey = self::CACHE_KEY_CTOR_PARAMS . strtolower( $class ); | |
| 43 | + | |
| 44 | + if ( ( $reflectedCtorParams = $this->cache->fetch( $cacheKey ) ) === false ) { | |
| 45 | + $this->cache->store( $cacheKey, $reflectedCtorParams = $this->reflector->getCtorParams( $class ) ); | |
| 46 | + } | |
| 47 | + | |
| 48 | + return $reflectedCtorParams; | |
| 49 | + } | |
| 50 | + | |
| 51 | + public function getParamTypeHint( \ReflectionFunctionAbstract $function, \ReflectionParameter $param ) { | |
| 52 | + $lowParam = strtolower( $param->name ); | |
| 53 | + | |
| 54 | + if ( $function instanceof \ReflectionMethod ) { | |
| 55 | + $lowClass = strtolower( $function->class ); | |
| 56 | + $lowMethod = strtolower( $function->name ); | |
| 57 | + $paramCacheKey = self::CACHE_KEY_CLASSES . "{$lowClass}.{$lowMethod}.param-{$lowParam}"; | |
| 58 | + } else { | |
| 59 | + $lowFunc = strtolower( $function->name ); | |
| 60 | + $paramCacheKey = ( strpos( $lowFunc, '{closure}' ) === false ) | |
| 61 | + ? self::CACHE_KEY_FUNCS . ".{$lowFunc}.param-{$lowParam}" | |
| 62 | + : null; | |
| 63 | + } | |
| 64 | + | |
| 65 | + $typeHint = ( $paramCacheKey === null ) ? false : $this->cache->fetch( $paramCacheKey ); | |
| 66 | + | |
| 67 | + if ( false === $typeHint ) { | |
| 68 | + $typeHint = $this->reflector->getParamTypeHint( $function, $param ); | |
| 69 | + if ( $paramCacheKey !== null ) { | |
| 70 | + $this->cache->store( $paramCacheKey, $typeHint ); | |
| 71 | + } | |
| 72 | + } | |
| 73 | + | |
| 74 | + return $typeHint; | |
| 75 | + } | |
| 76 | + | |
| 77 | + public function getFunction( $functionName ) { | |
| 78 | + $lowFunc = strtolower( $functionName ); | |
| 79 | + $cacheKey = self::CACHE_KEY_FUNCS . $lowFunc; | |
| 80 | + | |
| 81 | + if ( ( $reflectedFunc = $this->cache->fetch( $cacheKey ) ) === false ) { | |
| 82 | + $this->cache->store( $cacheKey, $reflectedFunc = $this->reflector->getFunction( $functionName ) ); | |
| 83 | + } | |
| 84 | + | |
| 85 | + return $reflectedFunc; | |
| 86 | + } | |
| 87 | + | |
| 88 | + public function getMethod( $classNameOrInstance, $methodName ) { | |
| 89 | + $className = is_string( $classNameOrInstance ) | |
| 90 | + ? $classNameOrInstance | |
| 91 | + : get_class( $classNameOrInstance ); | |
| 92 | + | |
| 93 | + $cacheKey = self::CACHE_KEY_METHODS . strtolower( $className ) . '.' . strtolower( $methodName ); | |
| 94 | + | |
| 95 | + if ( ( $reflectedMethod = $this->cache->fetch( $cacheKey ) ) === false ) { | |
| 96 | + $this->cache->store( $cacheKey, $reflectedMethod = $this->reflector->getMethod( $classNameOrInstance, $methodName ) ); | |
| 97 | + } | |
| 98 | + | |
| 99 | + return $reflectedMethod; | |
| 100 | + } | |
| 101 | +} | |