public static function get_instance() { if (empty(self::$instance)) { self::$instance = new tr_hook(); } return self::$instance; }
function route() { tr_hook::add("404", function () { echo "Page Not found"; }); $routeConfig = tr_config::config()->get("route"); if ($routeConfig) { tr_route::serve($routeConfig); } }
function tpl() { if (self::$twig) { return self::$twig; } Twig_Autoloader::register(); $loader = new Twig_Loader_Filesystem(ROOT_PATH . '/view'); $twig = new Twig_Environment($loader, array('cache' => ROOT_PATH . '/cache/templates_c', 'auto_reload' => true, 'autoescape' => "html")); self::$twig = $twig; tr_hook::fire("twig_add_ext", self::$twig); return self::$twig; }
public static function serve($routes, $routesTmp = array()) { tr_hook::fire('before_request', compact('routes')); $request_method = strtolower($_SERVER['REQUEST_METHOD']); $path_info = '/'; if (!empty($_SERVER['PATH_INFO'])) { $path_info = $_SERVER['PATH_INFO']; } elseif (!empty($_SERVER['ORIG_PATH_INFO']) && $_SERVER['ORIG_PATH_INFO'] !== '/index.php') { $path_info = $_SERVER['ORIG_PATH_INFO']; } else { if (!empty($_SERVER['REQUEST_URI'])) { $path_info = strpos($_SERVER['REQUEST_URI'], '?') > 0 ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI']; } } $discovered_handler = null; $regex_matches = array(); list($discovered_handler, $regex_matches) = self::match($routes, $path_info); $result = null; $handler_instance = null; //如果没有匹配 if (!$discovered_handler) { list($app, $apiVersion, $from, $secondFrom) = tr::getVersion(); $path_infoNew = str_replace("/" . $app . "/" . $from . $secondFrom . $apiVersion, "", $path_info); list($discovered_handler, $regex_matches) = self::match($routesTmp, $path_infoNew); } if ($discovered_handler) { if (stristr($discovered_handler, "@")) { list($className, $method) = explode("@", $discovered_handler); $handler_instance = new $className(); $request_method = $method; } else { if (is_string($discovered_handler)) { $handler_instance = new $discovered_handler(); } elseif (is_callable($discovered_handler)) { $handler_instance = $discovered_handler(); } } } if ($handler_instance) { unset($regex_matches[0]); if (method_exists($handler_instance, $request_method)) { tr_hook::fire('before_handler', compact('routes', 'discovered_handler', 'request_method', 'regex_matches')); $result = call_user_func_array(array($handler_instance, $request_method), $regex_matches); tr_hook::fire('after_handler', compact('routes', 'discovered_handler', 'request_method', 'regex_matches', 'result')); } else { tr_hook::fire('404', compact('routes', 'discovered_handler', 'request_method', 'regex_matches')); } } else { tr_hook::fire('404', compact('routes', 'discovered_handler', 'request_method', 'regex_matches')); } tr_hook::fire('after_request', compact('routes', 'discovered_handler', 'request_method', 'regex_matches', 'result')); }
static function getVersion() { list($app, $apiVersion) = self::parsePath(); preg_match("/([a-zA-Z]+)/i", $apiVersion, $match); if (!isset($match[1])) { tr_hook::fire('404'); return; } $from = substr($match[1], 0, 1); $secondFrom = strlen($match[1]) > 1 ? substr($match[1], 1) : ""; $apiVersion = str_replace($match[1], "", $apiVersion); $apiVersion = !strstr($apiVersion, ".") ? $apiVersion . ".0" : $apiVersion; if ($apiVersion < 0) { tr_hook::fire('404'); return; } return array($app, $apiVersion, $from, $secondFrom); }
function route() { tr_hook::add("404", function () { echo "Page Not found"; }); $routeConfigTmp = tr_config::config()->get("route"); list($app, $version, $from, $secondFrom) = tr::getVersion(); $routeConfig = $routeConfigTmp[$app]; if (!$routeConfig) { tr_hook::fire("404"); } if ($routeConfig) { $newRouteConfig = array(); $newRouteConfigTmp = array(); arsort($routeConfig); foreach ($routeConfig as $k => $v) { if ($v) { if ($version < $k) { continue; } foreach ($v as $kEnd => $vEnd) { $routetmp = $app . "/" . $from . $secondFrom . $k . "/" . $kEnd; $routetmp = str_replace("//", '/', $routetmp); $newRouteConfig[$routetmp] = $vEnd; $newRouteConfigTmp[$kEnd] = $vEnd; } } } // print_r($newRouteConfig); // print_r($newRouteConfigTmp); tr_route::serve($newRouteConfig, $newRouteConfigTmp); } else { tr_hook::fire("404"); } }
public static function serve($routes) { if (!isset($_SERVER['REQUEST_METHOD'])) { return; } tr_hook::fire('before_request', compact('routes')); $onlyReset = tr::config()->get("app.only_reset"); $request_method = strtolower($_SERVER['REQUEST_METHOD']); $path_info = '/'; if (!empty($_SERVER['PATH_INFO'])) { $path_info = $_SERVER['PATH_INFO']; } elseif (!empty($_SERVER['ORIG_PATH_INFO']) && $_SERVER['ORIG_PATH_INFO'] !== '/index.php') { $path_info = $_SERVER['ORIG_PATH_INFO']; } else { if (!empty($_SERVER['REQUEST_URI'])) { $path_info = strpos($_SERVER['REQUEST_URI'], '?') > 0 ? strstr($_SERVER['REQUEST_URI'], '?', true) : $_SERVER['REQUEST_URI']; } } $discovered_handler = null; $regex_matches = array(); if (isset($routes[$path_info])) { $discovered_handler = $routes[$path_info]; } elseif ($routes) { $tokens = array(':string' => '([a-zA-Z]+)', ':number' => '([0-9]+)', ':alpha' => '([a-zA-Z0-9-_]+)'); foreach ($routes as $pattern => $handler_name) { $pattern = strtr($pattern, $tokens); if (preg_match('#^/?' . $pattern . '/?$#', $path_info, $matches)) { $discovered_handler = $handler_name; $regex_matches = $matches; break; } } } $result = null; $handler_instance = null; if ($discovered_handler) { if (stristr($discovered_handler, "@")) { list($className, $method) = explode("@", $discovered_handler); $handler_instance = new $className(); $request_method = $method; } else { if (is_string($discovered_handler)) { $handler_instance = new $discovered_handler(); } elseif (is_callable($discovered_handler)) { $handler_instance = $discovered_handler(); } } } if ($handler_instance) { unset($regex_matches[0]); if (self::is_xhr_request() && method_exists($handler_instance, $request_method . '_xhr')) { header('Content-type: application/json'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); $request_method .= '_xhr'; } if (method_exists($handler_instance, $request_method)) { tr_hook::fire('before_handler', compact('routes', 'discovered_handler', 'request_method', 'regex_matches')); $result = call_user_func_array(array($handler_instance, $request_method), $regex_matches); tr_hook::fire('after_handler', compact('routes', 'discovered_handler', 'request_method', 'regex_matches', 'result')); } else { tr_hook::fire('404', compact('routes', 'discovered_handler', 'request_method', 'regex_matches')); } } else { tr_hook::fire('404', compact('routes', 'discovered_handler', 'request_method', 'regex_matches')); } tr_hook::fire('after_request', compact('routes', 'discovered_handler', 'request_method', 'regex_matches', 'result')); }
<?php define("ROOT_PATH", __DIR__); define('DS', DIRECTORY_SEPARATOR); define('PS', PATH_SEPARATOR); define("ENVIRONMENT", isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : "developer"); require_once ROOT_PATH . '/vendor/autoload.php'; require_once ROOT_PATH . "/lib/tr/init.php"; tr_init::getInstance()->create(); require_once ROOT_PATH . "/task.php"; tr_hook::fire("task");
<?php tr_hook::add("twig_add_ext", function ($twig) { $twig->addExtension(new twig_static()); });
<?php //css,js压缩 tr_hook::add("task", function () { tr_minify::minify(); });