-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContainerAwareRestfulStrategy.php
63 lines (53 loc) · 1.81 KB
/
ContainerAwareRestfulStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace Puzzle\Route\Strategy;
use League\Route\Strategy\RestfulStrategy;
use ArrayObject;
use League\Route\Http\Exception as HttpException;
use RuntimeException;
use Symfony\Component\HttpFoundation\JsonResponse;
class ContainerAwareRestfulStrategy extends RestfulStrategy
{
/**
* {@inheritdoc}
*/
public function dispatch($controller, array $vars)
{
try {
$response = $this->invokeController($controller, [
$this->getRequest(),
$vars,
$this->getContainer()->get('config')
]);
if ($response instanceof JsonResponse) {
return $response;
}
if (is_array($response) || $response instanceof ArrayObject) {
return new JsonResponse($response);
}
throw new RuntimeException(
'Your controller action must return a valid response for the Restful Strategy. ' .
'Acceptable responses are of type: [Array], [ArrayObject] and [Symfony\Component\HttpFoundation\JsonResponse]'
);
} catch (HttpException $e) {
return $e->getJsonResponse();
}
}
/**
* Invoke a controller action
*
* @param string|array|\Closure $controller
* @param array $vars
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function invokeController($controller, array $vars = [])
{
if (is_array($controller)) {
$controller = [
(is_object($controller[0])) ? $controller[0] : $this->getContainer()->get($controller[0]),
$controller[1]
];
}
$controller[0]->setContainer($this->getContainer());
return call_user_func_array($controller, array_values($vars));
}
}