public static function readCTL() { $routes = array(); $parse = new Parser(); foreach (self::allCTL() as $className) { $parse->analyze($className); } $parse->setAllowInherited(true); //$parse->setMethodFilter(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED); $classes = $parse->getClasses(); foreach ($classes as $class) { // is web restful if (!$class->hasAnnotation("Restful")) { continue; } // is has uri annotation $classUriAnns = $class->getAnnotations("uri"); if (empty($classUriAnns)) { $classUri = ""; } else { $classUri = $classUriAnns[0]->getValue(); } $className = $class->getName(); $methods = $class->getMethods(); foreach ($methods as $method) { $HttpMethods = array(); if ($method->hasAnnotation('GET')) { $HttpMethods[] = 'GET'; } if ($method->hasAnnotation('POST')) { $HttpMethods[] = 'POST'; } if ($method->hasAnnotation('PUT')) { $HttpMethods[] = 'PUT'; } if ($method->hasAnnotation('DELETE')) { $HttpMethods[] = 'DELETE'; } $uriParamAnns = $method->getAnnotations("uri"); if (count($uriParamAnns) == 0) { $uri = $classUri; } else { $uri = $classUri . $uriParamAnns[0]->getValue(); } $route = array('controller' => $className, 'action' => $method->getName(), 'methods' => $HttpMethods, 'uri' => $uri); $routes[] = $route; } } return $routes; }
private function allowInheritance($allow = true) { $parser = new Parser(); $test = $this->createTestClass(true); $parser->setAllowInherited($allow); // allow all access modifications - public, protected & private $parser->setMethodFilter(null); $parser->analyze($test); $class = $parser->getClass("DerivedClass"); $this->assertNotNull($class, "Could not find class"); $methods = $class->getMethods(); // if inheritance is not allowed, there should be no methods found if (!$allow) { $this->assertEquals(0, count($methods)); return; } if (count($methods) < 1) { $this->fail("Expected to get at least one method, none found."); } // DerivedClass has no methods of its own, therefore all of its methods // should be declared by its parent class *only* foreach ($methods as $method) { $reflectionObj = $method->getReflectionObject(); $this->assertNotEquals($reflectionObj->class, get_class($test)); } }
/** * 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); }