Skip to content

Commit

Permalink
Add AnnotationRouteCompiler::getRoutes()
Browse files Browse the repository at this point in the history
This allows to get a list of routes to display to the end user.
  • Loading branch information
BenMorel committed Mar 25, 2019
1 parent 8ba27b6 commit c911832
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/AnnotationRouteCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class AnnotationRouteCompiler
*/
private $defaultMethods;

/**
* An array of [path, HTTP methods, controller class, controller method] arrays.
*
* @var array
*/
private $routes = [];

/**
* AnnotationRouteCompiler constructor.
*
Expand Down Expand Up @@ -60,11 +67,13 @@ public function compile(array $classNames) : array
foreach ($classNames as $className) {
$reflectionClass = new \ReflectionClass($className);

$prefixPath = '';
$prefixRegexp = '';
$classParameterNames = [];

foreach ($this->annotationReader->getClassAnnotations($reflectionClass) as $annotation) {
if ($annotation instanceof Route) {
$prefixPath = $annotation->path;
[$prefixRegexp, $classParameterNames] = $this->processAnnotation($annotation);

break;
Expand All @@ -85,6 +94,13 @@ public function compile(array $classNames) : array
$httpMethods = $this->defaultMethods;
}

$this->routes[] = [
$prefixPath . $annotation->path,
$annotation->methods,
$className,
$methodName
];

$result[] = [
$pathRegexp,
$httpMethods,
Expand All @@ -98,9 +114,24 @@ public function compile(array $classNames) : array
}
}

// Sort routes by path & methods
sort($this->routes);

return $result;
}

/**
* Returns an array of [path, HTTP methods, controller class, controller method] arrays.
*
* This array is only available after compile() has been executed.
*
* @return array
*/
public function getRoutes() : array
{
return $this->routes;
}

/**
* @todo 0.4.0 make private
*
Expand Down

0 comments on commit c911832

Please sign in to comment.