Przeglądaj źródła

refactored and fix previous commit

Fabien Potencier 14 lat temu
rodzic
commit
1af21221ae

+ 8 - 6
src/Symfony/Bundle/FrameworkBundle/Controller/ParamConverterListener.php

@@ -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;
@@ -11,7 +11,7 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
 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>
@@ -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);
         }
@@ -50,8 +50,10 @@ class ParamConverterListener
     /**
      * @param  Event $event
      * @param  mixed $controller
-     * @throws NotFoundHttpException
+     *
      * @return mixed
+     *
+     * @throws NotFoundHttpException
      */
     public function filterController(Event $event, $controller)
     {
@@ -67,8 +69,8 @@ class ParamConverterListener
                 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);
                     }
                 }
             }

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

@@ -112,7 +112,7 @@ class FrameworkExtension extends Extension
             $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);
         }
 

+ 4 - 3
src/Symfony/Bundle/FrameworkBundle/ParamConverter/Converter/ConverterInterface.php

@@ -1,13 +1,13 @@
 <?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
@@ -15,9 +15,10 @@ interface ConverterInterface
     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);

+ 5 - 5
src/Symfony/Bundle/FrameworkBundle/ParamConverter/ConverterManager.php

@@ -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>
@@ -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)
@@ -40,7 +40,7 @@ class ConverterManager
                 $converted = true;
             }
         }
-        
+
         if (true !== $converted) {
             throw new \InvalidArgumentException(sprintf('Could not convert attribute "%s" into an instance of "%s"', $reflectionParam->getName(), $reflectionClass->getName()));
         }

+ 5 - 5
src/Symfony/Bundle/FrameworkBundle/Resources/config/param_converter.xml

@@ -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>

+ 4 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

@@ -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" />
@@ -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>

+ 5 - 5
src/Symfony/Bundle/FrameworkBundle/Tests/ParamConverter/ConverterManagerTest.php

@@ -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
 {
@@ -18,7 +18,7 @@ class ConverterManagerTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($manager->all(), array($importantConverter));
 
         $manager->add($lessImportantConverter);
-        
+
         $this->assertEquals($manager->all(), array(
             $importantConverter,
             $lessImportantConverter,
@@ -67,11 +67,11 @@ class ConverterManagerTest extends \PHPUnit_Framework_TestCase
 
     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 - 1
src/Symfony/Bundle/FrameworkBundle/Tests/ParamConverter/Fixtures/ConvertableObject.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace Symfony\Bundle\FrameworkBundle\Tests\ParamConverter\Fixtures;
+namespace Symfony\Bundle\FrameworkBundle\Tests\Request\ParamConverter\Fixtures;
 
 class ConvertableObject
 {