Exemplo n.º 1
0
 /**
  * Render specified template file with data provided
  * @param $template_path Template file path (full)
  * @param array $data Data array
  * @param bool|true $wrap To be wrapped with main template if true
  * @return bool|html
  * @throws FileException if template file does not exist
  */
 public function render($template_path, $data = array(), $wrap = true)
 {
     //Launches the appropriate event
     Service::get('eventManager')->trigger('renderAction', "Render specified template file \"" . $template_path . "\" with data provided");
     $data['include'] = function ($controllerName, $actionName, $params) {
         Helper::dispatch($controllerName, $actionName, $params);
     };
     $data['getRoute'] = function ($route_name, $params = array()) {
         return Service::get('router')->buildRoute($route_name, $params);
     };
     $data['generateToken'] = function () {
         echo '<input type="hidden" name="token" value="' . Service::get('session')->get('token') . '"/>';
     };
     $data['route'] = Registry::getConfig('route');
     $data['user'] = Service::get('session')->get('user');
     if (!$wrap) {
         //Gets messages from session message container to display them in view
         $flush = Service::get('session')->get('messages') ? Service::get('session')->get('messages') : array();
         Service::get('session')->del('messages');
     }
     extract($data);
     //Checks if template file exists
     if (file_exists($template_path)) {
         ob_start();
         include $template_path;
         $content = ob_get_contents();
         ob_end_clean();
     } else {
         throw new FileException("File " . $template_path . " does not found");
     }
     if ($wrap) {
         $content = $this->renderMain($content);
     }
     return $content;
 }
Exemplo n.º 2
0
 public function run()
 {
     $router = Service::get('router');
     $route = $router->parseRoute($_SERVER['REQUEST_URI']);
     try {
         //Checks if route is empty
         if (!empty($route)) {
             //Verifies user role if it needs
             if (array_key_exists('security', $route)) {
                 if (is_null($user = Service::get('session')->get('user')) || !in_array($user->role, $route['security'])) {
                     throw new SecurityException("Access is denied");
                 }
             }
             //Returns Response object
             $response = Helper::dispatch($route['controller'], $route['action'], $route['params']);
         } else {
             throw new HttpNotFoundException("Route does not found", 404);
         }
     } catch (SecurityException $e) {
         Service::get('session')->set('returnUrl', Registry::getConfig('route')['pattern']);
         $response = new ResponseRedirect(Service::get('router')->buildRoute('login'));
     } catch (HttpNotFoundException $e) {
         $response = new Response(Service::get('renderer')->render(Registry::getConfig('error_400'), array('code' => $e->getCode(), 'message' => $e->getMessage())));
     } catch (\Exception $e) {
         $response = new Response(Service::get('renderer')->render(Registry::getConfig('error_500'), array('code' => $e->getCode(), 'message' => $e->getMessage())));
     }
     $response->send();
 }