Exemple #1
0
 /**
  * @param Request     $request Request object
  * @param Entity|null $user
  *
  * @return Response Reponse object
  *
  * @throws \Exception If unexpected exception is thrown
  */
 public function dispatch(Request $request, Entity $user = null) : Response
 {
     $controllerFactory = new ControllerFactory($this->pdo, $this->configuration, $request, $user);
     /**
      * Figure out what controller to use and what action to call
      */
     try {
         /**
          * @var Router $router
          */
         $router = $this->configuration->get('Router');
         list($controllerName, $action, $arguments) = array_values($router->route($request->getPath()));
         /**
          * Validate and initialize controller
          */
         $controller = $controllerFactory->create($controllerName);
     } catch (RouteNonexistentException $e) {
         /**
          * Log nonexistent route (404)
          */
         if ($this->logger) {
             $this->logger->addWarning('Route not connected', ['path' => $request->getPath()]);
         }
         $controller = $controllerFactory->create(NotFoundController::class);
         $action = 'index';
         $arguments = [];
     }
     /**
      * Set arguments
      */
     $controller->setArguments($arguments);
     /**
      * Validate and set controller action
      */
     try {
         $controller->setAction($action);
     } catch (ControllerActionProtectedInsufficientAuthenticationException $e) {
         /**
          * Log unauthed protected controller action (403)
          */
         if ($this->logger) {
             $this->logger->addWarning('Unauthenticated attempt to access protected action', ['path' => $request->getPath(), 'controller' => $controller->getShortName(), 'action' => $action]);
         }
         if ($this->configuration->exists('User.SignIn.Controller.Class') && $this->configuration->exists('User.SignIn.Controller.Action')) {
             /**
              * Save what controller and action was requested and then redirect to sign in form
              */
             // @todo test that this works
             $request->getCookie()->set('SignIn.onSuccess.path', $request->getPath());
             $controller = $controllerFactory->create($this->configuration->get('User.SignIn.Controller.Class'));
             $controller->setAction($this->configuration->get('User.SignIn.Controller.Action'));
         } else {
             $controller = $controllerFactory->create(NotFoundController::class);
             $controller->setAction('index');
         }
     } catch (ControllerActionPrivateInsufficientAuthenticationException $e) {
         /**
          * Log unauthed private controller action (403)
          */
         if ($this->logger) {
             $this->logger->addWarning('Unauthenticated attempt to access private action', ['path' => $request->getPath(), 'controller' => $controller->getShortName(), 'action' => $action]);
         }
     }
     /**
      * Create response from controller action headers and output
      */
     $response = $controller->callAction();
     /**
      * Performance logging
      */
     if ($this->logger) {
         $time = number_format((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000, 2);
         // @todo $_SERVER usage
         $this->logger->addDebug("Dispatched request in {$time} ms", ['path' => $request->getPath()]);
     }
     return $response;
 }
Exemple #2
0
 /**
  * @covers ::getPost
  */
 public function testGetPost()
 {
     $post = ['User.Email' => '*****@*****.**'];
     $request = new Request(new Cookie($this->configuration), [], $post);
     $this->assertSame($post, $request->getPost());
 }
Exemple #3
0
 /**
  * Set flash message
  *
  * Recommended types: error, warning, success & info
  *
  * @param string $message Flash message
  * @param string $type    Flash type
  */
 protected function setFlash(string $message, string $type)
 {
     $cookie = $this->request->getCookie();
     $cookie->set('Flash.Message', $message);
     $cookie->set('Flash.Type', $type);
 }