Advertisement
Guest User

Untitled

a guest
Nov 12th, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.88 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Sparkle;
  4.  
  5. class Application
  6. {
  7.     public $router;
  8.  
  9.     public $config;
  10.  
  11.     public $errors;
  12.  
  13.     public function __construct($config)
  14.     {
  15.         if (is_null($config['path'])) $config['path'] = '/';
  16.         if (is_null($config['views'])) return trigger_error('\'View\' path parameter is required', E_USER_ERROR);
  17.  
  18.         $this->config = $config;
  19.         $this->router = new Router($this, $this->config['path']);
  20.  
  21.         if (!is_null($this->config['dev']) AND $this->config['dev'] == true) {
  22.             /**
  23.              * Developer mode
  24.              */
  25.             ini_set('error_reporting', E_ALL);
  26.             ini_set('display_errors', 'On');
  27.         }
  28.     }
  29.  
  30.     public function render($filename, $array = [])
  31.     {
  32.         \Twig_Autoloader::register();
  33.  
  34.         $loader = new \Twig_Loader_Filesystem($this->config['views']);
  35.  
  36.         $twig = new \Twig_Environment($loader);
  37.  
  38.         return $twig->render($filename, $array);
  39.     }
  40.  
  41.     public function error($error, $method)
  42.     {
  43.         $this->errors[] = [
  44.             "error" => $error,
  45.             "method" => $method
  46.         ];
  47.  
  48.         return $this;
  49.     }
  50.  
  51.     public function terminate($reason = null)
  52.     {
  53.         if (is_null($reason)) return die();
  54.         if (is_null($this->errors)) return die("{$reason}");
  55.  
  56.         foreach ($this->errors as $error) {
  57.             if ($error['error'] != $reason) {
  58.                 continue;
  59.             } else {
  60.                 if (is_callable($error['method']) AND @!preg_match('/[a-z0-9_-]+@+[a-z0-9_-]+/i', $error['method'])) {
  61.                     echo call_user_func($error['method'], $this);
  62.                 } elseif (preg_match('/[a-z0-9_-]+@+[a-z0-9_-]+/i', $error['method'])) {
  63.                     $controller_name = $this->router->namespace . Helper::array_split($error['method'], 0);
  64.                     $controller_method = Helper::array_split($found_route['method'], 1);
  65.  
  66.                     $controller = new $controller_name($this);
  67.                     if (method_exists($controller, $controller_method)) {
  68.                         echo $controller->{$controller_method}();
  69.                     } else {
  70.                         return trigger_error('Method does not exist!', E_USER_ERROR);
  71.                     }
  72.                 } else {
  73.                     return trigger_error('Function does not exist!', E_USER_ERROR);
  74.                 }
  75.                 break;
  76.             }
  77.         }
  78.     }
  79.  
  80.     public function run()
  81.     {
  82.         $this->router->dispatch();
  83.     }
  84. }
  85.  
  86. class Controller
  87. {
  88.     public $app;
  89.  
  90.     public function __construct(Application $app)
  91.     {
  92.         $this->app = $app;
  93.     }
  94. }
  95.  
  96. class Helper
  97. {
  98.     /**
  99.      * Splits an array and returns value of index
  100.      *
  101.      * @return string
  102.      */
  103.     public static function array_split($array, $index)
  104.     {
  105.         $array = explode('@', $array);
  106.  
  107.         return $array[(int)$index];
  108.     }
  109. }
  110.  
  111. class Router
  112. {
  113.     public $routes;
  114.     public $methods = ['GET', 'POST'];
  115.     private $path;
  116.     protected $namespace;
  117.  
  118.     public $app;
  119.  
  120.     private $pass;
  121.  
  122.     public function __construct(Application $app, $path = null, $namespace = null)
  123.     {
  124.         $this->app = $app;
  125.         $this->path = $path;
  126.         $this->namespace = $namespace;
  127.     }
  128.  
  129.     /**
  130.      * Adds a route to $this->routes array
  131.      * @param  string $method
  132.      * @param  array $arguments [Array of params]
  133.      * @return object $this
  134.      */
  135.     public function __call($method, $arguments)
  136.     {
  137.         if (empty($method) or empty($arguments)) return false;
  138.         if (!in_array(strtoupper($method), $this->methods)) return false;
  139.  
  140.         $this->routes[] = [
  141.             "method" => strtoupper($method),
  142.             "route" => $arguments[0],
  143.             "uses" => $arguments[1]
  144.         ];
  145.  
  146.         return $this;
  147.  
  148.     }
  149.  
  150.     public function dispatch()
  151.     {
  152.         $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
  153.         $path = substr($uri, strpos($uri, $this->path) + strlen($this->path));
  154.         $particles['path'] = array_filter(explode('/', $path));
  155.  
  156.         $passed = false;
  157.  
  158.         $found_route;
  159.  
  160.         $variables = [];
  161.  
  162.         foreach ($this->routes as $route) {
  163.  
  164.             if ($route['method'] != $_SERVER['REQUEST_METHOD']) continue;
  165.  
  166.             $particles['route'] = array_filter(explode('/', $route['route']));
  167.  
  168.             if (count($particles['path']) == 1 AND $particles['path'] == $particles['route']) {
  169.                 $passed = true;
  170.                 $found_route = $route;
  171.                 break;
  172.             }
  173.  
  174.             if (count($particles['path']) != count($particles['route'])) {
  175.                 continue;
  176.             }
  177.  
  178.             foreach ($particles['path'] as $key => $value) {
  179.                 if ($value == $particles['route'][$key] or preg_match('/{.*?}/', $particles['route'][$key])) {
  180.                     $this->pass++;
  181.                 }
  182.             }
  183.  
  184.             if ($this->pass == count($particles['path'])) {
  185.                 $passed = true;
  186.                 $found_route = $route;
  187.                 foreach ($particles['route'] as $key => $value) {
  188.                     if (preg_match('/{.*?}/', $value)) {
  189.                         $value = preg_replace('/[{}]/u', '', $value);
  190.                         $variables[$value] = urldecode($particles['path'][$key]);
  191.                     }
  192.                 }
  193.                 break;
  194.             } else {
  195.                 $this->pass = 0;
  196.                 continue;
  197.             }
  198.         }
  199.  
  200.         if ($passed) {
  201.             if (is_callable($found_route['uses']) AND @!preg_match('/[a-z0-9_-]+@+[a-z0-9_-]+/i', $found_route['uses'])) {
  202.                 if (!empty($variables)) {
  203.                     echo call_user_func($found_route['uses'], $this->app, $variables);
  204.                 } else {
  205.                     echo call_user_func($found_route['uses'], $this->app);
  206.                 }
  207.             } elseif (preg_match('/[a-z0-9_-]+@+[a-z0-9_-]+/i', $found_route['uses'])) {
  208.                 $controller_name = $this->namespace . Helper::array_split($found_route['uses'], 0);
  209.                 $controller_method = Helper::array_split($found_route['uses'], 1);
  210.  
  211.                 $controller = new $controller_name($this->app);
  212.                 if (method_exists($controller, $controller_method)) {
  213.                     if (!empty($variables)) {
  214.                         echo $controller->{$controller_method}($variables);
  215.                     } else {
  216.                         echo $controller->{$controller_method}();
  217.                     }
  218.                 } else {
  219.                     return trigger_error('Method does not exist!', E_USER_ERROR);
  220.                 }
  221.             } else {
  222.                 return trigger_error('Function does not exist!', E_USER_ERROR);
  223.             }
  224.         } else {
  225.             return $this->app->terminate(404);
  226.         }
  227.     }
  228. }
  229.  
  230. class Model
  231. {
  232.     public $app;
  233.  
  234.     public $data;
  235.  
  236.     public function __construct(Application $app)
  237.     {
  238.         $this->app = $app;
  239.     }
  240.  
  241.     public function __get($key) {
  242.         return isset($this->data[$key]) ? $this->data[$key] : null;
  243.     }
  244.  
  245.     public function __set($key, $value) {
  246.         $this->data[$key] = $value;
  247.     }
  248.  
  249.     public function save()
  250.     {
  251.         return $this;
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement