|
1
|
|
|
<?php |
|
2
|
|
|
namespace danrevah\SandboxBundle\EventListener; |
|
3
|
|
|
|
|
4
|
|
|
use danrevah\SandboxBundle\Managers\SandboxResponseManager; |
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
|
8
|
|
|
|
|
9
|
|
|
class SandboxListener |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var \Symfony\Component\HttpFoundation\Request |
|
13
|
|
|
*/ |
|
14
|
|
|
private $request; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var \danrevah\SandboxBundle\Managers\SandboxResponseManager |
|
17
|
|
|
*/ |
|
18
|
|
|
private $sandboxResponseManager; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param RequestStack $requestStack |
|
22
|
|
|
* @param SandboxResponseManager $sandboxResponseManager |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct( |
|
25
|
|
|
RequestStack $requestStack, |
|
26
|
|
|
SandboxResponseManager $sandboxResponseManager |
|
27
|
|
|
) { |
|
28
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
|
29
|
|
|
$this->sandboxResponseManager = $sandboxResponseManager; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Overriding response in Sandbox mode |
|
34
|
|
|
* |
|
35
|
|
|
* @param FilterControllerEvent $event |
|
36
|
|
|
* @throws \Exception |
|
37
|
|
|
*/ |
|
38
|
|
|
public function onKernelController(FilterControllerEvent $event) |
|
39
|
|
|
{ |
|
40
|
|
|
$controller = $event->getController(); |
|
41
|
|
|
|
|
42
|
|
|
list($responseController,,,) = $this->sandboxResponseManager->getResponseController( |
|
43
|
|
|
$controller[0], |
|
44
|
|
|
$controller[1], |
|
45
|
|
|
$this->request->query, |
|
46
|
|
|
$this->request->request, |
|
47
|
|
|
$this->getStreamParams() |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
// Fall back to real controller if none has been found and force mode is false |
|
51
|
|
|
if ($responseController === false) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$event->setController($responseController); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return ArrayCollection |
|
61
|
|
|
*/ |
|
62
|
|
|
private function getStreamParams() |
|
63
|
|
|
{ |
|
64
|
|
|
// get parameters from stream |
|
65
|
|
|
$request = $this->request->getContent(); |
|
66
|
|
|
$requestArray = json_decode($request, true); |
|
67
|
|
|
$streamParams = $requestArray === null ? new ArrayCollection() : new ArrayCollection($requestArray); |
|
68
|
|
|
return $streamParams; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|