Skip to content

Commit 68ba59b

Browse files
committed
Merge pull request #761 from danrot/media_controller_issue
added failing test case for uninflected resource
2 parents a1763eb + 039ab1d commit 68ba59b

File tree

4 files changed

+117
-9
lines changed

4 files changed

+117
-9
lines changed

Routing/Loader/Reader/RestActionReader.php

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*/
2727
class RestActionReader
2828
{
29+
const COLLECTION_ROUTE_PREFIX = 'c';
30+
2931
private $annotationReader;
3032
private $paramReader;
3133
private $inflector;
@@ -150,8 +152,8 @@ public function read(RestRouteCollection $collection, \ReflectionMethod $method,
150152
return;
151153
}
152154

153-
list($httpMethod, $resources) = $httpMethodAndResources;
154-
$arguments = $this->getMethodArguments($method);
155+
list($httpMethod, $resources, $isCollection, $isInflectable) = $httpMethodAndResources;
156+
$arguments = $this->getMethodArguments($method);
155157

156158
// if we have only 1 resource & 1 argument passed, then it's object call, so
157159
// we can set collection singular name
@@ -234,8 +236,10 @@ public function read(RestRouteCollection $collection, \ReflectionMethod $method,
234236
}
235237
}
236238
// add route to collection
237-
$collection->add($routeName.$annotation->getName(), new Route(
238-
$pattern, $defaults, $requirements, $options, $host, $schemes, null, $condition));
239+
$route = new Route(
240+
$pattern, $defaults, $requirements, $options, $host, $schemes, null, $condition
241+
);
242+
$this->addRoute($collection, $routeName, $route, $isCollection, $isInflectable, $annotation);
239243
}
240244

241245
} else {
@@ -247,8 +251,10 @@ public function read(RestRouteCollection $collection, \ReflectionMethod $method,
247251
}
248252
}
249253
// add route to collection
250-
$collection->add($routeName, new Route(
251-
$pattern, $defaults, $requirements, $options, $host, $schemes, null, $condition));
254+
$route = new Route(
255+
$pattern, $defaults, $requirements, $options, $host, $schemes, null, $condition
256+
);
257+
$this->addRoute($collection, $routeName, $route, $isCollection, $isInflectable);
252258
}
253259
}
254260

@@ -300,19 +306,24 @@ private function getHttpMethodAndResourcesFromMethod(\ReflectionMethod $method,
300306
$resources = preg_split(
301307
'/([A-Z][^A-Z]*)/', $matches[2], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
302308
);
309+
$isCollection = false;
310+
$isInflectable = true;
303311

304-
if (0 === strpos($httpMethod, 'c')
312+
if (0 === strpos($httpMethod, self::COLLECTION_ROUTE_PREFIX)
305313
&& in_array(substr($httpMethod, 1), $this->availableHTTPMethods)
306314
) {
315+
$isCollection = true;
307316
$httpMethod = substr($httpMethod, 1);
308317
if (!empty($resource)) {
309-
$resource[count($resource)-1] = $this->inflector->pluralize(end($resource));
318+
$resourcePluralized = $this->inflector->pluralize(end($resource));
319+
$isInflectable = ($resourcePluralized != $resource[count($resource) - 1]);
320+
$resource[count($resource)-1] = $resourcePluralized;
310321
}
311322
}
312323

313324
$resources = array_merge($resource, $resources);
314325

315-
return array($httpMethod, $resources);
326+
return array($httpMethod, $resources, $isCollection, $isInflectable);
316327
}
317328

318329
/**
@@ -527,4 +538,27 @@ private function readMethodAnnotations(\ReflectionMethod $reflection, $annotatio
527538
}
528539
return $annotations;
529540
}
541+
542+
/**
543+
* @param RestRouteCollection $collection
544+
* @param $routeName
545+
* @param $route
546+
* @param $isCollection
547+
* @param null $annotation
548+
*/
549+
private function addRoute(RestRouteCollection $collection, $routeName, $route, $isCollection, $isInflectable, $annotation = null)
550+
{
551+
if ($annotation) {
552+
$routeName = $routeName.$annotation->getName();
553+
}
554+
555+
if ($isCollection && !$isInflectable) {
556+
$collection->add(self::COLLECTION_ROUTE_PREFIX.$routeName, $route);
557+
if (!$collection->get($routeName)) {
558+
$collection->add($routeName, $route);
559+
}
560+
} else {
561+
$collection->add($routeName, $route);
562+
}
563+
}
530564
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Tests\Fixtures\Controller;
13+
14+
use FOS\RestBundle\Controller\FOSRestController;
15+
use FOS\RestBundle\Routing\ClassResourceInterface;
16+
17+
class InformationController extends FOSRestController implements ClassResourceInterface
18+
{
19+
public function cgetAction()
20+
{} // [GET] /information
21+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Tests\Fixtures\Controller;
13+
14+
use FOS\RestBundle\Controller\FOSRestController;
15+
use FOS\RestBundle\Routing\ClassResourceInterface;
16+
17+
class MediaController extends FOSRestController implements ClassResourceInterface
18+
{
19+
public function cgetAction()
20+
{} // [GET] /media
21+
22+
public function getAction($slug)
23+
{} // [GET] /media/{slug}
24+
}

Tests/Routing/Loader/RestRouteLoaderTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,35 @@ public function testCustomActionRoutesInDeveloperOrder()
233233
$this->assertLessThan($pos['prefix_get_bars'], $pos['prefix_get_bars_custom']);
234234
}
235235

236+
/**
237+
* Test if the routes are also working with uninflected words
238+
*
239+
* @see https://github.com/FriendsOfSymfony/FOSRestBundle/pull/761
240+
*/
241+
public function testMediaFixture()
242+
{
243+
$expectedMethod = 'GET';
244+
$collection = $this->loadFromControllerFixture('MediaController');
245+
246+
$this->assertCount(2, $collection->all());
247+
$this->assertEquals($expectedMethod, $collection->get('get_media')->getRequirement('_method'));
248+
$this->assertEquals($expectedMethod, $collection->get('cget_media')->getRequirement('_method'));
249+
250+
}
251+
252+
/**
253+
* Test if the routes are also working with uninflected words
254+
*
255+
* @see https://github.com/FriendsOfSymfony/FOSRestBundle/pull/761
256+
*/
257+
public function testInformationFixture()
258+
{
259+
$collection = $this->loadFromControllerFixture('InformationController');
260+
261+
$this->assertCount(2, $collection->all());
262+
$this->assertSame($collection->get('get_information'), $collection->get('cget_information'));
263+
}
264+
236265
/**
237266
* Load routes collection from fixture class under Tests\Fixtures directory.
238267
*

0 commit comments

Comments
 (0)