public function execute($callable, array $args = [], $context = null)
    {
        if ($callable instanceof \Closure) {
            $ref = new \ReflectionObject($callable);

            $args = $this->getMethodArgs($ref->getMethod('__invoke'), $args);
        } else {
            if (is_string($callable)) {
                $callable = explode('::', $callable);
            }

            list($object, $method) = $callable;

            $ref = new \ReflectionClass($object);

            $args = $this->getMethodArgs($ref->getMethod($method), $args);
        }

        $closure = function () use ($args, $callable) {
            switch (count($args)) {
                case 0:
                    return $callable();
                case 1:
                    return $callable($args[0]);
                case 2:
                    return $callable($args[0], $args[1]);
                case 3:
                    return $callable($args[0], $args[1], $args[2]);
                case 4:
                    return $callable($args[0], $args[1], $args[2], $args[3]);
                default:
                    return call_user_func_array($callable, $args);
            }
        };

        if ($context) {
            $closure = $closure->bindTo($context, $context);
        }

        return $closure();
    }
public function execute($callable, array $args = [], $context = null)
    {
        if ($callable instanceof \Closure) {
            $ref = new \ReflectionObject($callable);

            $args = $this->getMethodArgs($ref->getMethod('__invoke'), $args);
        } else {
            if (is_string($callable)) {
                $callable = explode('::', $callable);
            }

            list($object, $method) = $callable;

            $ref = new \ReflectionClass($object);

            $args = $this->getMethodArgs($ref->getMethod($method), $args);
        }

        $closure = function () use ($args, $callable) {
            switch (count($args)) {
                case 0:
                    return $callable();
                case 1:
                    return $callable($args[0]);
                case 2:
                    return $callable($args[0], $args[1]);
                case 3:
                    return $callable($args[0], $args[1], $args[2]);
                case 4:
                    return $callable($args[0], $args[1], $args[2], $args[3]);
                default:
                    return call_user_func_array($callable, $args);
            }
        };

        if ($context) {
            $closure = $closure->bindTo($context, $context);
        }

        return $closure();
    }
public function execute ( $ callable , array $ args = [ ] , $ context = null ) { if ( $ callable instanceof \ Closure ) { $ ref = new \ ReflectionObject ( $ callable ) ; $ args = $ this -> getMethodArgs ( $ ref -> getMethod ( '__invoke' ) , $ args ) ; } else { if ( is_string ( $ callable ) ) { $ callable = explode ( '::' , $ callable ) ; } list ( $ object , $ method ) = $ callable ; $ ref = new \ ReflectionClass ( $ object ) ; $ args = $ this -> getMethodArgs ( $ ref -> getMethod ( $ method ) , $ args ) ; } $ closure = function ( ) use ( $ args , $ callable ) { switch ( count ( $ args ) ) { case 0 : return $ callable ( ) ; case 1 : return $ callable ( $ args [ 0 ] ) ; case 2 : return $ callable ( $ args [ 0 ] , $ args [ 1 ] ) ; case 3 : return $ callable ( $ args [ 0 ] , $ args [ 1 ] , $ args [ 2 ] ) ; case 4 : return $ callable ( $ args [ 0 ] , $ args [ 1 ] , $ args [ 2 ] , $ args [ 3 ] ) ; default : return call_user_func_array ( $ callable , $ args ) ; } } ; if ( $ context ) { $ closure = $ closure -> bindTo ( $ context , $ context ) ; } return $ closure ( ) ; }
Execute a callable with dependencies.

@param callable $callable
@param array    $args
@param object   $context

@return  mixed

@throws DependencyResolutionException
@throws \ReflectionException
Execute a callable with dependencies .