|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* All rights reserved |
|
5
|
|
|
* Copyright 2015 Gendoria |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Gendoria\ParamConverterBundle\Request\ParamConverter; |
|
9
|
|
|
|
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
|
12
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* This param converter converts parameters based on service method call. |
|
18
|
|
|
* |
|
19
|
|
|
* Param converter invocation: |
|
20
|
|
|
* |
|
21
|
|
|
* `@ParamConverter("parameter_name", converter="service_param_converter", options={"service" = "service_id", "method" = "service_method", "arguments" = {"%requestParamName%", "@otherServiceId", "someParameter"})` |
|
22
|
|
|
* |
|
23
|
|
|
* Using this converter you can inject virtually any parameter into a controller, as service method call is not restricted with anything. |
|
24
|
|
|
* You should be **very cautious**, though, when passing request parameters into a service method call, as it can potentially lead to injections of malicious code. |
|
25
|
|
|
* All of the parameters injected should be properly restricted (eg to integers) using routing configuration. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Tomasz Struczyński <[email protected]> |
|
28
|
|
|
*/ |
|
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
|
10 |
|
public function __construct(Container $container) |
|
44
|
|
|
{ |
|
45
|
10 |
|
$this->container = $container; |
|
46
|
10 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Apply param converter. |
|
50
|
|
|
* |
|
51
|
|
|
* @param Request $request Request |
|
52
|
|
|
* @param ParamConverter $configuration Param converter configuration. |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
* |
|
56
|
|
|
* @throws InvalidArgumentException |
|
57
|
|
|
*/ |
|
58
|
9 |
|
public function apply(Request $request, ParamConverter $configuration) |
|
59
|
|
|
{ |
|
60
|
9 |
|
$param = $configuration->getName(); |
|
61
|
9 |
|
$options = $this->getOptions($configuration); |
|
62
|
|
|
|
|
63
|
9 |
|
foreach ($options['arguments'] as &$value) { |
|
64
|
7 |
|
$value = $this->parseArgument($value, $request); |
|
65
|
7 |
|
} |
|
66
|
|
|
|
|
67
|
7 |
|
$service = $this->container->get($options['service']); |
|
68
|
7 |
|
$return = call_user_func_array(array($service, $options['method']), $options['arguments']); |
|
69
|
|
|
|
|
70
|
7 |
|
if ($configuration->getClass() && (!is_object($return) || !is_a($return, $configuration->getClass()))) { |
|
71
|
1 |
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
6 |
|
$request->attributes->set($param, $return); |
|
75
|
|
|
|
|
76
|
6 |
|
return true; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Parse single argument. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $value |
|
83
|
|
|
* @param Request $request |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
* |
|
87
|
|
|
* @throws InvalidArgumentException |
|
88
|
|
|
*/ |
|
89
|
7 |
|
private function parseArgument($value, Request $request) |
|
90
|
|
|
{ |
|
91
|
7 |
|
if (strpos($value, '%') === 0 && strrpos($value, '%') === strlen($value)-1) { |
|
92
|
4 |
|
return $this->parseParameterArgument(substr($value, 1, strlen($value)-2), $request); |
|
93
|
3 |
|
} elseif (strpos($value, '@') === 0) { |
|
94
|
2 |
|
if ($this->container->has(substr($value, 1)) === false) { |
|
95
|
1 |
|
throw new InvalidArgumentException('Unknown service requested: '.$value); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
return $this->container->get(substr($value, 1)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
return $value; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Parse parameter type argument. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $value |
|
108
|
|
|
* @param Request $request |
|
109
|
|
|
* @return type |
|
110
|
|
|
*/ |
|
111
|
4 |
|
private function parseParameterArgument($value, Request $request) |
|
112
|
|
|
{ |
|
113
|
4 |
|
if ($this !== $result = $request->get($value, $this)) { |
|
114
|
2 |
|
return $result; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
//Argument may refer to function call of another argument |
|
118
|
3 |
|
if (strpos($value, '::')) { |
|
119
|
3 |
|
$paramName = substr($value, 0, strpos($value, '::')); |
|
120
|
3 |
|
$fnData = substr($value, strpos($value, '::')+2); |
|
121
|
3 |
|
if (strpos($fnData, '(')) { |
|
122
|
2 |
|
$fnName = substr($fnData, 0, strpos($fnData, '(')); |
|
123
|
2 |
|
$fnArguments = $this->parseFunctionArguments(substr($fnData, strpos($fnData, '(')), $request); |
|
124
|
1 |
|
} else { |
|
125
|
1 |
|
$fnName = $fnData; |
|
126
|
1 |
|
$fnArguments = array(); |
|
127
|
|
|
} |
|
128
|
2 |
|
$possibleObject = $request->get($paramName); |
|
129
|
2 |
|
if (!$possibleObject || !is_callable(array($possibleObject, $fnName))) { |
|
130
|
|
|
return null; |
|
131
|
|
|
} |
|
132
|
2 |
|
return call_user_func_array(array($possibleObject, $fnName), $fnArguments); |
|
133
|
|
|
} |
|
134
|
|
|
return null; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
2 |
|
private function parseFunctionArguments($argumentsStr, Request $request) |
|
138
|
|
|
{ |
|
139
|
2 |
|
if (strpos($argumentsStr, '(') !== 0 || strpos($argumentsStr, ')') !== strlen($argumentsStr)-1) { |
|
140
|
1 |
|
throw new \InvalidArgumentException("Incorrect function arguments string"); |
|
141
|
|
|
} |
|
142
|
1 |
|
$argumentsArr = explode(",", substr($argumentsStr, 1, strlen($argumentsStr)-2)); |
|
143
|
1 |
|
foreach ($argumentsArr as &$argument) { |
|
144
|
1 |
|
$argument = $this->parseArgument($argument, $request); |
|
145
|
1 |
|
} |
|
146
|
1 |
|
return $argumentsArr; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
public function supports(ParamConverter $configuration) |
|
150
|
|
|
{ |
|
151
|
1 |
|
$options = $this->getOptions($configuration); |
|
152
|
|
|
|
|
153
|
1 |
|
return $this->container->has($options['service']) && !empty($options['method']); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
10 |
|
protected function getOptions(ParamConverter $configuration) |
|
157
|
|
|
{ |
|
158
|
10 |
|
$options = $configuration->getOptions(); |
|
159
|
10 |
|
if (!is_array($options)) { |
|
160
|
1 |
|
$options = array(); |
|
161
|
1 |
|
} |
|
162
|
|
|
|
|
163
|
10 |
|
return array_replace(array( |
|
164
|
10 |
|
'service' => null, |
|
165
|
10 |
|
'method' => null, |
|
166
|
10 |
|
'arguments' => array(), |
|
167
|
10 |
|
), $options); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|