Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
Fixed failing on unknown entities
Browse files Browse the repository at this point in the history
 - By catching more exceptions, the isManaged is more robust.
 - If an unresolvable value is passed to the generator, or it resolves
   to an empty string, it will now fail with a proper exception message.
  • Loading branch information
Iltar van der Berg authored and linaori committed Aug 24, 2017
1 parent 891d7bd commit 1d1fc6a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/DoctrineBridge/EntityIdDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public function isManaged($object)

try {
$this->registry->getRepository($className);
} catch (MappingException $e) {
} catch (\Exception $e) {
return $this->resolvedCache[$className] = false;
} catch (\Throwable $e) {
return $this->resolvedCache[$className] = false;
}

Expand Down
37 changes: 37 additions & 0 deletions src/Exception/UnresolvedParameterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Iltar\HttpBundle\Exception;

/**
* @author Iltar van der Berg <kjarli@gmail.com>
*/
class UnresolvedParameterException extends \RuntimeException
{
/**
* @var string[]
*/
private $parameters;

/**
* @param string $route
* @param string[] $parameters
*/
public function __construct($route, array $parameters)
{
parent::__construct(sprintf(
'Parameters for the route \'%s\' could not be resolved to scalar values. Parameters: "%s".',
$route,
implode(', ', array_keys($parameters))
));

$this->parameters = $parameters;
}

/**
* @return string[]
*/
public function getParameters()
{
return $this->parameters;
}
}
22 changes: 22 additions & 0 deletions src/Router/ParameterResolvingRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Iltar\HttpBundle\Router;

use Iltar\HttpBundle\Exception\UnresolvedParameterException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand Down Expand Up @@ -63,6 +64,27 @@ public function getRouteCollection()
*/
public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
{
$resolvedParameters = $this->resolverCollection->resolve($parameters);
$unresolved = [];

foreach ($resolvedParameters as $key => $parameter) {
// sanity check for parameters that could not be resolved at all
if (!is_scalar($parameter)) {
$unresolved[$key] = is_object($parameter) ? get_class($parameter): gettype($parameter);
continue;
}

// sanity check for parameters that could not be resolved into a usable value
if ('' === (string) $parameter) {
$unresolved[$key] = gettype($parameter);
continue;
}
}

if (0 !== count($unresolved)) {
throw new UnresolvedParameterException($name, $unresolved);
}

return $this->router->generate($name, $this->resolverCollection->resolve($parameters), $referenceType);
}

Expand Down
17 changes: 17 additions & 0 deletions test/Functional/RouteGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,21 @@ public function testEntityIdResolverWithWithoutAvailableAccessor()

$router->generate('message_route', ['blind_write' => new BlindWrite(10)]);
}

/**
* @expectedException \Iltar\HttpBundle\Exception\UnresolvedParameterException
* @expectedExceptionMessage Parameters for the route 'mapped_fallback_route' could not be resolved to scalar values. Parameters: "object_key, empty_key, array_key, resource_key".
*/
public function testEntityIdResolverWithWithoutAlternatives()
{
$router = static::$kernel->getContainer()->get('router');

$router->generate('mapped_fallback_route', [
'object_key' => new \stdClass(),
'empty_key' => '',
'array_key' => [],
'normal_key' => '10',
'resource_key' => fopen(__FILE__, 'rb'),
]);
}
}

0 comments on commit 1d1fc6a

Please sign in to comment.