Esempio n. 1
0
 public function render($name, $params = [])
 {
     if (!$this->template) {
         $this->template = $this->container->get(TemplateRendererInterface::class);
     }
     return $this->template->render($name, $params);
 }
Esempio n. 2
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $userId = $request->getAttribute('user_id');
     $user = $this->userFinder->findById($userId);
     $todos = $this->todoFinder->findByAssigneeId($userId);
     return new HtmlResponse($this->templates->render('page::user-todo-list', ['user' => $user, 'todos' => $todos]));
 }
Esempio n. 3
0
 /**
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  * @return ResponseInterface
  */
 public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
 {
     $users = [];
     $this->queryBus->dispatch(new GetAllUsers())->then(function ($result) use(&$users) {
         $users = $result;
     });
     return new HtmlResponse($this->templates->render('page::user-list', ['users' => $users]));
 }
Esempio n. 4
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  *
  * @return HtmlResponse|RedirectResponse
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     if ($request->getMethod() === 'POST') {
         $this->albumService->addAlbum($request->getParsedBody());
         return new RedirectResponse($this->router->generateUri('album.index'));
     }
     return new HtmlResponse($this->template->render('album::add', ['form' => $this->form]));
 }
Esempio n. 5
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @return HtmlResponse
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     /**
      * @var Session $session
      */
     $session = $request->getAttribute('session');
     $flashMessage = $session->getSegment('App\\Album')->getFlash('flash');
     $albums = $this->albumService->listAlbums();
     return new HtmlResponse($this->template->render('album::index', compact('albums', 'flashMessage')));
 }
Esempio n. 6
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable|null $next
  * @return HtmlResponse
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     $templateName = $this->defaultTemplateName;
     $layoutName = $this->defaultLayoutName;
     if (isset($request->getQueryParams()['t'])) {
         $templateName = $request->getQueryParams()['t'];
     }
     if (isset($request->getQueryParams()['l'])) {
         $layoutName = $request->getQueryParams()['l'];
     }
     return new HtmlResponse($this->template->render('proto::' . $templateName, ['layout' => 'proto-layout::' . $layoutName]));
 }
Esempio n. 7
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  *
  * @return HtmlResponse|RedirectResponse
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     try {
         $album = $this->albumService->getAlbum($request->getAttribute('id'));
     } catch (\Exception $e) {
         return new HtmlResponse($this->template->render('error::404'), 404);
     }
     if ($request->getMethod() === 'POST') {
         $this->albumService->addAlbum($request->getParsedBody());
         return new RedirectResponse($this->router->generateUri('album.index'));
     }
     return new HtmlResponse($this->template->render('album::edit', ['album' => $album, 'form' => $this->form]));
 }
Esempio n. 8
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $userId = $request->getAttribute('user_id');
     $user = null;
     $this->queryBus->dispatch(new GetUserById($userId))->then(function ($result) use(&$user) {
         $user = $result;
     });
     $todos = null;
     $this->queryBus->dispatch(new GetTodosByAssigneeId($userId))->then(function ($result) use(&$todos) {
         $todos = $result;
     });
     return new HtmlResponse($this->templates->render('page::user-todo-list', ['user' => $user, 'todos' => $todos]));
 }
Esempio n. 9
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  * @return HtmlResponse
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $userId = $request->getAttribute('user_id');
     $invalidUser = true;
     $user = null;
     if ($userId) {
         $user = $this->userFinder->findById($userId);
         if ($user) {
             $invalidUser = false;
         }
     }
     return new HtmlResponse($this->templates->render('page::user-todo-form', ['invalidUser' => $invalidUser, 'user' => $user]));
 }
Esempio n. 10
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  * @return HtmlResponse
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $userId = $request->getAttribute('user_id');
     $invalidUser = true;
     $user = null;
     if ($userId) {
         $user = null;
         $this->queryBus->dispatch(new GetUserById($userId))->then(function ($result) use(&$user) {
             $user = $result;
         });
         if ($user) {
             $invalidUser = false;
         }
     }
     return new HtmlResponse($this->templates->render('page::user-todo-form', ['invalidUser' => $invalidUser, 'user' => $user]));
 }
 /**
  * Create a 404 response.
  *
  * If we have a template renderer composed, renders the 404 template into
  * the response.
  *
  * @param Request $request
  * @param Response $response
  * @return Response
  */
 private function create404(Request $request, Response $response)
 {
     if ($this->renderer) {
         $response->getBody()->write($this->renderer->render($this->template404, ['uri' => $request->getUri()]));
     }
     return $response->withStatus(404);
 }
