Skip to content

Commit

Permalink
add multiple routes
Browse files Browse the repository at this point in the history
  • Loading branch information
mgiustiniani committed Mar 17, 2014
1 parent ed28a05 commit ec5a895
Showing 1 changed file with 68 additions and 29 deletions.
97 changes: 68 additions & 29 deletions Routing/Loader/Reader/RestActionReader.php
Expand Up @@ -188,27 +188,6 @@ public function read(RestRouteCollection $collection, \ReflectionMethod $method,
$host = '';
$schemes = array();

$annotation = $this->readRouteAnnotation($method);
if ($annotation) {
$annoRequirements = $annotation->getRequirements();

if (!isset($annoRequirements['_method'])) {
$annoRequirements['_method'] = $requirements['_method'];
}

$pattern = $annotation->getPattern() !== null ? $this->routePrefix . $annotation->getPattern() : $pattern;
$requirements = array_merge($requirements, $annoRequirements);
$options = array_merge($options, $annotation->getOptions());
$defaults = array_merge($defaults, $annotation->getDefaults());
//TODO remove checks after Symfony requirement is bumped to 2.2
if (method_exists($annotation, 'getHost')) {
$host = $annotation->getHost();
}
if (method_exists($annotation, 'getSchemes')) {
$schemes = $annotation->getSchemes();
}
}

if ($this->includeFormat === true) {
$pattern .= '.{_format}';

Expand All @@ -217,10 +196,38 @@ public function read(RestRouteCollection $collection, \ReflectionMethod $method,
}
}

// add route to collection
$collection->add($routeName, new Route(
$pattern, $defaults, $requirements, $options, $host, $schemes
));
$annotations = $this->readRouteAnnotation($method);
if ($annotations) {
foreach ($annotations as $annotation) {
$annoRequirements = $annotation->getRequirements();

if (!isset($annoRequirements['_method'])) {
$annoRequirements['_method'] = $requirements['_method'];
}

$pattern = $annotation->getPattern() !== null ? $this->routePrefix . $annotation->getPattern() : $pattern;
$requirements = array_merge($requirements, $annoRequirements);
$options = array_merge($options, $annotation->getOptions());
$defaults = array_merge($defaults, $annotation->getDefaults());
//TODO remove checks after Symfony requirement is bumped to 2.2
if (method_exists($annotation, 'getHost')) {
$host = $annotation->getHost();
}
if (method_exists($annotation, 'getSchemes')) {
$schemes = $annotation->getSchemes();
}

// add route to collection
$collection->add($routeName.$annotation->getName(), new Route(
$pattern, $defaults, $requirements, $options, $host, $schemes));
}

}
else {
// add route to collection
$collection->add($routeName, new Route(
$pattern, $defaults, $requirements, $options, $host, $schemes));
}
}

/**
Expand Down Expand Up @@ -423,17 +430,21 @@ private function getCustomHttpMethod($httpMethod, array $resources, array $argum
*
* @param \ReflectionMethod $reflection
*
* @return Annotation|null
* @return array|null
*/
private function readRouteAnnotation(\ReflectionMethod $reflection)
{
foreach (array('Route','Get','Post','Put','Patch','Delete','Head') as $annotationName) {
if ($annotation = $this->readMethodAnnotation($reflection, $annotationName)) {
return $annotation;
$annotations = array();

foreach ($this->availableHTTPMethods as $annotationName) {
if ($annotations_new = $this->readMethodAnnotations($reflection, $annotationName)) {
$annotations = array_merge($annotations, $annotations_new);
}
}
return $annotations;
}


/**
* Reads class annotations.
*
Expand Down Expand Up @@ -467,4 +478,32 @@ private function readMethodAnnotation(\ReflectionMethod $reflection, $annotation
return $annotation;
}
}
/**
* Reads method annotations.
*
* @param ReflectionMethod $reflection controller action
* @param string $annotationName annotation name
*
* @return array|null
*/
private function readMethodAnnotations(\ReflectionMethod $reflection, $annotationName)
{

$annotations = array();
$annotationClass = "FOS\\RestBundle\\Controller\\Annotations\\$annotationName";

if ($annotations_new = $this->annotationReader->getMethodAnnotations($reflection)) {

foreach($annotations_new as $annotation) {
if ($annotation instanceof $annotationClass) {
echo $annotationName.PHP_EOL;
$annotations[]= $annotation;

}
}


}
return $annotations;
}
}

0 comments on commit ec5a895

Please sign in to comment.