Calls the specified handler when this promise is fulfilled.

If the handler returns a promise,

@param callable $handler
@return PromiseInterface
700
public function to($handler /*, $args… */)
    {
        $args = array_slice(func_get_args(), 1);

        return $this->then(
            function (Allocation $allocation) use ($handler, $args) {
                try {
                    $result = call_user_func_array($handler, $args);
                    $result = \React\Promise\resolve($result);
                    $result->then(array($allocation, 'releaseAll'), array($allocation, 'releaseAll'));
                } catch (\Exception $e) {
                    $result = new RejectedPromise($e);
                    $allocation->releaseAll();
                }

                return $result;
            }
        );
    }
1360163
public function setHandler($handler)
    {
        $handler = new $handler($this->config);

        if (!$handler instanceof HandlerInterface) {
            throw new \RuntimeException('Handler not initialized. Please use Handler class instanceof HandlerInterface.');
        }

        $this->handler = $handler;

        return $this;
    }
574136
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $isExecutingNextHandler = false;
        $next = new class($isExecutingNextHandler, $handler) implements RequestHandlerInterface {
            /**
             * @var bool
             */
            private $isExecutingNextHandler;
            /**
             * @var RequestHandlerInterface
             */
            private $nextHandler;

            public function __construct(bool &$isExecutingNextHandler, RequestHandlerInterface $realHandler)
            {
                $this->isExecutingNextHandler = &$isExecutingNextHandler;
                $this->nextHandler = $realHandler;
            }

            /**
             * Handle the request and return a response.
             */
            public function handle(ServerRequestInterface $request): ResponseInterface
            {
                $this->isExecutingNextHandler = true;
                $result = $this->nextHandler->handle($request);
                $this->isExecutingNextHandler = false;

                return $result;
            }
        };

        try {
            return $this->middleware->process($request, $next);
        } catch (\Throwable $throwable) {
            if ($isExecutingNextHandler) {
                throw $throwable;
            }

            return $handler->handle($request);
        }
    }
369172
public function instantiate(string $handler) : EventHandler
    {
        $handler = ltrim($handler, '\\');
        if (false === $this->container->has($handler)) {
            throw new InvalidArgumentException(
                sprintf('Handler %s could not be found. Did you register it?', $handler)
            );
        }

        return $this->container->get($handler);
    }
1254514
public function setHandler(AbstractHandler $handler)
    {
        $handler->setErrorHandler($this);
        $this->handler = $handler;

        return $this;
    }
858816
public function instantiate(string $handler) : EventHandler
    {
        if (false === isset($this->handlers[$handler])) {
            throw new InvalidArgumentException(
                sprintf('Handler %s could not be found. Did you register it?', $handler)
            );
        }

        $callable = $this->handlers[$handler];

        return ($callable instanceof \Closure) ? $callable() : new $callable();
    }
1172186
protected function resolve(string $handler)
    {
        $handler = $this->split($handler);

        if (is_string($handler)) {
            return function_exists($handler) ? $handler : $this->createClass($handler);
        }

        list($class, $method) = $handler;

        if ((new ReflectionMethod($class, $method))->isStatic()) {
            return $handler;
        }

        return [$this->createClass($class), $method];
    }
1371099
private function findHandler($handler)
    {
        // if the handler is callable, return it immediately:
        if (is_callable($handler)) {
            return $handler;
        }

        if (!is_string($handler)) {
            throw new ResolverException(sprintf('Cannot resolver handler of type "%s".', gettype($handler)));
        }

        list ($handler, $method) = $this->resolveHandlerAndMethod($handler);

        // if the service parameter is registererd as service, return the
        // service object and its method as callable:
        if ($service = $this->getService($handler)) {
            return null === $method ? $service : [$service, $method];
        }

        if (!class_exists($handler)) {
            return [$handler, $method];
        }
        $err = null;

        set_error_handler(function ($errno, $msg) {
            throw $this->newResolverException($msg);
        });

        try {
            $resolvedHandler = [new $handler, $method];
        } catch (ResolverException $e) {
            $err = $e;
        } catch (\Throwable $t) {
            $err = $this->newResolverException($t->getMessage());
        }

        restore_error_handler();

        if (null !== $err) {
            throw $err;
        }

        return $resolvedHandler;
    }
1080854
public function get($path, callable $handler): RouterInterface
    {
        $ruler = $this->createRuler('GET', $path);
        return $this->addRuler($ruler, $handler);
    }
313888
public function addRecoverableErrorHandler($handler) {
        if ( ! $handler instanceof INativeErrorHandler && ! is_callable($handler)) {
            throw new InvalidHandlerType(
                s('Recoverable error handler must be a callable or an instance of %s.',
                    INativeErrorHandler::class)
            );
        }

        if (is_callable($handler)) {
            $handler = $this->createNativeErrorHandler($handler);
        }

        $this->recoverableErrorHandlers[] = $handler;
    }
1205198
protected function makeClosure($handler)
    {
        if (is_callable($handler)) {
            return $handler;
        }

        if (is_string($handler)) {
            if (!class_exists($handler)) {
                throw new InvalidArgumentException(sprintf('Class "%s" not exists.', $handler));
            }

            if (!in_array(EventHandlerInterface::class, (new \ReflectionClass($handler))->getInterfaceNames(), true)) {
                throw new InvalidArgumentException(sprintf('Class "%s" not an instance of "%s".', $handler, EventHandlerInterface::class));
            }

            return function ($payload) use ($handler) {
                return (new $handler($this->app ?? null))->handle($payload);
            };
        }

        if ($handler instanceof EventHandlerInterface) {
            return function () use ($handler) {
                return $handler->handle(...func_get_args());
            };
        }

        throw new InvalidArgumentException('No valid handler is found in arguments.');
    }
534828
public function handler($handler = null)
    {
        if ($handler !== null) {
            $this->handler = $this->getRealHandler($handler);
        }

        return $this->handler;
    }
1025785
public function setHandler($handler = null)
    {
        $this->handler = null !== $handler ? (string)$handler: $handler;

        return $this;
    }
108448
public function setHandler(callable $handler = null)
    {
        $old = $this->handler;
        $this->handler = $handler;

        return $old;
    }
1099452
public function pushHandler($handler)
    {
        if (is_callable($handler)) {
            $handler = new CallbackHandler($handler);
        }

        if (!$handler instanceof HandlerInterface) {
            throw new InvalidArgumentException(
                  "Argument to " . __METHOD__ . " must be a callable, or instance of "
                . "Whoops\\Handler\\HandlerInterface"
            );
        }

        $this->handlerStack[] = $handler;
        return $this;
    }
802397
public function subscribe($channel, callable $handler)
    {
        $loop = $this->client->pubSubLoop();

        $loop->subscribe($channel);

        foreach ($loop as $message) {
            /** @var \stdClass $message */
            if ($message->kind === 'message') {
                call_user_func($handler, Utils::unserializeMessagePayload($message->payload));
            }
        }

        unset($loop);
    }
223857
public function addListener($name, callable $handler)
    {
        $this->assertEventName($name);
        $this->getEventDispatcher()->addListener($name, $handler);
        return $this;
    }