Skip to content

Instantly share code, notes, and snippets.

@merk
Last active December 20, 2015 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save merk/6137212 to your computer and use it in GitHub Desktop.
Save merk/6137212 to your computer and use it in GitHub Desktop.
Service ParamConverter
services:
ibms.service_param_converter:
class: Infinite\Helper\ParamConverter\ServiceParamConverter
arguments:
- @service_container
tags:
- { name: request.param_converter, converter: service, priority: 10 }
<?php
class CustomerController extends BaseCustomerController
{
/**
* Renders an appliance choice form allowing the user to
* add an appliance if the required one doesnt exist.
*
* @ParamConverter("customer", converter="service", options={
* "service" = "ibms_customer.customer_manager",
* })
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Ibms\CustomerBundle\Entity\Customer $customer
* @return $this
*/
public function applianceChoiceAction(Request $request, Customer $customer)
{
// ...
}
}
<?php
/**
* This file is part of the Infinite Helper Library
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Infinite\Helper\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Request;
/**
* A service aware param converter, converting request parameters
* into objects based on service/method defined in the ParamConverter's
* options array.
*
* @author Tim Nagel <t.nagel@infinite.net.au>
*/
class ServiceParamConverter implements ParamConverterInterface
{
/**
* @var ContainerInterface
*/
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function apply(Request $request, ConfigurationInterface $configuration)
{
$options = $this->getOptions($configuration);
$service = $this->container->get($options['service']);
$arguments = $this->buildArguments($options['mapping'], $request);
$arguments = array_filter($arguments, function ($value) {
return !is_null($value);
});
if (!$arguments) {
if (false === $configuration->isOptional()) {
throw new NotFoundHttpException(sprintf('%s object not found.', $configuration->getName()));
} else {
return false;
}
}
$object = call_user_func_array(array($service, $options['method']), $arguments);
if (null === $object && false === $configuration->isOptional()) {
throw new NotFoundHttpException(sprintf('%s object not found.', $configuration->getName()));
}
$request->attributes->set($configuration->getName(), $object);
return true;
}
protected function buildArguments(array $mapping, Request $request)
{
$arguments = array();
foreach ($mapping as $attribute) {
$arguments[] = $request->attributes->get($attribute);
}
return $arguments;
}
public function supports(ConfigurationInterface $configuration)
{
$options = $this->getOptions($configuration);
if (null === $options['service']) {
return false;
}
return true;
}
protected function getOptions(ConfigurationInterface $configuration)
{
return array_replace(array(
'mapping' => array('id'),
'method' => "find",
'service' => null,
), $configuration->getOptions());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment