/** * Analyze the array of provided classes for auto-route annotations * * @param $classes * * @throws \Exception */ private function analyzeClassesForAutoRoutes($classes) { if (empty($classes)) { return; } if (!is_array($classes)) { $classes = array($classes); } $allRoutes = array(); foreach ($classes as $class) { // create the DocBlock parser $dbp = new Parser(); $dbp->setAllowInherited(false); $dbp->setMethodFilter(ReflectionMethod::IS_PUBLIC); // only inspect public methods $dbp->analyze($class); $methods = $dbp->getMethods(); if (empty($methods)) { continue; } $routes = array(); $descriptors = array(); foreach ($methods as $method) { $annotations = $this->buildAnnotationDescriptors($method->getAnnotations()); // if there is no @url annotation, ignore this method if (!$method->hasAnnotation(self::ROUTE)) { continue; } $descriptor = new RouteDescriptor($annotations, $method); $descriptors[] = $descriptor; // @ignore annotations force the AutoRouter to ignore that method if ($method->hasAnnotation("ignore")) { continue; } // if the auto-route callable cannot be accessed, an exception is thrown if (!is_object($class) && !$method->getReflectionObject()->isStatic()) { throw new Exception($method->name . " is not statically accessible. Try passing " . "a class instance of " . $class . " to the AutoRoute plugin " . "instead of the class name."); } // create the auto-route $route = $this->createRoute($method, $class, $descriptor); $routes[] = $route; $allRoutes[] = $route; } foreach ($routes as $route) { if (empty($route)) { continue; } // create Slim routes for the auto-route $slimRoute = $this->getSlimInstance()->map($route->getUri(), $route->getCallback()); foreach ($route->getMethods() as $method) { $slimRoute->via($method); } $name = $route->getName(); if (!empty($name)) { $slimRoute->name($name); } $conditions = $this->getConditionValues($route->getConditions()); if (!empty($conditions) && count($conditions) > 0) { $slimRoute->conditions($conditions); } } } // set the globally accessible list of auto-routes $this->getSlimInstance()->routes = $allRoutes; $this->slimInstance->applyHook("spore.autoroute.ready", $allRoutes); }