/** * @param Route $route * @return mixed * @throws \Exception */ public function handleRoute(Route $route) { $callback = $route->getCallback(); list($controller, $action) = explode('@', $callback); $controller = str_replace(':', '\\', $controller); $bundle = trim(substr($controller, 0, strpos($controller, 'Controller')), '\\'); $this->getContainer()->singleton('kernel')->setActiveBundle($bundle); unset($bundle); $service = $this->container->set('request_callback', $controller)->get('request_callback'); if (($instance = $service->singleton()) instanceof ControllerInterface) { $instance->setContainer($this->container); $instance->currentAction = $action; } try { try { $response = $service->__initialize(); if ($response instanceof Response) { return $response; } } catch (\Exception $e) { } $response = call_user_func_array([$service, $action], $route->getParameters()); $instance->statusCode = $response->getStatusCode() === 200 ? 1 : 0; if (1 !== $instance->statusCode) { $instance->code = $response->getStatusCode(); $instance->msg = $response->getContent(); } unset($instance); return $response; } catch (HttpException $e) { $response = $e->getContent(); if ($response instanceof Response) { return $response; } return new Response($e->getContent(), $e->getStatusCode(), $e->getHeaders()); } catch (\Exception $e) { if ($this->getContainer()->get('kernel')->isDebug()) { return new Response($e->getMessage(), Response::HTTP_SERVICE_UNAVAILABLE); } return new Response('server interval error.', Response::HTTP_INTERNAL_SERVER_ERROR); } }
/** * Format route output to command line. * * @param Route $route * @param Output $output * @param $type */ public function formatOutput(Route $route, Output $output, $type = self::STYLE_DETAIL) { switch ($type) { case self::STYLE_DETAIL: $output->write('Route ['); $output->write('<success>"' . $route->getName() . '"</success>'); $output->writeln(']'); $output->writeln("Path:\t\t" . str_replace('//', '/', $route->getPath())); $output->writeln("Method:\t\t" . $route->getMethod()); $output->writeln("Format:\t\t" . implode(', ', $route->getFormats())); $output->writeln("Callback:\t" . (is_callable($route->getCallback()) ? 'Closure' : $route->getCallback())); $output->writeln("Defaults:\t" . implode(', ', $route->getDefaults())); $output->writeln("Requirements:\t" . implode(', ', $route->getRequirements())); $output->writeln("Path-Regex:\t" . $route->getPathRegex()); $output->writeln(''); break; case self::STYLE_LIST: default: $name = $route->getName(); $method = $route->getMethod(); $schema = $route->getScheme() ?? 'http'; $path = $route->getPath(); $output->write($name . str_repeat(' ', 25 - strlen($name))); $output->write($method . str_repeat(' ', 15 - strlen($method))); $output->write($schema . str_repeat(' ', 15 - strlen($schema))); $output->writeln($path); } return; }