protected function execute() { try { $this->deferred()->resolve($this->delegate->invokeWith(\func_get_args())); } catch (\Exception $e) { $this->deferred()->reject($e); } }
/** * {@inheritdoc} */ public function resolve(MessageInterface $message) { $nameOfMessage = $this->nameOfMessageResolver->resolve($message); $handler = $this->getHandlerFor($nameOfMessage); $handlerMethodName = $this->getHandlerMethodFor($nameOfMessage); if (!method_exists($handler, $handlerMethodName)) { throw NotFoundException::methodForObject($handler, $handlerMethodName); } return Delegate::fromMethod($handler, $handlerMethodName); }
/** * {@inheritdoc} */ public function apply($value) { if (!is_object($value)) { throw new \RuntimeException('Trying to call method of non-object'); } if (!method_exists($value, $this->name)) { throw new \RuntimeException(\sprintf('Undefined method %s::%s', \get_class($value), $this->name)); } $method = Delegate::fromMethod($value, $this->name); if ($method->reflection()->isPrivate() || $method->reflection()->isProtected()) { throw new \RuntimeException(\sprintf('Trying to call non-public method %s::%s', \get_class($value), $this->name)); } return $method->invokeWith($this->args()); }
/** */ private function onTick() { try { $this->lastResult = $this->task->__invoke($this); ++$this->iterations; if ($this->timer->isPeriodic()) { $this->deferred()->notify($this->lastResult); if ($this->maxIterations() !== null && $this->iterations() >= $this->maxIterations()) { $this->cancel(); } } else { $this->deferred()->resolve($this->lastResult); } } catch (\Exception $e) { $this->deferred()->reject($e); $this->timer->cancel(); } }
/** * Execute the given message... after other running messages are complete. * * @param mixed $message * @param callable $next * * @throws \Exception */ public function handle($message, callable $next) { $this->queue[] = Delegate::fromClosure(function () use($message, $next) { return $next($message); }); if ($this->isRunning) { return; } $this->isRunning = true; try { $this->runQueuedJobs(); } catch (\Exception $e) { $this->isRunning = false; $this->queue = []; throw $e; } $this->isRunning = false; }
/** * {@inheritdoc} */ protected function notify($state = null) { $this->notify->__invoke($state); }
/** * @return callable */ public function target() { return $this->callbackDelegate->target(); }
/** * {@inheritdoc} */ public function equals($a, $b) { return $this->callbackDelegate->__invoke($a, $b); }
/** * {@inheritdoc} */ public function compare($a, $b) { return $this->direction()->value() * parent::compare($this->selector->__invoke($a), $this->selector->__invoke($b)); }
/** * @param BinaryConstraintOperator $operator */ protected function visitNotEqualityOperator(BinaryConstraintOperator $operator) { $this->visitRelationalOperator($operator, Delegate::fromMethod($this->queryBuilder, 'notEqual')); }
/** * @param string $method * @param array $arguments * * @return mixed */ public static function __callStatic($method, $arguments) { return Delegate::fromMethod(self::this(), $method)->invokeWith($arguments); }
/** * @return Delegate */ private function chainedExecution() { $middlewares = []; foreach ($this->middlewares as $priority => $collection) { foreach ($collection as $middleware) { $middlewares[] = $middleware; } } $next = Delegate::fromClosure(function ($message) { // the final middleware return the same message return $message; }); // reverse iteration over middlewares /** @var MiddlewareInterface $middleware */ while ($middleware = array_pop($middlewares)) { $next = Delegate::fromClosure(function ($message) use($middleware, $next) { return $middleware->handle($message, $next); }); } return $next; }
/** * @return static */ public function createQueryBuilder() { return $this->factory->__invoke(); }
/** * Test reflection method. */ public function testReflection() { $this->given($closure = function ($value = null) { return $value; })->when($reflection = Delegate::fromClosure($closure)->reflection())->then()->object($reflection)->isEqualTo(new \ReflectionFunction($closure)); $this->when($reflection = Delegate::fromMethod($this, 'sampleMethod')->reflection())->then()->object($reflection)->isEqualTo(new \ReflectionMethod($this, 'sampleMethod')); $this->given($foo = new FooCallable())->when($reflection = (new Delegate($foo))->reflection())->then()->object($reflection)->isEqualTo(new \ReflectionMethod($foo, '__invoke')); $this->when($reflection = Delegate::fromStaticMethod(self::class, 'sampleStaticMethod')->reflection())->then()->object($reflection)->isEqualTo(new \ReflectionMethod(self::class . '::sampleStaticMethod')); }
/** * @param callable $callable */ public function __construct($callable) { parent::__construct($callable); $this->callback = $callable; }