Skip to content

Commit

Permalink
refactored and fix previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Dec 22, 2010
1 parent baf07a1 commit 1af2122
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 26 deletions.
Expand Up @@ -2,7 +2,7 @@

namespace Symfony\Bundle\FrameworkBundle\Controller;

use Symfony\Bundle\FrameworkBundle\ParamConverter\ConverterManager;
use Symfony\Bundle\FrameworkBundle\Request\ParamConverter\ConverterManager;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand All @@ -11,7 +11,7 @@
use Symfony\Component\EventDispatcher\Event;

/**
* Converts \ReflectionParameters for Controller actions into Objects if the ReflectionParameter have a class
* Converts \ReflectionParameters for Controller actions into Objects if the \ReflectionParameter have a class
* (Typehinted).
*
* @author Fabien Potencier <fabien.potencier@symfony-project.org>
Expand All @@ -30,7 +30,7 @@ class ParamConverterListener
*/
public function __construct(ConverterManager $manager, ContainerInterface $container)
{
foreach ($container->findTaggedServiceIds('param_converter.converter') as $id => $attributes) {
foreach ($container->findTaggedServiceIds('request.param_converter') as $id => $attributes) {
$priority = isset($attributes['priority']) ? (integer) $attributes['priority'] : 0;
$manager->add($container->get($id), $priority);
}
Expand All @@ -50,8 +50,10 @@ public function register(EventDispatcher $dispatcher, $priority = 0)
/**
* @param Event $event
* @param mixed $controller
* @throws NotFoundHttpException
*
* @return mixed
*
* @throws NotFoundHttpException
*/
public function filterController(Event $event, $controller)
{
Expand All @@ -67,8 +69,8 @@ public function filterController(Event $event, $controller)
try {
$this->manager->apply($request, $param);
} catch (\InvalidArgumentException $e) {
if (false == $param->isOptional()) {
throw new NotFoundHttpException($e->getMessage());
if (false === $param->isOptional()) {
throw new NotFoundHttpException(sprintf('Unable to convert parameter "%s".', $param->getName()), 0, $e);
}
}
}
Expand Down
Expand Up @@ -112,7 +112,7 @@ public function configLoad($config, ContainerBuilder $container)
$this->registerTestConfiguration($config, $container);
}

if (array_key_exists('param_converter', $config)) {
if (array_key_exists('param_converter', $config) || array_key_exists('param-converter', $config)) {
$this->registerParamConverterConfiguration($config, $container);
}

Expand Down
@@ -1,23 +1,24 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\ParamConverter\Converter;
namespace Symfony\Bundle\FrameworkBundle\Request\ParamConverter;

use Symfony\Component\HttpFoundation\Request;

interface ConverterInterface
{
/**
* Convert the \ReflectionPropertt to something else.
* Convert the \ReflectionParameter to something else.
*
* @param Request $request
* @param \ReflectionParameter $property
*/
function apply(Request $request, \ReflectionParameter $parameter);

/**
* Returns boolean true if the ReflectionProperty is supported. Else false
* Returns boolean true if the ReflectionClass is supported, false otherwise
*
* @param \ReflectionParameter $parameter
*
* @return boolean
*/
function supports(\ReflectionClass $class);
Expand Down
@@ -1,13 +1,12 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\ParamConverter;
namespace Symfony\Bundle\FrameworkBundle\Request\ParamConverter;

use Symfony\Bundle\FrameworkBundle\ParamConverter\Converter\ConverterInterface;
use Symfony\Bundle\FrameworkBundle\Request\ParamConverter\ConverterInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* ConverterManager
* Keeps track of converters
* Keeps track of param converters.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Henrik Bjornskov <hb@peytz.dk>
Expand All @@ -26,6 +25,7 @@ class ConverterManager
*
* @param Request $request
* @param array $reflectionParam An array of ReflectionParameter objects
*
* @throws InvalidArgumentException
*/
public function apply(Request $request, \ReflectionParameter $reflectionParam)
Expand All @@ -40,7 +40,7 @@ public function apply(Request $request, \ReflectionParameter $reflectionParam)
$converted = true;
}
}

if (true !== $converted) {
throw new \InvalidArgumentException(sprintf('Could not convert attribute "%s" into an instance of "%s"', $reflectionParam->getName(), $reflectionClass->getName()));
}
Expand Down
Expand Up @@ -5,18 +5,18 @@
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="param_converter.converter_manager.class">Symfony\Bundle\FrameworkBundle\ParamConverter\ConverterManager</parameter>
<parameter key="param_converter.controller.param_converter_listener.class">Symfony\Bundle\FrameworkBundle\Controller\ParamConverterListener</parameter>
<parameter key="request.param_converter.manager.class">Symfony\Bundle\FrameworkBundle\Request\ParamConverter\ConverterManager</parameter>
<parameter key="request.param_converter.listener.class">Symfony\Bundle\FrameworkBundle\Controller\ParamConverterListener</parameter>
</parameters>

<services>
<!-- ConverterManager -->
<service id="param_converter.converter_manager" class="%param_converter.converter_manager.class%" />
<service id="request.param_converter.manager" class="%request.param_converter.manager.class%" />

<!-- ParamConverterListener -->
<service id="param_converter.controller.param_converter_listener" class="%param_converter.controller.param_converter_listener.class%">
<service id="request.param_converter.listener" class="%request.param_converter.listener.class%">
<tag name="kernel.listener" />
<argument type="service" id="param_converter.converter_manager" />
<argument type="service" id="request.param_converter.manager" />
<argument type="service" id="service_container" />
</service>
</services>
Expand Down
Expand Up @@ -15,6 +15,7 @@
<xsd:element name="session" type="session" minOccurs="0" maxOccurs="1" />
<xsd:element name="templating" type="templating" minOccurs="0" maxOccurs="1" />
<xsd:element name="translator" type="translator" minOccurs="0" maxOccurs="1" />
<xsd:element name="param-converter" type="param-converter" minOccurs="0" maxOccurs="1" />
</xsd:sequence>

<xsd:attribute name="ide" type="xsd:string" />
Expand Down Expand Up @@ -74,4 +75,7 @@
<xsd:complexType name="translator">
<xsd:attribute name="fallback" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="param-converter">
</xsd:complexType>
</xsd:schema>
@@ -1,9 +1,9 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\ParamConverter;
namespace Symfony\Bundle\FrameworkBundle\Tests\Request\ParamConverter;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\ParamConverter\ConverterManager;
use Symfony\Bundle\FrameworkBundle\Request\ParamConverter\ConverterManager;

class ConverterManagerTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -18,7 +18,7 @@ public function testManagerCanContainerConverters()
$this->assertEquals($manager->all(), array($importantConverter));

$manager->add($lessImportantConverter);

$this->assertEquals($manager->all(), array(
$importantConverter,
$lessImportantConverter,
Expand Down Expand Up @@ -67,11 +67,11 @@ public function testManagerWillApplyConvertersSuccessfully()

private function getReflectionParameter()
{
return new \ReflectionParameter(array('Symfony\Bundle\FrameworkBundle\Tests\ParamConverter\Fixtures\ConvertableObject', 'typehintedMethod'), 'object');
return new \ReflectionParameter(array('Symfony\Bundle\FrameworkBundle\Tests\Request\ParamConverter\Fixtures\ConvertableObject', 'typehintedMethod'), 'object');
}

private function getConverterInterfaceMock()
{
return $this->getMock('Symfony\Bundle\FrameworkBundle\ParamConverter\Converter\ConverterInterface');
return $this->getMock('Symfony\Bundle\FrameworkBundle\Request\ParamConverter\ConverterInterface');
}
}
@@ -1,6 +1,6 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\ParamConverter\Fixtures;
namespace Symfony\Bundle\FrameworkBundle\Tests\Request\ParamConverter\Fixtures;

class ConvertableObject
{
Expand Down

0 comments on commit 1af2122

Please sign in to comment.