1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: mglaub |
5
|
|
|
* Date: 22.06.2015 |
6
|
|
|
* Time: 15:53 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace MaglLegacyApplication\Service; |
10
|
|
|
|
11
|
|
|
use Laminas\EventManager\EventManager; |
12
|
|
|
use Laminas\Http\Response; |
13
|
|
|
use Laminas\Mvc\Controller\AbstractActionController; |
14
|
|
|
use Laminas\Mvc\MvcEvent; |
15
|
|
|
use Laminas\Mvc\View\Http\DefaultRenderingStrategy; |
16
|
|
|
use Laminas\Router\RouteMatch; |
17
|
|
|
use Laminas\Stdlib\ResponseInterface; |
18
|
|
|
use Laminas\View\Model\ViewModel; |
19
|
|
|
|
20
|
|
|
class ControllerService |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var MvcEvent |
25
|
|
|
*/ |
26
|
|
|
private $event; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EventManager |
30
|
|
|
*/ |
31
|
|
|
private $eventManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var RouteMatch |
35
|
|
|
*/ |
36
|
|
|
private $routeMatch; |
37
|
|
|
|
38
|
1 |
|
public function __construct(EventManager $eventManager, MvcEvent $event) |
39
|
|
|
{ |
40
|
1 |
|
$this->eventManager = $eventManager; |
41
|
1 |
|
$this->event = $event; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $controllerName |
46
|
|
|
* @param $action |
47
|
|
|
* @param array $params |
48
|
|
|
* @return string|ResponseInterface |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
1 |
|
public function runControllerAction($controllerName, $action, $params = array()) |
52
|
|
|
{ |
53
|
1 |
|
$this->event->getRouteMatch() |
54
|
1 |
|
->setParam('controller', $controllerName) |
55
|
1 |
|
->setParam('action', $action); |
56
|
|
|
|
57
|
1 |
|
foreach ($params as $key => $value) { |
58
|
|
|
$this->event->getRouteMatch()->setParam($key, $value); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$serviceManager = $this->event->getApplication()->getServiceManager(); |
62
|
1 |
|
$controllerManager = $serviceManager->get('ControllerManager'); |
63
|
|
|
|
64
|
|
|
/** @var AbstractActionController $controller */ |
65
|
1 |
|
$controller = $controllerManager->get($controllerName); |
66
|
|
|
|
67
|
1 |
|
$controller->setEvent($this->event); |
68
|
1 |
|
$result = $controller->dispatch($this->event->getRequest()); |
69
|
|
|
|
70
|
1 |
|
if ($result instanceof Response) { |
71
|
|
|
return $result; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @var DefaultRenderingStrategy $renderingStrategy */ |
75
|
1 |
|
$renderingStrategy = null; |
76
|
1 |
|
foreach (array('HttpDefaultRenderingStrategy', 'DefaultRenderingStrategy') as $serviceName) { |
77
|
1 |
|
if ($serviceManager->has($serviceName)) { |
78
|
1 |
|
$renderingStrategy = $serviceManager->get($serviceName); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
$this->event->setViewModel($result); |
83
|
|
|
|
84
|
1 |
|
if ($renderingStrategy instanceof DefaultRenderingStrategy) { |
85
|
|
|
/** @var ViewModel $result */ |
86
|
1 |
|
if (!$result->terminate()) { |
87
|
1 |
|
$layout = new ViewModel(); |
88
|
1 |
|
$layoutTemplate = $renderingStrategy->getLayoutTemplate(); |
89
|
1 |
|
$layout->setTemplate($layoutTemplate); |
90
|
1 |
|
$layout->addChild($result); |
91
|
1 |
|
$this->event->setViewModel($layout); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return $renderingStrategy->render($this->event); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|