Skip to content

Instantly share code, notes, and snippets.

@havvg
Last active August 25, 2016 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havvg/54ba33fc11316485887add8aa030f11d to your computer and use it in GitHub Desktop.
Save havvg/54ba33fc11316485887add8aa030f11d to your computer and use it in GitHub Desktop.
<?php
namespace Application\Request\ParamConverter;
use Application\Domain\User\Model\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* @todo Remove this class once https://github.com/symfony/symfony/issues/19354 has been fixed.
*/
final class DomainUserParamConverter implements ParamConverterInterface
{
/**
* @var ParamConverterInterface
*/
private $converter;
/**
* Constructor.
*
* @param ParamConverterInterface $converter
*/
public function __construct(ParamConverterInterface $converter)
{
$this->converter = $converter;
}
/**
* {@inheritdoc}
*/
public function supports(ParamConverter $configuration)
{
return $this->converter->supports($configuration) || User::class === $configuration->getClass();
}
/**
* {@inheritdoc}
*/
public function apply(Request $request, ParamConverter $configuration)
{
if (User::class !== $configuration->getClass()) {
return $this->converter->apply($request, $configuration);
}
// By setting the `convert_user` flag to false on a route, the param converter will be skipped entirely.
// This will trigger the DomainUserValueResolver and return the User entity of the currently logged in user.
// This is required, if route parameters may match a User entity but are not related to the resolution of it.
if ($request->attributes->getBoolean('convert_user', true)) {
return false;
}
try {
return $this->converter->apply($request, $configuration);
} catch (\Exception $e) {
return false;
}
}
}
services:
request.param_converter.domain_user:
class: Application\Request\ParamConverter\DomainUserParamConverter
decorates: sensio_framework_extra.converter.doctrine.orm
arguments:
- '@request.param_converter.domain_user.inner'
tags:
- { name: 'request.param_converter', converter: 'domain_user' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment