Last active
December 20, 2015 13:18
-
-
Save merk/6137212 to your computer and use it in GitHub Desktop.
Service ParamConverter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
ibms.service_param_converter: | |
class: Infinite\Helper\ParamConverter\ServiceParamConverter | |
arguments: | |
- @service_container | |
tags: | |
- { name: request.param_converter, converter: service, priority: 10 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) | |
{ | |
// ... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 <[email protected]> | |
*/ | |
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