public function invoke($middleware, ServerRequestInterface $request, callable $next) : ResponseInterface { if (!$this->invoker) { $this->invoker = new Invoker(new NumericArrayResolver(), $this->container); } return $this->invoker->call($middleware, [$request, $next]); }
private function dispatch() { $req = Request::createFromGlobals(); $ctx = new RequestContext(); $ctx->fromRequest($req); $matcher = new UrlMatcher($this->routes, $ctx); $pi = $req->getPathInfo(); try { $match = $matcher->match($pi); $callable = null; $controller = null; $method = null; if (isset($match['callable'])) { $callable = $match['callable']; } if (isset($match['controller'])) { $controller = $match['controller']; $method = $match['method']; } unset($match['callable']); unset($match['_route']); unset($match['controller']); unset($match['method']); $match['request'] = $req; $response = ''; if ($callable) { $invoker = new Invoker(); $response = $invoker->call($callable, $match); } if ($controller) { if (!class_exists($controller)) { $error = new Response('Invalid method', 501); $error->send(); die; } $controllerInst = new $controller($this->context); $response = call_user_func_array([$controllerInst, $method], array_values($match)); } if (is_object($response) && $response instanceof Response) { $response->send(); } else { if (is_string($response)) { echo $response; } } die; } catch (MethodNotAllowedException $mex) { // let wordpress continue doing what it does. $response = new Response('Method not allowed', 405); $response->send(); } catch (ResourceNotFoundException $ex) { // let wordpress continue doing what it does. } }
/** * Resolve a factory definition to a value. * * This will call the callable of the definition. * * @param FactoryDefinition $definition * * {@inheritdoc} */ public function resolve(Definition $definition, array $parameters = []) { $callable = $definition->getCallable(); if (!is_callable($callable)) { throw new DefinitionException(sprintf('The factory definition "%s" is not callable', $definition->getName())); } if (!$this->invoker) { $this->invoker = new Invoker(new NumericArrayResolver(), $this->container); } return $this->invoker->call($callable, [$this->container]); }
/** * Resolve a factory definition to a value. * * This will call the callable of the definition. * * @param FactoryDefinition $definition * * {@inheritdoc} */ public function resolve(Definition $definition, array $parameters = []) { if (!$this->invoker) { $this->invoker = new Invoker(new NumericArrayResolver(), $this->container); } try { return $this->invoker->call($definition->getCallable(), [$this->container]); } catch (NotCallableException $e) { throw new DefinitionException(sprintf('Entry "%s" cannot be resolved: factory %s', $definition->getName(), $e->getMessage())); } }
public function invoke($middleware, ServerRequestInterface $request, callable $next) : ResponseInterface { if (!$this->invoker) { $this->invoker = $this->createInvoker(); } $parameters = $request->getAttributes(); $parameters['request'] = $request; $parameters['next'] = $next; $newResponse = $this->invoker->call($middleware, $parameters); if (is_string($newResponse)) { // Allow direct string response $newResponse = new HtmlResponse($newResponse); } elseif (!$newResponse instanceof ResponseInterface) { throw new \RuntimeException(sprintf('The controller did not return a response (expected %s, got %s)', ResponseInterface::class, is_object($newResponse) ? get_class($newResponse) : gettype($newResponse))); } return $newResponse; }
/** * Resolve a factory definition to a value. * * This will call the callable of the definition. * * @param FactoryDefinition $definition * * {@inheritdoc} */ public function resolve(Definition $definition, array $parameters = []) { if (!$this->invoker) { $parameterResolver = new ResolverChain([new AssociativeArrayResolver(), new FactoryParameterResolver($this->container), new NumericArrayResolver()]); $this->invoker = new Invoker($parameterResolver, $this->container); } $callable = $definition->getCallable(); try { $providedParams = [$this->container, $definition]; $extraParams = $this->resolveExtraParams($definition->getParameters()); $providedParams = array_merge($providedParams, $extraParams); return $this->invoker->call($callable, $providedParams); } catch (NotCallableException $e) { // Custom error message to help debugging if (is_string($callable) && class_exists($callable) && method_exists($callable, '__invoke')) { throw new DefinitionException(sprintf('Entry "%s" cannot be resolved: factory %s. Invokable classes cannot be automatically resolved if autowiring is disabled on the container, you need to enable autowiring or define the entry manually.', $definition->getName(), $e->getMessage())); } throw new DefinitionException(sprintf('Entry "%s" cannot be resolved: factory %s', $definition->getName(), $e->getMessage())); } catch (NotEnoughParametersException $e) { throw new DefinitionException(sprintf('Entry "%s" cannot be resolved: %s', $definition->getName(), $e->getMessage())); } }
/** * @param ContainerInterface $container the container for injection */ public function __construct(ContainerInterface $container) { parent::__construct(new ResolverChain([new AssociativeArrayResolver(), new TypeHintResolver(), new TypeHintContainerResolver($container), new NumericArrayResolver()])); }