예제 #1
0
 /**
  * Handle exception
  * 
  * @param \Exception $e
  * @return \Psr\Http\Message\ResponseInterface
  */
 public function handle(\Exception $e)
 {
     $response = $this->provider->getResponse();
     $response = $response->withStatus(500);
     $message = 'Internal Server Error';
     $view = $this->provider->getView();
     if ($this->provider->getConfig('display_exceptions')) {
         $view->exception = $e;
     }
     $view->message = $message;
     $config = $this->provider->getConfig();
     if (isset($config['templates']['template_error'])) {
         $result = $view->renderFile($config['templates']['template_error']);
     } else {
         if (isset($view->exception)) {
             $body = sprintf("<h1>An error occurred</h1><h2>%s</h2><h3>Message</h3><p>%s</p><h3>Stack trace</h3><p>%s</p>", $message, $e->getMessage(), $e->getTraceAsString());
         } else {
             $body = sprintf("<h1>An error occurred</h1><h2>%s</h2>", $message);
         }
         $result = sprintf("<html><head><title>%s</title><style>body {font-family: Helvetica,Arial,sans-serif;font-size: 20px;line-height: 28px;padding:20px;}</style></head><body>%s</body></html>", 'Tlumx application: ' . $message, $body);
     }
     $response->getBody()->write($result);
     $response->withHeader('Content-Type', 'text/html');
     return $response;
 }
 /**
  * @runInSeparateProcess
  */
 public function testHandleTemplate()
 {
     $file = __DIR__ . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'error.phtml';
     $provider = new ServiceProvider();
     $provider->setConfig(array('templates' => array('template_error' => $file)));
     $handler = new DefaultExceptionHandler($provider);
     $e = new \Exception();
     $response = $handler->handle($e);
     $view = $provider->getView();
     $view->message = 'Internal Server Error';
     $result = $view->renderFile($file);
     $this->assertInstanceOf('Psr\\Http\\Message\\ResponseInterface', $response);
     $body = $response->getBody();
     $body->rewind();
     $this->assertEquals($result, $body->getContents());
 }
예제 #3
0
 /**
  * Handle
  *
  * @param array $allowedMethods
  * @return \Psr\Http\Message\ResponseInterface
  */
 public function handle(array $allowedMethods = [])
 {
     $response = $this->provider->getResponse();
     if (empty($allowedMethods)) {
         $response = $response->withStatus(404);
         $message = 'Page not found';
     } else {
         $response = $response->withStatus(405);
         $message = 'Method Not Allowed';
     }
     $config = $this->provider->getConfig();
     if (isset($config['templates']['template_404'])) {
         $view = $this->provider->getView();
         $view->message = $message;
         $result = $view->renderFile($config['templates']['template_404']);
     } else {
         $body = sprintf("<h1>An error occurred</h1><h2>%s</h2>", $message);
         $result = sprintf("<html><head><title>%s</title><style>body {font-family: Helvetica,Arial,sans-serif;font-size: 20px;line-height: 28px;padding:20px;}</style></head><body>%s</body></html>", 'Tlumx application: ' . $message, $body);
     }
     $response->getBody()->write($result);
     $response->withHeader('Content-Type', 'text/html');
     return $response;
 }
예제 #4
0
 /**
  * Dispatch
  * 
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  * @return ResponseInterface
  * @throws Exception\InvalidRouterResultException
  * @throws Exception\InvalidRouterException
  * @throws Exception\ControllerNotFoundException
  * @throws Exception\InvalidControllerException
  */
 protected function dispatch(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $em = $this->serviceProvider->getEventManager();
     $event = new Event('', array('application' => $this));
     $em->trigger($event->setName(self::EVENT_PRE_DISPATCH));
     $request = $this->serviceProvider->getRequest();
     $result = $request->getAttribute('router_result');
     if ($result instanceof RouterResult === false) {
         throw new Exception\InvalidRouterResultException("Router result not isset in Request");
     }
     $handler = $result->getHandler();
     $handler['controller'] = isset($handler['controller']) ? $handler['controller'] : 'index';
     $handler['action'] = isset($handler['action']) ? $handler['action'] : 'index';
     if (!is_string($handler['controller'])) {
         throw new Exception\InvalidRouterException('Invalid controller name in route handler');
     }
     if (!is_string($handler['action'])) {
         throw new Exception\InvalidRouterException('Invalid action name in route handler');
     }
     $controllerName = $handler['controller'];
     $controllers = $this->getConfig('controllers');
     $controllerClass = isset($controllers[$controllerName]) ? $controllers[$controllerName] : null;
     if (!class_exists($controllerClass)) {
         throw new Exception\ControllerNotFoundException(sprintf('Controller "%s" not exist.', $controllerName));
     }
     $r = new \ReflectionClass($controllerClass);
     if (!$r->isSubclassOf('Tlumx\\Controller')) {
         throw new Exception\InvalidControllerException(sprintf('Controller "%s" is not an instance of Tlumx\\Controller.', $controllerClass));
     }
     $request = $request->withAttribute('router_result_handler', $handler);
     $this->serviceProvider->setRequest($request);
     $controller = new $controllerClass($this->serviceProvider);
     $controller->run();
     $request = $this->serviceProvider->getRequest();
     $response = $this->serviceProvider->getResponse();
     $em->trigger($event->setName(self::EVENT_POST_DISPATCH));
     $request = $this->serviceProvider->getRequest();
     return $next($request, $response);
 }