| 1 |
<?php |
| 2 |
|
| 3 |
namespace FL\Assistant\System\Container; |
| 4 |
|
| 5 |
class Executable { |
| 6 |
|
| 7 |
private $callableReflection; |
| 8 |
private $invocationObject; |
| 9 |
private $isInstanceMethod; |
| 10 |
|
| 11 |
public function __construct( \ReflectionFunctionAbstract $reflFunc, $invocationObject = null ) { |
| 12 |
if ( $reflFunc instanceof \ReflectionMethod ) { |
| 13 |
$this->isInstanceMethod = true; |
| 14 |
$this->setMethodCallable( $reflFunc, $invocationObject ); |
| 15 |
} else { |
| 16 |
$this->isInstanceMethod = false; |
| 17 |
$this->callableReflection = $reflFunc; |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
private function setMethodCallable( \ReflectionMethod $reflection, $invocationObject ) { |
| 22 |
if ( is_object( $invocationObject ) ) { |
| 23 |
$this->callableReflection = $reflection; |
| 24 |
$this->invocationObject = $invocationObject; |
| 25 |
} elseif ( $reflection->isStatic() ) { |
| 26 |
$this->callableReflection = $reflection; |
| 27 |
} else { |
| 28 |
throw new \InvalidArgumentException( |
| 29 |
'ReflectionMethod callables must specify an invocation object' |
| 30 |
); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public function __invoke() { |
| 35 |
$args = func_get_args(); |
| 36 |
$reflection = $this->callableReflection; |
| 37 |
|
| 38 |
if ( $this->isInstanceMethod ) { |
| 39 |
return $reflection->invokeArgs( $this->invocationObject, $args ); |
| 40 |
} |
| 41 |
|
| 42 |
return $this->callableReflection->isClosure() |
| 43 |
? $this->invokeClosureCompat( $reflection, $args ) |
| 44 |
: $reflection->invokeArgs( $args ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @TODO Remove this extra indirection when 5.3 support is dropped |
| 49 |
*/ |
| 50 |
private function invokeClosureCompat( $reflection, $args ) { |
| 51 |
if ( version_compare( PHP_VERSION, '5.4.0' ) >= 0 ) { |
| 52 |
$scope = $reflection->getClosureScopeClass(); |
| 53 |
$closure = \Closure::bind( |
| 54 |
$reflection->getClosure(), |
| 55 |
$reflection->getClosureThis(), |
| 56 |
$scope ? $scope->name : null |
| 57 |
); |
| 58 |
return call_user_func_array( $closure, $args ); |
| 59 |
} else { |
| 60 |
return $reflection->invokeArgs( $args ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public function getCallableReflection() { |
| 65 |
return $this->callableReflection; |
| 66 |
} |
| 67 |
|
| 68 |
public function getInvocationObject() { |
| 69 |
return $this->invocationObject; |
| 70 |
} |
| 71 |
|
| 72 |
public function isInstanceMethod() { |
| 73 |
return $this->isInstanceMethod; |
| 74 |
} |
| 75 |
} |
| 76 |
|