Exemplo n.º 1
0
 /**
  * @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;
 }
Exemplo n.º 2
0
 /**
  * @method POST
  * @param AbstractRequest $request
  * @return \Janeiro\Response
  */
 public function createAction(AbstractRequest $request)
 {
     $response = new Response();
     $response->setStatus(401);
     try {
         $username = $request->getParameter('username');
         $password = $request->getParameter('password');
         if ($this->userAuthentication->validate($username, $password)) {
             $this->userSession->create($username);
             $response->setStatus(201);
         }
         usleep(250 * 1000);
     } catch (\Exception $e) {
         $response->setStatus(500);
     }
     return $response;
 }
Exemplo n.º 3
0
 /**
  * @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();
 }
Exemplo n.º 4
0
 /**
  * Create controller object
  *
  * @param \Janeiro\Request\AbstractRequest $request
  * @throws \Janeiro\Mvc\Exception
  * @return array
  */
 private function getController(AbstractRequest $request)
 {
     if (false === ($target = $this->getControllerClassAndMethod())) {
         throw new Exception('Resource not found', Exception::HTTP_NOT_FOUND);
     }
     list($class, $method) = $target;
     $controllerReflector = $this->container->getFactory($class)->getReflection();
     $methodAnnotation = $controllerReflector->getAnnotation('method', $method, $request->getMethod());
     if (false === in_array($request->getMethod(), explode(',', $methodAnnotation))) {
         throw new Exception(sprintf('%s method not allowed', $request->getMethod()), Exception::HTTP_METHOD_NOT_ALLOWED);
     }
     $this->view->init($class, $method);
     return [$this->container->resolve($class, null), $method];
 }
Exemplo n.º 5
0
 /**
  * @param \Janeiro\Request\AbstractRequest $request
  * @return \Janeiro\Response
  */
 public function indexAction(AbstractRequest $request)
 {
     $translations = $this->translation->get($request->getParameter('locale'), $request->getParameter('module'));
     return new Response('application/json', json_encode($translations));
 }
Exemplo n.º 6
0
 /**
  * Match request against given route
  *
  * @param \Janeiro\Request\AbstractRequest $request
  * @param array &$route
  * @throws \Janeiro\Mvc\Exception
  * @return int|bool
  */
 private function match(AbstractRequest $request, &$route)
 {
     $match = false;
     $pattern = str_replace('/', '\\/', trim($route['pattern'], '/'));
     $pattern = str_replace(array_keys(self::$expressions), array_values(self::$expressions), $pattern);
     if (preg_match('/^' . $pattern . '$/', implode('/', $request->get()), $matches)) {
         if (false === empty($route['parameters']['deny'])) {
             throw new Exception('access denied');
         }
         $match = $this->rate($route['parameters'], $matches);
     }
     return $match;
 }
Exemplo n.º 7
0
 /**
  * @param \Janeiro\Request\AbstractRequest
  * @template Index.phtml
  * @return string
  */
 public function indexAction(AbstractRequest $request)
 {
     $this->view->set('locale', $request->getLanguage());
     $this->view->set('hash', $this->session->getHash());
     return $this->view->render();
 }
Exemplo n.º 8
0
 /**
  * @param \Janeiro\Request\AbstractRequest $request
  * @template Schema/Repository.phtml
  */
 public function repositoryAction(AbstractRequest $request)
 {
     $this->view->set('entity', CamelCase::to($request->getParameter(2)));
     echo $this->view->render();
 }
Exemplo n.º 9
0
 /**
  * @param \Janeiro\Request\AbstractRequest $request
  * @return string
  */
 public function indexAction(AbstractRequest $request)
 {
     return sprintf('variable is %d', $request->getParameter('variable'));
 }