Skip to content

Commit

Permalink
[routing] Added '_negotiation' parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfsimon committed Oct 12, 2012
1 parent cf6ff20 commit 94e2df2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
Expand Up @@ -21,9 +21,9 @@ class NotAcceptableHttpException extends HttpException
/**
* Constructor.
*
* @param array $headers An array of headers
* @param string $message The internal exception message
* @param \Exception $previous The previous exception
* @param array $headers An array of headers
* @param integer $code The internal exception code
*/
public function __construct($message = null, \Exception $previous = null, array $headers = array(), $code = 0)
Expand Down
45 changes: 42 additions & 3 deletions src/Symfony/Component/Routing/Exception/NotAcceptableException.php
Expand Up @@ -22,19 +22,58 @@
*/
class NotAcceptableException extends \RuntimeException implements ExceptionInterface
{
/**
* @var array
*/
protected $negotiationVariants;

/**
* @var RequestAcceptance
*/
protected $acceptance;

/**
* @var string
*/
protected $requirement;

public function __construct(RequestAcceptance $acceptance, $requirement, $message = null, $code = 0, \Exception $previous = null)
/**
* @param array $negotiationVariants
* @param RequestAcceptance $acceptance
* @param string $requirement
* @param int $code
* @param \Exception|null $previous
*/
public function __construct(array $negotiationVariants, RequestAcceptance $acceptance, $requirement, $code = 0, \Exception $previous = null)
{
$this->negotiationVariants = $negotiationVariants;
$this->acceptance = $acceptance;
$this->requirement = $requirement;
$message = sprintf('None of the accepted values "%s" match route requirement "%s".', implode(', ', $acceptance->getValues()), $requirement);
parent::__construct($message, $code, $previous);
}

public function getVariants()
/**
* @return array
*/
public function getNegotiationVariants()
{
return $this->negotiationVariants;
}

/**
* @return RequestAcceptance
*/
public function getAcceptance()
{
return $this->acceptance;
}

/**
* @return string
*/
public function getRequirement()
{

return $this->requirement;
}
}
17 changes: 14 additions & 3 deletions src/Symfony/Component/Routing/Matcher/UrlMatcher.php
Expand Up @@ -111,9 +111,15 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
return $ret;
}

// check negotiate route option
$negotiationVariants = array();
if ($route->getOption('negotiate')) {
foreach ($this->negotiatedVariables as $variable) {
$route->setDefault($variable, null);
if (null !== $requirement = $route->getRequirement($variable)) {
// requirement must be set with the form "xx|yy|zz"
$negotiationVariants[$variable] = explode('|', strtoupper($requirement));
$route->setDefault($variable, null);
}
}
}

Expand Down Expand Up @@ -151,7 +157,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
if (null !== $default = $acceptance->getBestValue()) {
$route->setDefault($variable, $default);
} else {
throw new NotAcceptableException($this->context->getAcceptance($variable), $requirement);
throw new NotAcceptableException($negotiationVariants, $this->context->getAcceptance($variable), $requirement);
}
}
}
Expand All @@ -167,7 +173,12 @@ protected function matchCollection($pathinfo, RouteCollection $routes)
continue;
}

return array_merge($this->mergeDefaults($matches, $route->getDefaults()), array('_route' => $name));
$parameters = array('_route' => $name);
if ($route->getOption('negotiate')) {
$parameters['_negotiation'] = $negotiationVariants;
}

return array_merge($this->mergeDefaults($matches, $route->getDefaults()), $parameters);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Routing/RequestAcceptance.php
@@ -1,5 +1,14 @@
<?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;

use Symfony\Component\HttpFoundation\AcceptHeaderParser;
Expand Down

0 comments on commit 94e2df2

Please sign in to comment.