/** * Constructs a test suite, which may contain nested suites and specs. The * anonymous function passed to the constructor contains the body of the * suite to be ran, and it is bound to the suite. * * @param string $title A title to be associated with the suite * @param \Closure $closure The closure to invoke when the suite is ran * @param Suite $parent An optional parent suite */ public function __construct($title, \Closure $closure, Suite $parent = null) { $this->title = $title; if (version_compare(PHP_VERSION, '5.4.0', ">=")) { $this->closure = $closure->bindTo($this); } else { $this->closure = $closure; } $this->specs = array(); $this->suites = array(); $this->store = array(); $this->parent = $parent; }
/** * Invokes original method and return result from it * * @return mixed */ public function proceed() { if (isset($this->advices[$this->current])) { /** @var $currentInterceptor \Go\Aop\Intercept\Interceptor */ $currentInterceptor = $this->advices[$this->current++]; return $currentInterceptor->invoke($this); } // Fill the closure only once if it's empty if (!$this->closureToCall) { $this->closureToCall = $this->reflectionMethod->getClosure($this->instance); } // Rebind the closure if instance was changed since last time if ($this->previousInstance !== $this->instance) { $this->closureToCall = $this->closureToCall->bindTo($this->instance, $this->reflectionMethod->class); $this->previousInstance = $this->instance; } $closureToCall = $this->closureToCall; $args = $this->arguments; switch (count($args)) { case 0: return $closureToCall(); case 1: return $closureToCall($args[0]); case 2: return $closureToCall($args[0], $args[1]); case 3: return $closureToCall($args[0], $args[1], $args[2]); case 4: return $closureToCall($args[0], $args[1], $args[2], $args[3]); case 5: return $closureToCall($args[0], $args[1], $args[2], $args[3], $args[4]); default: return forward_static_call_array($closureToCall, $args); } }
public function get() { if (!$this->closure instanceof \Closure) { throw new \RuntimeException("Cannot get a key with an undefined closure"); } if ($this->enabled) { $data = $this->predis->get($this->getKeyName($this->key)); } if ($data === null) { $boundCallback = $this->closure->bindTo($this); $data = $boundCallback(); if ($this->enabled) { $this->predis->setex($this->getKeyName($this->key), $this->ttl, serialize($data)); } if ($this->onMiss instanceof \Closure) { call_user_func_array($this->onMiss, [$this, $data]); } return $data; } else { $value = unserialize($data); if ($this->onHit instanceof \Closure) { call_user_func_array($this->onHit, [$this, $value]); } return $value; } }
/** * Handles the event * * @param Event $event * @return mixed|void */ public function discern(Event $event) { if ($this->eventInstance && !$event instanceof $this->eventInstance) { return; } $callback = $this->callback->bindTo($this, $this); $callback($event); }
/** * Execute the console command. * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * * @return mixed */ protected function execute(InputInterface $input, OutputInterface $output) { $inputs = array_merge($input->getArguments(), $input->getOptions()); $parameters = []; foreach ((new ReflectionFunction($this->callback))->getParameters() as $parameter) { if (isset($inputs[$parameter->name])) { $parameters[$parameter->name] = $inputs[$parameter->name]; } } return $this->getInvoker()->call($this->callback->bindTo($this, $this), $parameters); }
/** * Runs the stub. * * @param string $self The context form which the stub need to be executed. * @param array $params The call parameters array. * @return mixed The returned stub result. */ public function __invoke($self, $params) { if ($this->_closure) { if (is_string($self)) { $closure = $this->_closure->bindTo(null, $self); } else { $closure = $this->_closure->bindTo($self, get_class($self)); } return call_user_func_array($closure, $params); } if (isset($this->_returns[$this->_index])) { return $this->_returns[$this->_index++]; } return $this->_returns ? end($this->_returns) : null; }
/** * Invokes original method and return result from it * * @return mixed */ public function proceed() { if (isset($this->advices[$this->current])) { /** @var $currentInterceptor MethodInterceptor */ $currentInterceptor = $this->advices[$this->current++]; return $currentInterceptor->invoke($this); } // Rebind the closure if scope (class name) was changed since last time if ($this->previousScope !== $this->instance) { $this->closureToCall = $this->closureToCall->bindTo(null, $this->instance); $this->previousScope = $this->instance; } $closureToCall = $this->closureToCall; return $closureToCall($this->arguments); }
/** * @return void */ public final function customInit() { if ($this->customInitClosure instanceof \Closure) { $customInitClosure = $this->customInitClosure->bindTo($this, $this); $customInitClosure(); } }
public function on($event, \Closure $callback) { if (is_string($event)) { $this->events[$event] = $callback->bindTo($this, $this); } return $this; }
/** * Constructs a Spec, to be associated with a particular suite, and ran * by the test runner. The closure is bound to the suite. * * @param string $title A title to be associated with the spec * @param \Closure $closure The closure to invoke when the spec is called * @param Suite $suite The suite within which this spec was defined */ public function __construct($title, \Closure $closure = null, Suite $suite) { $this->title = $title; $this->suite = $suite; if ($closure) { $this->closure = $closure->bindTo($suite); } }
/** * Registers a route. * * @param string $name * @param \Closure $handler * * @return Route */ public function add($name, \Closure $handler) { // Bind the handler to the Router class $route = new Route($name, $handler->bindTo($this, $this)); // Insert the route $this->routes[$name] = $route; return $route; }
public function fingerprint(\Closure $fingerprint = null) { if (func_num_args() > 0) { $this->_fingerprint = $fingerprint->bindTo($this); return $this; } return $this->_fingerprint; }
/** * @param string $prefix * @param \Closure $closure */ public function group($prefix, \Closure $closure) { $original = $this->prefix; $this->prefix = sprintf('/%s/%s', trim($original, '/'), trim($prefix, '/')); $callback = $closure->bindTo($this); $callback(); $this->prefix = $original; }
public function __construct(\Closure $closure, Suite $suite) { $this->suite = $suite; if (version_compare(PHP_VERSION, '5.4.0', '>=')) { $this->closure = $closure->bindTo($suite); } else { $this->closure = $closure; } }
/** * Invokes original method and return result from it * * @return mixed */ public function proceed() { if (isset($this->advices[$this->current])) { /** @var $currentInterceptor Interceptor */ $currentInterceptor = $this->advices[$this->current++]; return $currentInterceptor->invoke($this); } // Fill the closure only once if it's empty if (!$this->closureToCall) { $this->closureToCall = $this->reflectionMethod->getClosure($this->instance); } // Rebind the closure if instance was changed since last time if ($this->previousInstance !== $this->instance) { $this->closureToCall = $this->closureToCall->bindTo($this->instance, $this->reflectionMethod->class); $this->previousInstance = $this->instance; } $closureToCall = $this->closureToCall; return $closureToCall(...$this->arguments); }
/** * Constructs a test suite, which may contain nested suites and specs. The * anonymous function passed to the constructor contains the body of the * suite to be ran, and it is bound to the suite. * * @param string $title A title to be associated with the suite * @param \Closure $closure The closure to invoke when the suite is ran * @param Suite $parent An optional parent suite */ public function __construct($title, \Closure $closure, Suite $parent = null) { $this->title = $title; $this->closure = $closure->bindTo($this); $this->specs = []; $this->suites = []; $this->store = []; $this->parent = $parent; $this->pending = false; }
/** * Constructs a Spec, to be associated with a particular suite, and ran * by the test runner. The closure is bound to the suite. * * @param string $title A title to be associated with the spec * @param \Closure $closure The closure to invoke when the spec is called * @param Suite $suite The suite within which this spec was defined */ public function __construct($title, \Closure $closure = null, Suite $suite) { $this->title = $title; $this->suite = $suite; if ($closure && version_compare(PHP_VERSION, '5.4.0', '>=')) { $this->closure = $closure->bindTo($suite); } else { if ($closure) { $this->closure = $closure; } } }
/** * Unserializes the closure. * * Unserializes the closure's data and recreates the closure using a * simulation of its original context. The used variables (context) are * extracted into a fresh scope prior to redefining the closure. The * closure is also rebound to its former object and scope. * * @see http://php.net/manual/en/serializable.unserialize.php * * @param string $serialized * * @throws ClosureUnserializationException */ public function unserialize($serialized) { // Unserialize the data and reconstruct the SuperClosure. $this->data = unserialize($serialized); $this->reconstructClosure(); if (!$this->closure instanceof \Closure) { throw new ClosureUnserializationException('The closure is corrupted and cannot be unserialized.'); } // Rebind the closure to its former binding, if it's not static. if (!$this->data['isStatic']) { $this->closure = $this->closure->bindTo($this->data['binding'], $this->data['scope']); } }
public function addRule(RuleInterface $routeRule, \Closure $callback) { if (false !== ($callbackClosure = $callback->bindTo($routeRule, $routeRule))) { $routeRule->setCallback($callbackClosure); } else { throw new InvalidRuleCallbackException('invalid callback closure specified!'); } if (!$routeRule->isTerminable()) { array_unshift($this->_rules, $routeRule); } else { $this->_rules[] = $routeRule; } return self::$_instance; }
/** * Unserializes the closure. * * Unserializes the closure's data and recreates the closure using a * simulation of its original context. The used variables (context) are * extracted into a fresh scope prior to redefining the closure. The * closure is also rebound to its former object and scope. * * @param string $serialized * * @throws ClosureUnserializationException * @link http://php.net/manual/en/serializable.unserialize.php */ public function unserialize($serialized) { // Unserialize the data and reconstruct the SuperClosure. $this->data = unserialize($serialized); try { $this->reconstructClosure(); } catch (\ParseException $e) { // Discard the parse exception, we'll throw a custom one // a few lines down. } if (!$this->closure instanceof \Closure) { throw new ClosureUnserializationException('The closure is corrupted and cannot be unserialized.'); } // Rebind the closure to its former binding, if it's not static. if (!$this->data['isStatic']) { $this->closure = $this->closure->bindTo($this->data['binding'], $this->data['scope']); } }
public function __construct(\Closure $closure, Suite $suite) { $this->suite = $suite; $this->closure = $closure->bindTo($suite); }
protected function doEvaluationWithNewThis(array $variableTable, $newThis) { $evaluator = $this->originalCompiledEvaluator->bindTo($newThis, $this->context->getScopeType()); return $evaluator($variableTable + $this->extraVariables); }
protected final function setProxy(\Closure $func) { $this->_proxy = $func->bindTo($this); }
public static function with($obj, \Closure $closure) { return $closure->bindTo($obj, $obj)->__invoke(); }
function afterSpecify(\Closure $callable = null) { $this->afterSpecify[] = $callable->bindTo($this); }
/** * It is just a wrapper for the * * @link http://www.php.net/manual/en/closure.bindto.php * @param object $newthis The object to which the given anonymous function should be bound, or NULL for the closure to be unbound. * @param mixed $newscope The class scope to which associate the closure is to be associated, or 'static' to keep the current one. * If an object is given, the type of the object will be used instead. * This determines the visibility of protected and private methods of the bound object. * @return Closure Returns the newly created Closure object or FALSE on failure */ public function bindTo($newthis, $newscope = 'static') { return $this->closure->bindTo($newthis, $newscope); }
public function registerMediaTypeParser($type, \Closure $callable) { $this->bodyParsers[$type] = $callable->bindTo($this); }
public function cacheBust(\Closure $cacheBust = null) { if (func_num_args() > 0) { $this->_cacheBust = $cacheBust->bindTo($this); return $this; } return $this->_cacheBust; }
private function isClosureStatic(\Closure $closure) { $rebound = new \ReflectionFunction(@$closure->bindTo(new \stdClass())); return $rebound->getClosureThis() === null; }
public function __construct(\Closure $function) { $this->fn = $function->bindTo($this); }