示例#1
0
 public function run()
 {
     $router = new Router(include '../app/config/routes.php');
     $route = $router->parseRoute($_SERVER['REQUEST_URI']);
     if (!empty($route)) {
     } else {
     }
     echo '<pre>';
     print_r($route);
 }
示例#2
0
 /**
  *  The method starts the app
  */
 public function run()
 {
     $router = new Router(Service::get('routes'));
     $route = $router->parseRoute();
     Service::set('currentRoute', $route);
     try {
         if (!empty($route)) {
             /**
              * Checks the route is allowed for all or not
              */
             if (array_key_exists('security', $route)) {
                 $user = Service::get('security')->getUser();
                 $allowed = Service::get('security')->checkGrants($user);
                 if (!$allowed) {
                     throw new AccessDenyException();
                 }
             }
             $controllerReflection = new \ReflectionClass($route['controller']);
             $action = $route['action'] . 'Action';
             if ($controllerReflection->hasMethod($action)) {
                 if ($controllerReflection->isInstantiable()) {
                     $controller = $controllerReflection->newInstance();
                     $actionReflection = $controllerReflection->getMethod($action);
                     $response = $actionReflection->invokeArgs($controller, $route['params']);
                     if (!$response instanceof Response) {
                         throw new BadResponseTypeException('Bad response');
                     }
                 } else {
                     throw new BadResponseTypeException('Bad response');
                 }
             } else {
                 throw new HttpNotFoundException('The page has not found');
             }
         } else {
             throw new HttpNotFoundException('The page has not found');
         }
     } catch (AccessDenyException $e) {
         $response = new ResponseRedirect('/login');
     } catch (HttpNotFoundException $e) {
         $renderer = new Renderer();
         $params = $e->getParams();
         $content = $renderer->render(Service::get('config')['error_500'], $params);
         $response = new Response($content, 'text/html', '404');
     } catch (BadResponseTypeException $e) {
         $renderer = new Renderer();
         $params = $e->getParams();
         $content = $renderer->render(Service::get('config')['error_500'], $params);
         $response = new Response($content, 'text/html', '500');
     } catch (\Exception $e) {
         $response = new Response($e->getMessage(), 'text/html', '200');
     }
     $response->send();
 }
示例#3
0
 /**
  * Run Router class and show resalt of parseRoute()
  */
 public function run()
 {
     $router = new Router($this->config['routes']);
     $routeInfo = $router->parseRoute();
     try {
         if (is_array($routeInfo)) {
             Service::set('route', $routeInfo);
             //Security - user Role Verification
             Service::get('security')->verifyUserRole();
             // Security - validation token
             Service::get('security')->verifyCsrfToken();
             $controllerName = $routeInfo['controller'];
             $actionName = $routeInfo['action'] . 'Action';
             $params = $routeInfo['params'];
             $reflectionClass = new \ReflectionClass($controllerName);
             if ($reflectionClass->isInstantiable()) {
                 $reflectionObj = $reflectionClass->newInstanceArgs();
                 if ($reflectionClass->hasMethod($actionName)) {
                     $reflectionMethod = $reflectionClass->getMethod($actionName);
                     $response = $reflectionMethod->invokeArgs($reflectionObj, $params);
                     if (!$response instanceof Response) {
                         throw new \Exception('Method - <b>' . $actionName . '</b> return not instance of class Response');
                     }
                 } else {
                     throw new \Exception('Can not find Method - ' . $actionName);
                 }
             } else {
                 throw new \Exception('Can not create Object from Class - ' . $controllerName);
             }
         } else {
             throw new HttpNotFoundException('Page Not Found');
         }
     } catch (RoleException $e) {
         $response = new ResponseRedirect('/login');
     } catch (HttpNotFoundException $e) {
         $content = $e->getExceptionContent('Error - 404');
         $response = new Response($content, 404);
     } catch (CustomException $e) {
         $content = $e->getExceptionContent();
         $response = new Response($content, 500);
     } catch (\Exception $e) {
         $response = new Response('<b>Message:</b> ' . $e->getMessage() . '<br />');
     }
     $response->send();
 }