/** * Map * Our main workhorse * * @access public * @param closure $callback * @param array $existing_routes * @param boolean $return_ci_style_routes * @return array */ public static function map($callback, $existing_routes = array(), $return_ci_style_routes = false) { $routeBuilder = new RouteBuilder(); call_user_func_array($callback, array($routeBuilder)); if (count($existing_routes) > 0) { foreach ($existing_routes as $to => $from) { $routeBuilder->genericRoute($to, $from); } } self::$routes = $routeBuilder->getRoutes(); self::$genericRoutes = $routeBuilder->getGenericRoutes(); $return_routes = array(); if ($return_ci_style_routes === true) { foreach (self::$routes as $bob_route) { $return_routes[$bob_route['TO']][$bob_route['VERB']] = $bob_route['FROM']; } } else { if (PHP_SAPI === 'cli' or defined('STDIN')) { //lets find all the CLI routes foreach (self::$routes as $bob_route) { if ($bob_route['VERB'] == 'CLI') { $return_routes[$bob_route['TO']] = $bob_route['FROM']; } } } elseif (isset($_SERVER['REQUEST_METHOD'])) { //return only the verb that is being used foreach (self::$routes as $bob_route) { if ($bob_route['VERB'] == $_SERVER['REQUEST_METHOD']) { $return_routes[$bob_route['TO']] = $bob_route['FROM']; } } } else { //map all the routes to the CI methodalogy foreach (self::$routes as $bob_route) { $return_routes[$bob_route['TO']][$bob_route['VERB']] = $bob_route['FROM']; } } } foreach (self::$routes as $route_me) { if (!is_null($route_me['NAME'])) { self::$namedRoutes[$route_me['VERB']][$route_me['NAME']] = $route_me['TO']; } if (count($route_me['GROUPS']) > 0) { foreach ($route_me['GROUPS'] as $group) { $x = $route_me; unset($x['GROUPS']); self::$groups[$group][] = $x; } } } return array_merge($return_routes, self::$genericRoutes); }
private function addRouteForController($controllerClass) { $ref = new ReflectionClass($controllerClass); $annotations = $this->reflectionHelper->getAnnotations($ref->getDocComment()); // these are used by the generated code /** @noinspection PhpUnusedLocalVariableInspection */ $container = $this->container; /** @noinspection PhpUnusedLocalVariableInspection */ $app = $this->app; $middlewareAnnotations = $annotations->getWithName('middleware'); foreach ($annotations->getWithName('route') as $ano) { $builder = new RouteBuilder(); $values = $ano->getValues(); $builder->setMethod(strtolower($values[0])); $builder->setControllerName($controllerClass); $builder->setRoute($values[1]); preg_match_all('/\\/:([a-z0-9]+)/i', $values[1], $matches); foreach ($matches[1] as $match) { $builder->addParam($match); } foreach ($middlewareAnnotations as $midAno) { $midValues = $midAno->getValues(); $builder->addMiddleware($midValues[0]); } eval($builder->render()); } }