Guest User

Untitled

a guest
Jan 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. <?php
  2. class Rou {
  3.     var $core;
  4.     var $config;
  5.     var $pathinfo;
  6.     var $main;
  7.     var $app;
  8.     public function __construct(){
  9.         $this->core = new Cor;
  10.     }
  11.     public function init($config){
  12.         $pathinfo = array_slice(explode('/', str_replace($config['url_suffix'], '', $_SERVER['PATH_INFO'])), 1);
  13.  
  14.         if(file_exists("controllers/{$config['default_route']}.php")) require "controllers/{$config['default_route']}.php";
  15.         if(file_exists("controllers/{$pathinfo[0]}.php")) require "controllers/{$pathinfo[0]}.php";
  16.  
  17.         if(class_exists($config['default_route'])) $main = new $config['default_route'];   
  18.         if(class_exists(String::lower($pathinfo[0]))) $app = new $pathinfo[0];
  19.  
  20.         if(isset($config)) $this->config = $config;
  21.         if(isset($pathinfo)) $this->pathinfo = $pathinfo;
  22.         if(isset($main)) $this->main = $main;
  23.         if(isset($app)) $this->app = $app;
  24.  
  25.         $this->global_func('before');
  26.         if($pathinfo[0] == '' && in_array('index', get_class_methods($main))){ #Is this is the root of the website?
  27.             $main->index(); #Show the index of the default route
  28.         } elseif(isset($pathinfo[1]) && isset($app) && in_array($pathinfo[1], get_class_methods($app))){ #Are we inside of a route?
  29.             call_user_func_array(array($app, $pathinfo[1]), array_slice($pathinfo, 1)); #Yep, let's call that function.
  30.         } elseif(!isset($pathinfo[1]) && isset($app) && in_array('index', get_class_methods($app))) { #Are we in a route and is there an index on this route?
  31.             $app->index(); #Show it
  32.         } elseif(in_array($pathinfo[0], get_class_methods($main))){ #Not anywhere else? Is it in the main route?
  33.             call_user_func_array(array($main, $pathinfo[0]), array_slice($pathinfo, 1)); #Yes! let's call it.
  34.         } else { #Nothing else :(
  35.             $this->core->error(404);
  36.         }
  37.         $this->global_func('after');
  38.     }
  39.     private function global_func($func){
  40.         if(class_exists($this->config['default_route']) && in_array($func, get_class_methods($this->main))) $this->main->$func();
  41.         if(isset($this->app) && class_exists($this->pathinfo[0]) && in_array($func, get_class_methods($this->app))) $this->app->$func();
  42.     }
  43. }
Add Comment
Please, Sign In to add comment