/** * @param ContainerInterface $container */ public function registerRoutes(ContainerInterface $container) { if (file_exists($this->getPath() . '/config/routing.php')) { // RouterInterface is used within routing.php $router = $container->resolve(RouterInterface::class); require_once $this->getPath() . '/config/routing.php'; } }
/** * */ public function run() { $router = $this->container->resolve(RouterInterface::class); $request = $this->container->resolve(RequestInterface::class); $dispatcher = $this->container->resolve(DispatcherInterface::class); $response = null; if ($payload = $router->resolve($request)) { $response = $dispatcher->dispatch($this->container, $payload); if (null === $response || !$response instanceof Response) { throw new \RuntimeException('Controllers must return a ResponseInterface implementation'); } $response->send(); } }
/** * @param ContainerInterface $container * @param array $payload * * @return string|null */ public function dispatch(ContainerInterface $container, array $payload) { $controllerClass = $payload['_controller']; $method = $payload['_method']; if (substr($method, -6) !== 'Action') { $method .= 'Action'; } $reflection = new \ReflectionMethod($controllerClass, $method); $args = []; foreach ($reflection->getParameters() as $param) { if (array_key_exists($param->name, $payload)) { $args[] = $payload[$param->name]; } elseif ($param->getClass() && ($object = $container->resolve($param->getClass()->name))) { $args[] = $object; } elseif ($param->isDefaultValueAvailable()) { $args[] = $param->getDefaultValue(); } } if ($controller = $container->resolve($controllerClass)) { return $reflection->invokeArgs($controller, $args); } }