Skip to content

Commit

Permalink
Moved Annotation to Route building logic to the annotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Apr 17, 2015
1 parent b0e09be commit 740bb08
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
24 changes: 21 additions & 3 deletions Annotation/Route.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use Titon\Annotation\Annotation;
use Titon\Route\Mixin\FilterMixin;
use Titon\Route\Mixin\MethodMixin;
use Titon\Route\Mixin\PatternMixin;
use Titon\Route\Route as BaseRoute;
use Titon\Route\Router;
use Titon\Utility\Col;

/**
Expand Down Expand Up @@ -43,9 +45,9 @@ class Route extends Annotation {
*
* @param string $key
* @param string $path
* @param mixed $methods
* @param mixed $filters
* @param mixed $patterns
* @param mixed $methods
* @param mixed $filters
* @param mixed $patterns
*/
public function __construct(string $key, string $path, mixed $methods = [], mixed $filters = [], array<string, string> $patterns = []) {
$this->key = $key;
Expand Down Expand Up @@ -73,4 +75,20 @@ class Route extends Annotation {
return $this->path;
}

/**
* Convert the annotation to a `Route` object.
*
* @param string $class
* @param string $action
* @return \Titon\Route\Route
*/
public function toRoute(string $class, string $action = 'index'): BaseRoute {
$route = new BaseRoute($this->getPath(), Router::buildAction(shape('class' => $class, 'action' => $action)));
$route->setMethods($this->getMethods());
$route->setFilters($this->getFilters());
$route->setPatterns($this->getPatterns());

return $route;
}

}
21 changes: 2 additions & 19 deletions Router.hh
Original file line number Diff line number Diff line change
Expand Up @@ -633,37 +633,20 @@ class Router implements Subject {
// Map resource routes if the annotation is on the class
foreach ($reader->getClassAnnotations() as $annotation) {
if ($annotation instanceof RouteAnnotation) {
$this->resource($annotation->getKey(), $this->buildAnnotationRoute($annotation, $class));
$this->resource($annotation->getKey(), $annotation->toRoute($class));
}
}

// Map regular routes if the annotation is on a method
foreach ($reader->getAnnotatedMethods() as $method => $annotations) {
foreach ($annotations as $annotation) {
if ($annotation instanceof RouteAnnotation) {
$this->map($annotation->getKey(), $this->buildAnnotationRoute($annotation, $class, $method));
$this->map($annotation->getKey(), $annotation->toRoute($class, $method));
}
}
}

return $this;
}

/**
* Build a route object from an annotation.
*
* @param \Titon\Route\Annotation\Route $annotation
* @param string $class
* @param string $method
* @return \Titon\Route\Route
*/
protected function buildAnnotationRoute(RouteAnnotation $annotation, string $class, string $method = 'index'): Route {
$route = new Route($annotation->getPath(), static::buildAction(shape('class' => $class, 'action' => $method)));
$route->setMethods($annotation->getMethods());
$route->setFilters($annotation->getFilters());
$route->setPatterns($annotation->getPatterns());

return $route;
}

}

0 comments on commit 740bb08

Please sign in to comment.