/** * @param \Janeiro\Request\AbstractRequest $request * @throws \Janeiro\Exception * @return \Janeiro\Response */ public function indexAction(AbstractRequest $request) { $response = null; if (1 === preg_match('/^[\\/A-Za-z]+$/', $request->getParameter('uri'), $uri)) { $namespaces = (array) $this->registry->get('routing.namespace'); $segments = explode('/', $uri[0]); $file = array_pop($segments) . '.js'; foreach ($namespaces as $namespace) { $path = [ROOT, 'src', str_replace("\\", DS, $namespace), $segments[0], 'View', 'JavaScript']; $path = implode(DS, array_merge($path, array_slice($segments, 1))); if (is_readable($path . DS . $file)) { $this->view->setTemplateDirectory($path); $this->view->setTemplate($file); $response = new Response('text/javascript'); $response->setHeader('Expires', 'Wed, 01 Jan 2014 00:00:00 GMT'); $response->setHeader('Cache-Control', 'no-store, no-cache, must-revalidate'); $response->setHeader('Pragma', 'no-cache'); $response->setContent($this->view->render()); $response->setStatus(200); break; } } } if (empty($response)) { throw new Exception('Resource not found', Exception::HTTP_NOT_FOUND); } return $response; }
/** * @param \Janeiro\Request\AbstractRequest $request * @template User/Create.phtml */ public function createAction(AbstractRequest $request) { $username = $request->getParameter(2); $password = $request->getParameter(3) === false ? null : hash('SHA512', $request->getParameter(3)); if (false !== $username) { try { if (false !== $this->users->findOneByUsername($username)) { throw new \Exception(sprintf('User "%s" already exists!', $username)); } $this->users->create(['username' => $username, 'password' => $password]); $this->view->set('message', sprintf('User "%s" created.', $username)); } catch (\Exception $e) { $this->view->set('message', $e->getMessage()); } } else { $this->view->setTemplate('User/Index.txt'); } echo $this->view->render(); }
/** * @expectedException \Janeiro\Mvc\Exception * @expectedExceptionMessage Could not create view from template "Bar" */ public function testExceptionThrownWhenTemplateDoesNotExist() { $this->view->setTemplate('Bar'); $this->view->setTemplateDirectory('Foo'); $this->view->render(); }