3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php namespace CW\Router; use CW\Errors\NiunException; class Route { public static $request; private static $param; private static $paramArr = array(); public static function run($route_rule = []){ self::$request = $route_rule; $routes = self::matchURI(); self::_prepareParams($routes); self::_prepareRoute($routes['controller'], $routes['action']); } private static function matchURI($uri = null) { $uri = (!$uri) ? parse_url($_SERVER['REQUEST_URI']) : $uri; $uri = ($uri['path'] == '/') ? '/' : rtrim($uri['path'],'\/'); if(!empty(self::$request)) { $count=count(self::$request); for($i=0; $i<$count; ++$i) { foreach(self::$request[$i] as $k => $v) { if (is_array($v) and $k !== 'param') { self::$param = self::$request[$i]['param']; $v['request'] = preg_replace_callback('/\<(?<key>[0-9a-z_]+)\>/', 'self::_replacer', str_replace(")",")?", $v['request']) ); $rulleTemp = array_merge((array)self::$request[$i], (array)$v); if(($t = self::_reportRulle($rulleTemp, $uri))) return $t; } } } } else return array(); } private static function _replacer($matches) { if(isset(self::$param[$matches['key']])) { return "(?<".$matches['key'].">".self::$param[$matches['key']].")"; } else return "(?<".$matches['key'].">"."([^/]+)".")"; } private static function _reportRulle($ini_array, $uri) { if(is_array($ini_array) and $uri) { if(preg_match("#^".$ini_array['request']."$#", $uri, $match)){ $r = array_merge((array)$ini_array, (array)$match); foreach($r as $k => $v) if((int)$k OR $k == 'param' OR $k == 'request') unset($r[$k]); return $r; } } } /** * Preparing controller to be included. Checking is controller exists. * Creating new specific model instance. Creating controller instance. * * @param $controller string Controller name. * @param $method string Method name. */ static function _prepareRoute($controller, $action) { $controller_path = Application . $controller . '/' . $controller . 'Controller.php'; self::_checkControllerExists($controller_path); self::_createModelInstance($controller); self::_createInstance($controller, $action); } /** * Checks requested URL on params and id and if exists sets to the private vars. * * @param $routes array Requested URL. */ static function _prepareParams($routes) { if ((!empty($routes['lang']) && !empty($routes['pagination']) && !empty($routes['page'])) || !empty($routes['lang']) || !empty($routes['pagination']) || !empty($routes['page'])) { self::$paramArr['lang'] = $routes['lang']; self::$paramArr['pagination'] = $routes['pagination']; self::$paramArr['page'] = $routes['page']; } } /** * Checks is controller exists and inlcude it. * * @param $controller_path string Controller path. Used to include and controller. * @throws Exception */ static function _checkControllerExists($controller_path) { try { if (file_exists($controller_path)) { require_once $controller_path; } else { throw new NiunException; } } catch (NiunException $e) { echo $e->getMessage(); } } /** * Creating new instance that required by URL. * * @param $controller string Controller name. * @param $method string Method name. */ static function _createInstance($controller, $action) { $instance = new $controller; if (method_exists($instance, $action)) { $reflection = new \ReflectionMethod($instance, $action); if (!$reflection->isPublic()) { @header('Location: /'); } $instance->$action(self::$paramArr); } else { @header('Location: /'); } } /** * Creates instance of model by requested controller. * * @param $controller string Controller name. */ static function _createModelInstance($controller) { $model = Application . $controller . '/' . $controller . 'Model.php'; if(file_exists($model)) { require_once ($model); } } }
Output for 5.4.0 - 7.1.0
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/dn5bn on line 13
Process exited with code 255.