Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- NAMESPACE ILLI\Core;
- USE ILLI\Core\Invoke\tMethod;
- USE ILLI\Core\Invoke\tStatic;
- USE ILLI\Core\Invoke\tCallable;
- USE ILLI\Core\Invoke\tClass;
- USE Closure;
- CLASS Invoke
- {
- USE tMethod
- {
- tMethod_emit AS public emitMethod;
- }
- USE tStatic
- {
- tStatic_emit AS public emitStatic;
- }
- USE tCallable
- {
- tCallable_emit AS public emitCallable;
- }
- USE tClass
- {
- tClass_emit AS public emitClass;
- }
- /**
- * emit normalized invokable array
- *
- * string -> tClass_emit -> new string()
- * string, array -> tClass_emit -> new string(array)
- *
- * object, string -> tMethod_emit -> object->string()
- * object, string, array -> tMethod_emit -> object->string(array)
- *
- * string, string -> tStatic_emit -> string::string()
- * string, string, array -> tStatic_emit -> string::string(array)
- *
- * closure -> tCallable_emit -> closure()
- * closure, array -> tCallable_emit -> closure(array)
- * object, closure -> tCallable_emit -> closure() use (object)
- * object, closure, array -> tCallable_emit -> closure(args) use (object)
- * string, closure -> tCallable_emit -> static::closure()
- * string, closure, array -> tCallable_emit -> static::closure(array)
- *
- * @see normalize()
- */
- public static function emit()
- {
- $args = func_get_args();
- if([] === $args)
- return NULL;
- switch(count($args)):
- case 0:
- return NULL;
- break;
- case 1:
- switch(TRUE):
- // [classname]
- case is_string($args[0]):
- return static::tClass_emit($args[0]);
- break;
- // [closure]
- case is_object($args[0]):
- return static::tCallable_emit($args[0]);
- break;
- endswitch;
- break;
- case 2:
- switch(TRUE):
- // [classname, args]
- case is_string($args[0]) && is_array($args[1]):
- return static::tClass_emit($args[0], $args[1]);
- break;
- // [closure, args]
- case is_object($args[0]) && is_array($args[1]):
- return static::tCallable_emit($args[0], $args[1]);
- break;
- // [instance, methodname]
- case is_object($args[0]) && is_string($args[1]):
- return static::tMethod_emit($args[0], $args[1]);
- break;
- // [classname, methodname]
- case is_string($args[0]) && is_string($args[1]):
- return static::tStatic_emit($args[0], $args[1]);
- break;
- // [instance, closure]
- case is_object($args[0]) && is_object($args[1]):
- return static::tCallable_emit($args[1]);
- break;
- // [classname, closure]
- case is_string($args[0]) && is_object($args[1]):
- return static::tCallable_emit($args[1]);
- break;
- endswitch;
- break;
- case 3:
- default:
- switch(TRUE):
- // [instance, methodname, args]
- case is_object($args[0]) && is_string($args[1]) && is_array($args[2]):
- return static::tMethod_emit($args[0], $args[1], $args[2]);
- break;
- // [classname, methodname, args]
- case is_string($args[0]) && is_string($args[1]) && is_array($args[2]):
- return static::tStatic_emit($args[0], $args[1], $args[2]);
- break;
- // [instance, closure, args]
- case is_object($args[0]) && is_object($args[1]) && is_array($args[2]):
- return static::tCallable_emit($args[1], $args[2]);
- break;
- // [classname, closure, args]
- case is_string($args[0]) && is_object($args[1]) && is_array($args[2]):
- return static::tCallable_emit($args[1], $args[2]);
- break;
- endswitch;
- break;
- endswitch;
- $type = [];
- foreach($args as $arg)
- $type[] = getType($arg);
- throw new \Exception('No Handler defined for ['.implode(', ', $type).'].');
- }
- /**
- * @param array $__subject normalized callable array
- * @see normalize()
- */
- public static function emitable($__subject)
- {
- if(FALSE === is_array($__subject))
- return FALSE;
- $length = count($__subject);
- if($length < 1 || $length > 2)
- return FALSE;
- if($length === 1)
- {
- if(is_object($__subject[0]))
- {
- return method_exists($__subject[0], '__invoke');
- }
- else
- if(is_string($__subject[0]))
- {
- return class_exists($__subject[0]);
- }
- return false;
- }
- if(is_object($__subject[0])
- || (is_string($__subject[0]) && class_exists($__subject[0])))
- {
- if(is_string($__subject[1]))
- {
- return method_exists($__subject[0], $__subject[1]);
- }
- }
- return false;
- }
- /**
- * create delegatable array from
- * - 'class::method' => string + string => [string, string] => class::method()
- * - 'method' + target => object + string => [object, string] => target->method()
- * - 'class' => string => [string] => new class
- *
- * - [] + target => object => [object] => target->__invoke()
- * - ['method'] + target => object + string => [object, method] => target->method()
- * - [instance, 'method'] => object + string => [string, string] => instance->method()
- * - ['class', 'method'] => string + string => [string, string] => class::method()
- * - ['class'] => string => [string] => new class
- *
- * convert misc handler-configs to callable address
- *
- * @param array|string|object $__subject callable array, class::method-string or closure
- * @param object|string $__target fallback-instance or -class
- * @return array
- * @note bind $__target to [closure $__subject]
- * @see emit()
- */
- public static function normalize($__subject, $__target)
- {
- // [\foo]
- if(is_object($__subject)
- && method_exists($__subject, '__invoke'))
- {
- if($__subject instanceOf Closure)
- {
- if(NULL === $__target)
- return [$__subject];
- $__subject = is_string($__target)
- ? Closure::bind($__subject, NULL, get_class($__target))
- : Closure::bind($__subject, $__target, get_class($__target));
- }
- return [$__target, $__subject];
- }
- if(is_callable($__subject) && is_array($__subject) && count($__subject) === 2)
- return $__subject;
- // '\foo::bar' | '\foo' | 'bar'
- if(is_string($__subject))
- {
- $subject = explode('::', $__subject);
- $length = count($subject);
- if($length === 2)
- {
- // ['foo', 'bar']
- return $subject;
- }
- else
- if($length === 1)
- {
- if(class_exists($subject[0]))
- {
- // ['foo']
- return $subject;
- }
- else
- {
- // ['bar']
- $__subject = $subject;
- }
- }
- }
- // [\foo] | ['\foo'] | ['bar'] | ['\foo', 'bar'] | [\foo, 'bar']
- if(is_array($__subject))
- {
- // []
- if(!isset($__subject[0]))
- return [$__target]; // target->__invoke()
- if(count($__subject) === 2)
- // ['\foo', 'bar'] | [\foo, 'bar']
- return $__subject;
- // [\foo] | ['\foo'] | ['bar']
- if(count($__subject) === 1)
- {
- // [\foo]
- if(is_object($__subject[0]))
- return $__subject; // \foo()
- // ['\foo'] | ['bar']
- if(is_string($__subject[0]))
- {
- // ['\foo']
- if(class_exists($__subject[0]))
- return $__subject; // new foo
- // ['bar']
- return [$__target , $__subject[0]]; // target->bar()
- }
- }
- }
- // \foo -> function[]{}/object -> [\foo]
- return static::normalize([$__subject], $__target);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement