1 | <?php |
||
29 | class ServiceParamConverter implements ParamConverterInterface |
||
30 | { |
||
31 | /** |
||
32 | * Service container. |
||
33 | * |
||
34 | * @var Container |
||
35 | */ |
||
36 | private $container; |
||
37 | |||
38 | /** |
||
39 | * Class constructor. |
||
40 | * |
||
41 | * @param Container $container Service container. |
||
42 | */ |
||
43 | 7 | public function __construct(Container $container) |
|
47 | |||
48 | /** |
||
49 | * Apply param converter. |
||
50 | * |
||
51 | * @param Request $request Request |
||
52 | * @param ParamConverter $configuration Param converter configuration. |
||
53 | * @return boolean |
||
54 | */ |
||
55 | 6 | public function apply(Request $request, ParamConverter $configuration) |
|
56 | { |
||
57 | $param = $configuration->getName(); |
||
58 | $options = $this->getOptions($configuration); |
||
59 | |||
60 | 6 | foreach ($options['arguments'] as &$value) { |
|
61 | if (strpos($value, '%') === 0 && strrpos($value, '%') === strlen($value)-1) { |
||
62 | $value = $request->get(substr($value, 1, strlen($value)-2)); |
||
63 | } elseif (strpos($value, '@') === 0) { |
||
64 | if (!$this->container->has(substr($value, 1))) { |
||
65 | throw new InvalidArgumentException("Unknown service requested: ".$value); |
||
66 | } |
||
67 | $value = $this->container->get(substr($value, 1)); |
||
68 | 1 | } |
|
69 | 3 | } |
|
70 | |||
71 | $service = $this->container->get($options['service']); |
||
72 | $return = call_user_func_array(array($service, $options['method']), $options['arguments']); |
||
73 | |||
74 | if ($configuration->getClass() && (!is_object($return) || !is_a($return, $configuration->getClass()))) { |
||
75 | 1 | return false; |
|
76 | } |
||
77 | |||
78 | $request->attributes->set($param, $return); |
||
79 | |||
80 | 4 | return true; |
|
81 | 5 | } |
|
82 | |||
83 | public function supports(ParamConverter $configuration) |
||
89 | |||
90 | 6 | protected function getOptions(ParamConverter $configuration) |
|
102 | } |