예제 #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;
 }
예제 #2
0
 /**
  * @template Index.txt
  * @param \Janeiro\Request\Cli $request
  */
 public function indexAction(Cli $request)
 {
     if ($request->getParameter(0)) {
         $this->dispatcher->forward(['controller' => $request->getParameter(0)]);
     } else {
         echo $this->view->render();
     }
 }
예제 #3
0
 public function setup()
 {
     $container = new Container();
     $container->set('view', function () use($container) {
         $view = new View($container);
         $view->registerEngine('Janeiro\\Mvc\\View\\Standard', '.php');
         return $view;
     }, true)->set('router', function () use($container) {
         $router = new Router($container);
         $router->addRoute('/:module/:controller', ['module' => 1, 'controller' => 2, 'action' => 'index']);
         $router->addRoute('/:module/:controller/:action', ['module' => 1, 'controller' => 2, 'action' => 3]);
         $router->addRoute('/test/parameter/variable/:integer', ['module' => 'test', 'controller' => 'parameter', 'parameters' => ['variable' => 1]]);
         return $router;
     }, true);
     $this->dispatcher = new Dispatcher($container);
     $this->container = $container;
 }
예제 #4
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();
 }
예제 #5
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];
 }
예제 #6
0
 /**
  * @template Schema/Update.phtml
  * @return string
  */
 public function updateAction()
 {
     $hash = hash('crc32', serialize($this->adapter->getSchema()));
     $hashCurrent = $hash;
     $this->view->set('current', $hash);
     do {
         $update = ROOT . sprintf($this->registry->get('database.schema'), $hash);
         if (file_exists($update)) {
             try {
                 $this->adapter->getDriver()->query(file_get_contents($update));
             } catch (\Exception $e) {
                 $update = false;
             }
         } else {
             $update = false;
         }
         $hash = hash('crc32', serialize($this->adapter->getSchema()));
     } while (false !== $update && $hash !== $hashCurrent);
     return $this->view->set('new', $hash)->render();
 }
예제 #7
0
 /**
  * @method GET
  * @view \Core\Module\User\View\Session\ItemView
  * @param \Janeiro\Request\AbstractRequest $request
  * @return \Janeiro\Response
  */
 public function showAction(AbstractRequest $request)
 {
     $this->view->set('user_session', $this->userSession->fetch($request->getParameter('id'))->current());
     return new Response('application/json', json_encode($this->view->render()));
 }
예제 #8
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();
 }
예제 #9
0
 /**
  * Return full filename w/ absolute path of template to be rendered
  *
  * @return string
  */
 protected function getFilename()
 {
     return str_replace('/', DS, $this->view->getTemplateDirectory() . '/' . $this->view->getTemplate());
 }
예제 #10
0
 /**
  * @expectedException \Janeiro\Mvc\Exception
  * @expectedExceptionMessage Could not create view
  */
 public function testExceptionThrownWhenViewDoesNotExist()
 {
     $this->view->render();
 }