Browse Source

Merge remote branch 'alexandresalome/feat-routing-exceptions'

* alexandresalome/feat-routing-exceptions:
  [Routing] Fix the exception inheritance + Add the LICENCE block in new files
  [Routing] Change the Exception namespacing + base class for every exception + update PHPDoc
  [Routing] Add specific exceptions for the UrlGenerator
Fabien Potencier 14 years ago
parent
commit
51eb746925

+ 21 - 0
src/Symfony/Component/Routing/Exception/Exception.php

@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Routing\Exception;
+
+/**
+ * Exception
+ *
+ * @author Alexandre Salomé <alexandre.salome@gmail.com>
+ */
+interface Exception
+{
+}

+ 21 - 0
src/Symfony/Component/Routing/Exception/InvalidParameterException.php

@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Routing\Exception;
+
+/**
+ * Exception thrown when a parameter is not valid
+ *
+ * @author Alexandre Salomé <alexandre.salome@gmail.com>
+ */
+class InvalidParameterException extends \InvalidArgumentException implements Exception
+{
+}

+ 22 - 0
src/Symfony/Component/Routing/Exception/MissingMandatoryParametersException.php

@@ -0,0 +1,22 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Routing\Exception;
+
+/**
+ * Exception thrown when a route cannot be generated because of missing
+ * mandatory parameters.
+ *
+ * @author Alexandre Salomé <alexandre.salome@gmail.com>
+ */
+class MissingMandatoryParametersException extends \InvalidArgumentException implements Exception
+{
+}

+ 21 - 0
src/Symfony/Component/Routing/Exception/NotExistingRouteException.php

@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Routing\Exception;
+
+/**
+ * Exception thrown when a route does not exists
+ *
+ * @author Alexandre Salomé <alexandre.salome@gmail.com>
+ */
+class NotExistingRouteException extends \InvalidArgumentException implements Exception
+{
+}

+ 9 - 5
src/Symfony/Component/Routing/Generator/UrlGenerator.php

@@ -14,6 +14,9 @@ namespace Symfony\Component\Routing\Generator;
 use Symfony\Component\Routing\Route;
 use Symfony\Component\Routing\RouteCollection;
 use Symfony\Component\Routing\RequestContext;
+use Symfony\Component\Routing\Exception\InvalidParameterException;
+use Symfony\Component\Routing\Exception\NotExistingRouteException;
+use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
 
 /**
  * UrlGenerator generates URL based on a set of routes.
@@ -69,12 +72,12 @@ class UrlGenerator implements UrlGeneratorInterface
      *
      * @return string The generated URL
      *
-     * @throws \InvalidArgumentException When route doesn't exist
+     * @throws Symfony\Component\Routing\Exception\NotExistingRouteException When route doesn't exist
      */
     public function generate($name, array $parameters = array(), $absolute = false)
     {
         if (null === $route = $this->routes->get($name)) {
-            throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', $name));
+            throw new NotExistingRouteException(sprintf('Route "%s" does not exist.', $name));
         }
 
         if (!isset($this->cache[$name])) {
@@ -85,7 +88,8 @@ class UrlGenerator implements UrlGeneratorInterface
     }
 
     /**
-     * @throws \InvalidArgumentException When route has some missing mandatory parameters
+     * @throws Symfony\Component\Routing\Exception\MissingMandatoryParametersException When route has some missing mandatory parameters
+     * @throws Symfony\Component\Routing\Exception\InvalidParameterException When a parameter value is not correct
      */
     protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute)
     {
@@ -97,7 +101,7 @@ class UrlGenerator implements UrlGeneratorInterface
 
         // all params must be given
         if ($diff = array_diff_key($variables, $tparams)) {
-            throw new \InvalidArgumentException(sprintf('The "%s" route has some missing mandatory parameters (%s).', $name, implode(', ', $diff)));
+            throw new MissingMandatoryParametersException(sprintf('The "%s" route has some missing mandatory parameters (%s).', $name, implode(', ', $diff)));
         }
 
         $url = '';
@@ -108,7 +112,7 @@ class UrlGenerator implements UrlGeneratorInterface
                     if (!$isEmpty = in_array($tparams[$token[3]], array(null, '', false), true)) {
                         // check requirement
                         if ($tparams[$token[3]] && !preg_match('#^'.$token[2].'$#', $tparams[$token[3]])) {
-                            throw new \InvalidArgumentException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $tparams[$token[3]]));
+                            throw new InvalidParameterException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $tparams[$token[3]]));
                         }
                     }
 

+ 4 - 4
tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php

@@ -131,7 +131,7 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
     }
 
     /**
-     * @expectedException \InvalidArgumentException
+     * @expectedException Symfony\Component\Routing\Exception\NotExistingRouteException
      */
     public function testGenerateWithoutRoutes()
     {
@@ -140,7 +140,7 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
     }
 
     /**
-     * @expectedException \InvalidArgumentException
+     * @expectedException Symfony\Component\Routing\Exception\MissingMandatoryParametersException
      */
     public function testGenerateForRouteWithoutManditoryParameter()
     {
@@ -149,7 +149,7 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
     }
 
     /**
-     * @expectedException \InvalidArgumentException
+     * @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
      */
     public function testGenerateForRouteWithInvalidOptionalParameter()
     {
@@ -158,7 +158,7 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
     }
 
     /**
-     * @expectedException \InvalidArgumentException
+     * @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
      */
     public function testGenerateForRouteWithInvalidManditoryParameter()
     {