コード例 #1
0
ファイル: Decorator.php プロジェクト: samsonasik/patchwork
 public function __invoke()
 {
     $top = Stack\top();
     $superclassMatches = $this->superclassMatches();
     $instanceMatches = $this->instanceMatches($top);
     $methodMatches = $this->methodMatches($top);
     if ($superclassMatches && $instanceMatches && $methodMatches) {
         $patch = $this->patch;
         if (isset($top["object"]) && $patch instanceof \Closure) {
             $patch = $patch->bindTo($top["object"], $this->superclass);
         }
         return dispatchTo($patch);
     }
     Patchwork\fallBack();
 }
コード例 #2
0
 public function __invoke()
 {
     $top = Stack\top();
     $superclassMatches = $this->superclassMatches();
     $instanceMatches = $this->instanceMatches($top);
     $methodMatches = $this->methodMatches($top);
     if ($superclassMatches && $instanceMatches && $methodMatches) {
         $patch = $this->patch;
         if (is_callable(array($patch, "bindTo"))) {
             if (isset($top["object"]) && $patch instanceof \Closure) {
                 $patch = $patch->bindTo($top["object"], $this->superclass);
             }
         }
         return runPatch($patch);
     }
     Patchwork\pass();
 }
コード例 #3
0
 /**
  * @inheritdoc
  * @see \Aop\JoinPoint\Support\FunctionSupportInterface::getFunctionName()
  */
 public function getFunctionName()
 {
     return Stack\top('function');
 }
コード例 #4
0
 /**
  * Invoke the original callable intercepted.
  *
  * @param int $index Index of `PatchworkInterceptor::$context`.
  */
 protected function pwInvoke($index)
 {
     $current = Stack\top();
     if ($current['object']) {
         $callOrigin = array($current['object'], $current['function']);
     } else {
         $callOrigin = $current['function'];
     }
     $this->disable($index);
     self::$context[$index]['return'] = call_user_func_array($callOrigin, self::$context[$index]['args']);
     $this->enable($index);
 }
コード例 #5
0
ファイル: Interceptor.php プロジェクト: rarescosma/patchwork
function callOriginal(array $args = null)
{
    list($class, $method, $offset) = end(State::$patchStack);
    $patch =& State::$patches[$class][$method][$offset];
    $backup = $patch;
    $patch = array('Patchwork\\fallBack', new PatchHandle());
    $top = Stack\top();
    if ($args === null) {
        $args = $top["args"];
    }
    try {
        if (isset($top["class"])) {
            $reflection = new \ReflectionMethod(Stack\topCalledClass(), $top["function"]);
            $reflection->setAccessible(true);
            $result = $reflection->invokeArgs(Stack\top("object"), $args);
        } else {
            $result = call_user_func_array($top["function"], $args);
        }
    } catch (\Exception $e) {
        $exception = $e;
    }
    $patch = $backup;
    if (isset($exception)) {
        throw $exception;
    }
    return $result;
}
コード例 #6
0
function relay(array $args = null)
{
    list($class, $method, $offset) = end(State::$routeStack);
    $route =& State::$routes[$class][$method][$offset];
    $backup = $route;
    $route = ['Patchwork\\fallBack', new Handle()];
    $top = Stack\top();
    if ($args === null) {
        $args = $top['args'];
    }
    try {
        if (isset($top['class'])) {
            $reflection = new \ReflectionMethod(Stack\topCalledClass(), $top['function']);
            $reflection->setAccessible(true);
            $result = $reflection->invokeArgs(Stack\top('object'), $args);
        } else {
            $result = call_user_func_array($top['function'], $args);
        }
    } catch (\Exception $e) {
        $exception = $e;
    }
    $route = $backup;
    if (isset($exception)) {
        throw $exception;
    }
    return $result;
}
コード例 #7
0
function connectDefaultInternals()
{
    if (Config\getRedefinableInternals() === []) {
        return;
    }
    foreach (Config\getDefaultRedefinableInternals() as $function) {
        $offsets = [];
        foreach ((new \ReflectionFunction($function))->getParameters() as $offset => $argument) {
            $name = $argument->getName();
            if (strpos($name, 'call') !== false || strpos($name, 'func') !== false) {
                $offsets[] = $offset;
            }
        }
        connect($function, function () use($offsets) {
            $args = Stack\top('args');
            foreach ($offsets as $offset) {
                if (!isset($args[$offset])) {
                    continue;
                }
                $callable = $args[$offset];
                $args[$offset] = function () use($callable) {
                    if ($callable instanceof \Closure) {
                        return call_user_func_array($callable, func_get_args());
                    }
                    list($class, $method, $instance) = Utils\interpretCallable($callable);
                    if ($class === 'self' || $class === 'static' || $class === 'parent') {
                        $origin = debug_backtrace()[$frame]['class'];
                        if ($class === 'parent') {
                            $origin = get_parent_class($origin);
                        }
                        $class = $origin;
                        $callable = [$class, $method];
                    }
                    if ($class !== null && !is_callable([$class, $method])) {
                        $reflection = new \ReflectionMethod($class, $method);
                        $reflection->setAccessible(true);
                        return $reflection->invokeArgs($instance, func_get_args());
                    }
                    return dispatchDynamic($callable, func_get_args());
                };
            }
            return relay($args);
        });
    }
}