1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\Request; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Util\ClassUtils; |
15
|
|
|
use FOS\RestBundle\Controller\Annotations\ParamInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Contains the {@link ParamFetcher} params and links them to a request. |
20
|
|
|
* |
21
|
|
|
* @internal |
22
|
|
|
*/ |
23
|
|
|
final class ParameterBag |
24
|
|
|
{ |
25
|
|
|
private $paramReader; |
26
|
|
|
private $params = array(); |
27
|
|
|
|
28
|
22 |
|
public function __construct(ParamReaderInterface $paramReader) |
29
|
|
|
{ |
30
|
22 |
|
$this->paramReader = $paramReader; |
31
|
22 |
|
} |
32
|
|
|
|
33
|
11 |
|
public function getParams(Request $request) |
34
|
|
|
{ |
35
|
11 |
|
$requestId = spl_object_hash($request); |
36
|
11 |
|
if (!isset($this->params[$requestId]) || empty($this->params[$requestId]['controller'])) { |
37
|
1 |
|
throw new \InvalidArgumentException('Controller and method needs to be set via setController.'); |
38
|
|
|
} |
39
|
10 |
|
if ($this->params[$requestId]['params'] === null) { |
40
|
10 |
|
return $this->initParams($requestId); |
41
|
|
|
} |
42
|
|
|
|
43
|
7 |
|
return $this->params[$requestId]['params']; |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function addParam(Request $request, ParamInterface $param) |
47
|
|
|
{ |
48
|
1 |
|
$requestId = spl_object_hash($request); |
49
|
1 |
|
$this->getParams($request); |
50
|
|
|
|
51
|
1 |
|
$this->params[$requestId]['params'][$param->getName()] = $param; |
52
|
1 |
|
} |
53
|
|
|
|
54
|
11 |
|
public function setController(Request $request, $controller) |
55
|
|
|
{ |
56
|
11 |
|
$requestId = spl_object_hash($request); |
57
|
11 |
|
$this->params[$requestId] = array( |
58
|
11 |
|
'controller' => $controller, |
59
|
11 |
|
'params' => null, |
60
|
|
|
); |
61
|
11 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Initialize the parameters. |
65
|
|
|
* |
66
|
|
|
* @param string $requestId |
67
|
|
|
* |
68
|
|
|
* @throws \InvalidArgumentException |
69
|
|
|
*/ |
70
|
10 |
|
private function initParams($requestId) |
71
|
|
|
{ |
72
|
10 |
|
$controller = $this->params[$requestId]['controller']; |
73
|
10 |
|
if (!is_array($controller) || empty($controller[0]) || !is_object($controller[0])) { |
74
|
3 |
|
throw new \InvalidArgumentException( |
75
|
|
|
'Controller needs to be set as a class instance (closures/functions are not supported)' |
76
|
3 |
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
7 |
|
return $this->params[$requestId]['params'] = $this->paramReader->read( |
80
|
7 |
|
new \ReflectionClass(ClassUtils::getClass($controller[0])), |
81
|
7 |
|
$controller[1] |
82
|
7 |
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|