Permalink
Browse files

Merge pull request #245 from havvg/feature/param-converter-route-options

read route options in PropelParamConverter
  • Loading branch information...
2 parents d9d8e98 + a9fcc24 commit 2c0342f927d1675a586710847aac551476752fc6 @havvg havvg committed Sep 18, 2013
@@ -4,9 +4,11 @@
use Propel\PropelBundle\Util\PropelInflector;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Routing\RouterInterface;
/**
* PropelParamConverter
@@ -52,6 +54,16 @@ class PropelParamConverter implements ParamConverterInterface
protected $hasWith = false;
/**
+ * @var RouterInterface
+ */
+ protected $router;
+
+ public function setRouter(RouterInterface $router = null)
+ {
+ $this->router = $router;
+ }
+
+ /**
* @param Request $request
* @param ConfigurationInterface $configuration
*
@@ -81,6 +93,14 @@ public function apply(Request $request, ConfigurationInterface $configuration)
$options = $configuration->getOptions();
+ // Check route options for converter options, if there are non provided.
+ if (empty($options) && $this->router && $configuration instanceof ParamConverter) {
+ $converterOption = $this->router->getRouteCollection()->get($request->attributes->get('_route'))->getOption('propel_converter');
+ if (!empty($converterOption[$configuration->getName()])) {
+ $options = $converterOption[$configuration->getName()];
+ }
+ }
+
if (isset($options['mapping'])) {
// We use the mapping for calling findPk or filterBy
foreach ($options['mapping'] as $routeParam => $column) {
@@ -11,6 +11,10 @@
<services>
<service id="propel.converter.propel.orm" class="%propel.converter.propel.class%">
<tag name="request.param_converter" priority="10" />
+
+ <call method="setRouter">
+ <argument type="service" id="router" on-invalid="null" />
+ </call>
</service>
</services>
</container>
@@ -7,6 +7,8 @@
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Propel\PropelBundle\Tests\TestCase;
+use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\RouteCollection;
class PropelParamConverterTest extends TestCase
{
@@ -280,6 +282,49 @@ public function testParamConvertWithOptionWithFindPk()
$this->assertEquals($nb + 1, $this->con->getQueryCount(), 'no new query to get the books');
}
+ public function testConfigurationReadFromRouteOptionsIfEmpty()
+ {
+ $routes = new RouteCollection();
+ $routes->add('test_route', new Route('/test/{authorId}', array(), array(), array(
+ 'propel_converter' => array(
+ 'author' => array(
+ 'mapping' => array(
+ 'authorId' => 'id',
+ ),
+ ),
+ ),
+ )));
+
+ $router = $this->getMock('Symfony\Bundle\FrameworkBundle\Routing\Router', array(), array(), '', false);
+ $router
+ ->expects($this->once())
+ ->method('getRouteCollection')
+ ->will($this->returnValue($routes))
+ ;
+
+ $paramConverter = new PropelParamConverter();
+ $paramConverter->setRouter($router);
+
+ $request = new Request();
+ $request->attributes->add(array(
+ '_route' => 'test_route',
+ 'id' => 10,
+ 'author' => null,
+ ));
+
+ $configuration = new ParamConverter(array(
+ 'class' => 'Propel\PropelBundle\Tests\Request\ParamConverter\MyAuthor',
+ 'name' => 'author',
+ 'options' => array(),
+ ));
+
+ $paramConverter->apply($request, $configuration);
+
+ $author = $request->attributes->get('author');
+ $this->assertInstanceOf('Propel\PropelBundle\Tests\Request\ParamConverter\MyAuthor', $author,
+ 'param "author" should be an instance of "Propel\PropelBundle\Tests\Request\ParamConverter\MyAuthor"');
+ }
+
protected function loadFixtures()
{
$this->loadPropelQuickBuilder();

0 comments on commit 2c0342f

Please sign in to comment.