read route options in PropelParamConverter
This commit is contained in:
parent
d9d8e9879f
commit
a9fcc24807
|
@ -4,9 +4,11 @@ namespace Propel\PropelBundle\Request\ParamConverter;
|
|||
|
||||
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
|
||||
|
@ -51,6 +53,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 @@ class PropelParamConverter implements ParamConverterInterface
|
|||
|
||||
$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 Propel\PropelBundle\Request\ParamConverter\PropelParamConverter;
|
|||
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 @@ class PropelParamConverterTest extends TestCase
|
|||
$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();
|
||||
|
|
Loading…
Reference in a new issue