Exemplo n.º 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);
 }
Exemplo n.º 2
0
 /**
  * Generate a full URL for a given key parameter
  *
  * @param $key
  * @return null|string|void
  * @throws InvalidArgumentException
  */
 public function generateRoute($key)
 {
     if (!empty($key)) {
         $router = new Router(Service::get('config')->get('routes'));
         $url = $router->buildUrl($key);
         return $url;
     } else {
         throw new InvalidArgumentException('Cannot generate url for empty key.');
     }
 }
Exemplo n.º 3
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();
 }
Exemplo n.º 4
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();
 }
Exemplo n.º 5
0
 /**
  * Starts application
  *
  * @access public
  * @return void
  */
 public function run()
 {
     try {
         $route = $this->router->getRoute();
         if ($route == null) {
             throw new HttpNotFoundException("Page not found!", 404);
         }
         $response = $this->getResponse($route['controller'], $route['action'], $route['args']);
         if (!$response instanceof Response) {
             throw new BadResponseException('Wrong type of Response!');
         }
     } catch (DatabaseException $e) {
         $errorMessage = 'Sorry for the inconvenience. We are working to resolve this issue.
         Thank you for your patience.';
         $code = 500;
     } catch (HttpNotFoundException $e) {
         $code = $e->getCode();
         $errorMessage = 'Page not found!';
     } catch (BadControllerException $e) {
         $errorMessage = 'Sorry for the inconvenience. We are working to resolve this issue.
         Thank you for your patience.';
         $code = 500;
     } catch (BadTokenException $e) {
         $errorMessage = $e->getMessage();
         $code = $e->getCode();
     } catch (SecurityException $e) {
         $errorMessage = 'Sorry for the inconvenience. We are working to resolve this issue.
         Thank you for your patience.';
         $code = 500;
     } catch (BadResponseException $e) {
         $errorMessage = 'Sorry for the inconvenience. We are working to resolve this issue.
         Thank you for your patience.';
         $code = 500;
     } catch (\Exception $e) {
         $errorMessage = 'Sorry for the inconvenience. We are working to resolve this issue.
         Thank you for your patience.';
         $code = 500;
     } finally {
         if (isset($e) || !isset($response)) {
             $response = MainException::handleForUser($e, array('code' => $code, 'message' => $errorMessage));
         }
     }
     $response->send();
 }
Exemplo n.º 6
0
 public function run()
 {
     $map = $this->config['routes'];
     /* Выбрался массив с роутами  */
     $match_route = new Router($map);
     $route = $match_route->findRoute();
     $controller = new $route['controller']();
     $action = $route['action'] . 'Action';
     $vars = null;
     if (class_exists($route['controller'])) {
         $controller_reflection = new \ReflectionClass($route['controller']);
         if ($controller_reflection->hasMethod($action)) {
             $method = new \ReflectionMethod($controller, $action);
             $params = $method->getParameters();
             if (empty($params)) {
                 $method->invoke(new $controller());
             } else {
                 new RouteException();
             }
         } else {
             new FrameworkException();
         }
     }
     //определили контролер
     //определили екшен
     /*
             $controller = new stdClass();
             $response = $controller->$action(параметры);
             if($response instanceof ResponseInterface)
             {
                 if($response->type == 'html')
                 {
                     //TODO: доделать рендер, 
                     $renderer = new Renderer($view, $data);
                     $wrapped = $renderer->render($main_layout, array('content' => $response->getContent()));
                     $response = new Response($wrapped);
                 }
                 $response->send();
             }else{
                 throw new BadResponseExeption();
             }
             $response->send();*/
 }
Exemplo n.º 7
0
 /**
  * Create Router class and call function that
  * process URL address
  *
  * Check using reflection presence of necessary class
  * of controller and his methods
  *
  * Create Response class
  */
 public function run()
 {
     $response = null;
     $router = new Router(include '../app/config/routes.php');
     $route = $router->parseUrl(trim(strip_tags($_SERVER['REQUEST_URI'])));
     try {
         if (!empty($route)) {
             $controllerReflection = new \ReflectionClass($route['controller']);
             $action = $route['action'] . 'Action';
             if ($controllerReflection->hasMethod($action)) {
                 // Control user role
                 if ($action != 'indexAction' && $action != 'loginAction' && $action != 'signinAction') {
                     if ($_SESSION['role'] != 'ROLE_USER') {
                         throw new AuthRequredException('You must login');
                     }
                 }
                 $controller = $controllerReflection->newInstance();
                 if ($controller instanceof Controller) {
                     $actionReflection = $controllerReflection->getMethod($action);
                     ActiveRecord::getDBCon();
                     $response = $actionReflection->invokeArgs($controller, $route['params']);
                     if ($response instanceof Response) {
                         $response->send();
                         // Close database connection
                         call_user_func(Service::get('event')->trigger('db_close'));
                     } else {
                         throw new BadResponseTypeException('Missing type of response');
                     }
                 } else {
                     throw new BadControllerTypeException('Missing type of controller');
                 }
             }
         } else {
             throw new HttpNotFoundException('Route not found');
         }
     } catch (HttpNotFoundException $e) {
         // Render 404 or just show msg
         $error = $e->getMessage();
         include Service::get('config')->get('error_404');
     } catch (AuthRequredException $e) {
         // Reroute to login page
         Service::get('session')->addFlush('error', $e->getMessage());
         $response = new ResponseRedirect("/login");
         $response->sendHeaders();
     } catch (InvalidArgumentException $e) {
         echo $e->getMessage();
     } catch (BadResponseTypeException $e) {
         echo $e->getMessage();
     } catch (BadPathTypeException $e) {
         echo $e->getMessage();
     } catch (\PDOException $e) {
         echo $e->getMessage();
     } catch (DatabaseException $e) {
         echo $e->getMessage();
     } catch (InvalidTypeException $e) {
         echo $e->getMessage();
     } catch (BadControllerTypeException $e) {
         echo $e->getMessage();
     } catch (\Exception $e) {
         // Do 500 layout...
         include Service::get('config')->get('error_500');
     }
 }