Esempio n. 12
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  *
  * @return HtmlResponse|RedirectResponse
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     try {
         $album = (array) $this->albumService->getAlbum($request->getAttribute('id'));
     } catch (\Exception $e) {
         return new HtmlResponse($this->template->render('error::404'), 404);
     }
     if ($request->getMethod() === 'POST') {
         $body = new Parameters($request->getParsedBody());
         $del = $body->get('del', 'No');
         if (strtolower($del) === 'yes') {
             $this->albumService->deleteAlbum($album);
         }
         return new RedirectResponse($this->router->generateUri('album.index'));
     }
     return new HtmlResponse($this->template->render('album::delete', ['album' => $album]));
 }
Esempio n. 13
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     try {
         /**
          * @var Session $session
          */
         $session = $request->getAttribute('session');
         $album = new Album();
         $this->form->bind($album);
         $this->form->get('submit')->setValue('Add');
         if ($request->getMethod() === 'POST') {
             $this->albumService->addAlbum($request->getParsedBody());
             $session->getSegment('App\\Album')->setFlash('flash', ['type' => 'success', 'message' => sprintf('Successfully added album %s (%s)', $album->getTitle(), $album->getArtist())]);
             return new RedirectResponse($this->router->generateUri('album.index'));
         }
     } catch (\Exception $e) {
         // perhaps log an error and display a message to the user
     }
     return new HtmlResponse($this->template->render('album::add', ['form' => $this->form]));
 }
Esempio n. 14
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     try {
         $album = $this->albumService->getAlbum($request->getAttribute('id'));
         if ($request->getMethod() === 'POST') {
             $body = new Parameters($request->getParsedBody());
             $del = $body->get('del', 'No');
             if (strtolower($del) === 'yes') {
                 $this->albumService->deleteAlbum($album);
                 /**
                  * @var Session $session
                  */
                 $session = $request->getAttribute('session');
                 $session->getSegment('App\\Album')->setFlash('flash', ['type' => 'success', 'message' => sprintf('Successfully deleted album %s (%s)', $album->getTitle(), $album->getArtist())]);
             }
             // Redirect to list of albums
             return new RedirectResponse($this->router->generateUri('album.index'));
         }
     } catch (\Exception $e) {
         // do something useful
     }
     return new HtmlResponse($this->template->render('album::delete', compact('album')));
 }
Esempio n. 15
0
 /**
  * Invoke the controller action
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  * @return \Zend\Diactoros\Response\HtmlResponse
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     $data = [];
     $method = $request->getMethod();
     switch (strtoupper($method)) {
         case 'GET':
             break;
         case 'POST':
             // get the posted data from form
             $post = $request->getParsedBody();
             // @todo this should be dependency injected, but I do not have the time
             $validator = new \Ultradata\Home\Validator\Comment();
             $validator->assert($post);
             $errors = $validator->errors();
             if ($errors) {
                 $data['errors'] = $errors;
             } else {
                 // @todo this should be dependency injected, but I do not have the time
                 $comment = \RedBeanPHP\R::dispense('comments');
                 $comment->name = $post['name'];
                 $comment->email = $post['email'];
                 $comment->date = date('Y-m-d H:i:s');
                 $comment->ipAddress = Stdlib\Http::clientIp();
                 $comment->userAgent = Stdlib\Http::userAgent();
                 // save to database
                 \RedBeanPHP\R::store($comment);
             }
             break;
         default:
             break;
     }
     if (!$this->template) {
         return new JsonResponse(['welcome' => 'Welcome to this test application!', 'method' => $request->getMethod()]);
     }
     return new HtmlResponse($this->template->render('home::index', $data));
 }
Esempio n. 16
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @return HtmlResponse
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     $albums = $this->albumService->listAlbums();
     return new HtmlResponse($this->template->render('album::index', compact('albums')));
 }
 /**
  * @param array $messageData
  * @return string
  */
 private function composeBody(array $messageData) : string
 {
     return $this->renderer->render(self::TEMPLATE, $messageData);
 }
Esempio n. 18
0
 /**
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  * @return ResponseInterface
  */
 public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
 {
     return new HtmlResponse($this->templates->render('page::user-registration'));
 }
Esempio n. 19
0
 /**
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @param callable $next
  * @return ResponseInterface
  */
 public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next)
 {
     $users = $this->userFinder->findAll();
     return new HtmlResponse($this->templates->render('page::user-list', ['users' => $users]));
 }
Esempio n. 20
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  *
  * @return HtmlResponse
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response)
 {
     // album list
     $albums = $this->albumService->getAlbums();
     return new HtmlResponse($this->template->render('album::index', ['albums' => $albums]));
 }
 /**
  * Test action.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @return ResponseInterface
  */
 public function dump(ServerRequestInterface $request, ResponseInterface $response)
 {
     $dump = print_r($this->template, true);
     return $response->write($this->template->render('app::ping', compact('dump')));
 }