Skip to content

Commit

Permalink
Merge branch '1.8' into 2.0
Browse files Browse the repository at this point in the history
* 1.8:
  merge branch 1.7 into 1.8
  fix loading route directory resources
  • Loading branch information
xabbuh committed Apr 6, 2016
2 parents 7a0c420 + 932c7f9 commit 63a670d
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 9 deletions.
11 changes: 6 additions & 5 deletions Resources/config/routing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

<services>

<service id="fos_rest.routing.loader.directory" class="FOS\RestBundle\Routing\Loader\DirectoryRouteLoader" public="false">
<argument type="service" id="file_locator" />
<argument type="service" id="fos_rest.routing.loader.processor" />
<tag name="routing.loader" />
</service>

<service id="fos_rest.routing.loader.controller" class="FOS\RestBundle\Routing\Loader\RestRouteLoader" public="false">
<argument type="service" id="service_container" />
<argument type="service" id="file_locator" />
Expand All @@ -15,11 +21,6 @@
<tag name="routing.loader" />
</service>

<service id="fos_rest.routing.loader.directory" class="FOS\RestBundle\Routing\Loader\DirectoryRouteLoader">
<argument type="service" id="fos_rest.routing.loader.processor" />
<tag name="routing.loader" />
</service>

<service id="fos_rest.routing.loader.processor" class="FOS\RestBundle\Routing\Loader\RestRouteProcessor" public="false" />

<service id="fos_rest.routing.loader.yaml_collection" class="FOS\RestBundle\Routing\Loader\RestYamlCollectionLoader" public="false">
Expand Down
4 changes: 4 additions & 0 deletions Response/AllowedMethodsLoader/AllowedMethodsRouterLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public function warmUp($cacheDir)
}

$processedRoutes[$route->getPath()]['names'][] = $name;
$processedRoutes[$route->getPath()]['methods'] = array_merge(
$processedRoutes[$route->getPath()]['methods'],
$route->getMethods()
);
}

$allowedMethods = [];
Expand Down
19 changes: 17 additions & 2 deletions Routing/Loader/DirectoryRouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace FOS\RestBundle\Routing\Loader;

use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Routing\RouteCollection;
Expand All @@ -22,10 +23,12 @@
*/
class DirectoryRouteLoader extends Loader
{
private $fileLocator;
private $processor;

public function __construct(RestRouteProcessor $processor)
public function __construct(FileLocatorInterface $fileLocator, RestRouteProcessor $processor)
{
$this->fileLocator = $fileLocator;
$this->processor = $processor;
}

Expand All @@ -34,6 +37,10 @@ public function __construct(RestRouteProcessor $processor)
*/
public function load($resource, $type = null)
{
if (isset($resource[0]) && '@' === $resource[0]) {
$resource = $this->fileLocator->locate($resource);
}

if (!is_dir($resource)) {
throw new \InvalidArgumentException(sprintf('Given resource of type "%s" is no directory.', $resource));
}
Expand All @@ -55,6 +62,14 @@ public function load($resource, $type = null)
*/
public function supports($resource, $type = null)
{
return 'rest' === $type && is_string($resource) && is_dir($resource);
if ('rest' !== $type || !is_string($resource)) {
return false;
}

if (isset($resource[0]) && '@' === $resource[0]) {
$resource = $this->fileLocator->locate($resource);
}

return is_dir($resource);
}
}
25 changes: 25 additions & 0 deletions Tests/Functional/AllowedMethodsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Tests\Functional;

/**
* @author Ener-Getick <egetick@gmail.com>
*/
class AllowedMethodsTest extends WebTestCase
{
public function testAllowHeader()
{
$client = $this->createClient(array('test_case' => 'AllowedMethodsListener'));
$client->request('POST', '/allowed-methods');
$this->assertEquals('GET, LOCK, POST, PUT', $client->getResponse()->headers->get('Allow'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller;

use Symfony\Component\HttpFoundation\Response;

class AllowedMethodsController
{
public function indexAction()
{
return new Response();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\Api;

use Symfony\Component\HttpFoundation\JsonResponse;

class CommentController
{
public function getCommentAction($id)
{
return new JsonResponse(array('id' => $id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\Api;

use Symfony\Component\HttpFoundation\JsonResponse;

class PostController
{
public function getPostAction($id)
{
return new JsonResponse(array('id' => $id));
}
}
10 changes: 10 additions & 0 deletions Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ test_view_response_listener:
test_redirect_endpoint:
path: /hello/{name}
defaults: { _controller: TestBundle:Article:redirect }

test_allowed_methods1:
path: /allowed-methods
methods: ['GET', 'LOCK']
defaults: { _controller: TestBundle:AllowedMethods:index }

test_allowed_methods2:
path: /allowed-methods
methods: ['POST', 'PUT']
defaults: { _controller: TestBundle:AllowedMethods:index }
38 changes: 38 additions & 0 deletions Tests/Functional/RoutingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\RestBundle\Tests\Functional;

class RoutingTest extends WebTestCase
{
private $client;

public function setUp()
{
$this->client = $this->createClient(array('test_case' => 'Routing'));
}

public function testPostControllerRoutesAreRegistered()
{
$this->client->request('GET', '/posts/1');

$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$this->assertJsonStringEqualsJsonString('{ "id": 1 }', $this->client->getResponse()->getContent());
}

public function testCommentControllerRoutesAreRegistered()
{
$this->client->request('GET', '/comments/3');

$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$this->assertJsonStringEqualsJsonString('{ "id": 3 }', $this->client->getResponse()->getContent());
}
}
17 changes: 17 additions & 0 deletions Tests/Functional/app/AllowedMethodsListener/bundles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return array(
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \FOS\RestBundle\FOSRestBundle(),
new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(),
new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
);
9 changes: 9 additions & 0 deletions Tests/Functional/app/AllowedMethodsListener/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
imports:
- { resource: ../config/default.yml }

framework:
serializer:
enabled: true

fos_rest:
allowed_methods_listener: true
16 changes: 16 additions & 0 deletions Tests/Functional/app/Routing/bundles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return array(
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \FOS\RestBundle\FOSRestBundle(),
new \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\TestBundle(),
);
9 changes: 9 additions & 0 deletions Tests/Functional/app/Routing/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
imports:
- { resource: ../config/default.yml }

framework:
serializer:
enabled: true
router: { resource: "%kernel.root_dir%/Routing/routing.yml" }

fos_rest: ~
3 changes: 3 additions & 0 deletions Tests/Functional/app/Routing/routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
api_dir:
resource: '@TestBundle/Controller/Api'
type: rest
4 changes: 2 additions & 2 deletions Tests/Routing/Loader/DirectoryRouteLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testLoad()
*/
public function testSupports($resource, $type, $expected)
{
$loader = new DirectoryRouteLoader(new RestRouteProcessor());
$loader = new DirectoryRouteLoader($this->getMock('Symfony\Component\Config\FileLocatorInterface'), new RestRouteProcessor());

if ($expected) {
$this->assertTrue($loader->supports($resource, $type));
Expand All @@ -54,7 +54,7 @@ public function supportsDataProvider()

private function loadFromDirectory($resource)
{
$directoryLoader = new DirectoryRouteLoader(new RestRouteProcessor());
$directoryLoader = new DirectoryRouteLoader($this->getMock('Symfony\Component\Config\FileLocatorInterface'), new RestRouteProcessor());
$controllerLoader = $this->getControllerLoader();

// LoaderResolver sets the resolvers on the loaders passed to it
Expand Down

0 comments on commit 63a670d

Please sign in to comment.