Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Sparkle;
- class Application
- {
- public $router;
- public $config;
- public $errors;
- public function __construct($config)
- {
- if (is_null($config['path'])) $config['path'] = '/';
- if (is_null($config['views'])) return trigger_error('\'View\' path parameter is required', E_USER_ERROR);
- $this->config = $config;
- $this->router = new Router($this, $this->config['path']);
- if (!is_null($this->config['dev']) AND $this->config['dev'] == true) {
- /**
- * Developer mode
- */
- ini_set('error_reporting', E_ALL);
- ini_set('display_errors', 'On');
- }
- }
- public function render($filename, $array = [])
- {
- \Twig_Autoloader::register();
- $loader = new \Twig_Loader_Filesystem($this->config['views']);
- $twig = new \Twig_Environment($loader);
- return $twig->render($filename, $array);
- }
- public function error($error, $method)
- {
- $this->errors[] = [
- "error" => $error,
- "method" => $method
- ];
- return $this;
- }
- public function terminate($reason = null)
- {
- if (is_null($reason)) return die();
- if (is_null($this->errors)) return die("{$reason}");
- foreach ($this->errors as $error) {
- if ($error['error'] != $reason) {
- continue;
- } else {
- if (is_callable($error['method']) AND @!preg_match('/[a-z0-9_-]+@+[a-z0-9_-]+/i', $error['method'])) {
- echo call_user_func($error['method'], $this);
- } elseif (preg_match('/[a-z0-9_-]+@+[a-z0-9_-]+/i', $error['method'])) {
- $controller_name = $this->router->namespace . Helper::array_split($error['method'], 0);
- $controller_method = Helper::array_split($found_route['method'], 1);
- $controller = new $controller_name($this);
- if (method_exists($controller, $controller_method)) {
- echo $controller->{$controller_method}();
- } else {
- return trigger_error('Method does not exist!', E_USER_ERROR);
- }
- } else {
- return trigger_error('Function does not exist!', E_USER_ERROR);
- }
- break;
- }
- }
- }
- public function run()
- {
- $this->router->dispatch();
- }
- }
- class Controller
- {
- public $app;
- public function __construct(Application $app)
- {
- $this->app = $app;
- }
- }
- class Helper
- {
- /**
- * Splits an array and returns value of index
- *
- * @return string
- */
- public static function array_split($array, $index)
- {
- $array = explode('@', $array);
- return $array[(int)$index];
- }
- }
- class Router
- {
- public $routes;
- public $methods = ['GET', 'POST'];
- private $path;
- protected $namespace;
- public $app;
- private $pass;
- public function __construct(Application $app, $path = null, $namespace = null)
- {
- $this->app = $app;
- $this->path = $path;
- $this->namespace = $namespace;
- }
- /**
- * Adds a route to $this->routes array
- * @param string $method
- * @param array $arguments [Array of params]
- * @return object $this
- */
- public function __call($method, $arguments)
- {
- if (empty($method) or empty($arguments)) return false;
- if (!in_array(strtoupper($method), $this->methods)) return false;
- $this->routes[] = [
- "method" => strtoupper($method),
- "route" => $arguments[0],
- "uses" => $arguments[1]
- ];
- return $this;
- }
- public function dispatch()
- {
- $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
- $path = substr($uri, strpos($uri, $this->path) + strlen($this->path));
- $particles['path'] = array_filter(explode('/', $path));
- $passed = false;
- $found_route;
- $variables = [];
- foreach ($this->routes as $route) {
- if ($route['method'] != $_SERVER['REQUEST_METHOD']) continue;
- $particles['route'] = array_filter(explode('/', $route['route']));
- if (count($particles['path']) == 1 AND $particles['path'] == $particles['route']) {
- $passed = true;
- $found_route = $route;
- break;
- }
- if (count($particles['path']) != count($particles['route'])) {
- continue;
- }
- foreach ($particles['path'] as $key => $value) {
- if ($value == $particles['route'][$key] or preg_match('/{.*?}/', $particles['route'][$key])) {
- $this->pass++;
- }
- }
- if ($this->pass == count($particles['path'])) {
- $passed = true;
- $found_route = $route;
- foreach ($particles['route'] as $key => $value) {
- if (preg_match('/{.*?}/', $value)) {
- $value = preg_replace('/[{}]/u', '', $value);
- $variables[$value] = urldecode($particles['path'][$key]);
- }
- }
- break;
- } else {
- $this->pass = 0;
- continue;
- }
- }
- if ($passed) {
- if (is_callable($found_route['uses']) AND @!preg_match('/[a-z0-9_-]+@+[a-z0-9_-]+/i', $found_route['uses'])) {
- if (!empty($variables)) {
- echo call_user_func($found_route['uses'], $this->app, $variables);
- } else {
- echo call_user_func($found_route['uses'], $this->app);
- }
- } elseif (preg_match('/[a-z0-9_-]+@+[a-z0-9_-]+/i', $found_route['uses'])) {
- $controller_name = $this->namespace . Helper::array_split($found_route['uses'], 0);
- $controller_method = Helper::array_split($found_route['uses'], 1);
- $controller = new $controller_name($this->app);
- if (method_exists($controller, $controller_method)) {
- if (!empty($variables)) {
- echo $controller->{$controller_method}($variables);
- } else {
- echo $controller->{$controller_method}();
- }
- } else {
- return trigger_error('Method does not exist!', E_USER_ERROR);
- }
- } else {
- return trigger_error('Function does not exist!', E_USER_ERROR);
- }
- } else {
- return $this->app->terminate(404);
- }
- }
- }
- class Model
- {
- public $app;
- public $data;
- public function __construct(Application $app)
- {
- $this->app = $app;
- }
- public function __get($key) {
- return isset($this->data[$key]) ? $this->data[$key] : null;
- }
- public function __set($key, $value) {
- $this->data[$key] = $value;
- }
- public function save()
- {
- return $this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement