Commit | Line | Data |
---|---|---|
44d399bc S |
1 | <?php |
2 | /** | |
3 | * Zend Framework (http://framework.zend.com/) | |
4 | * | |
5 | * @link http://github.com/zendframework/zf2 for the canonical source repository | |
6 | * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | |
7 | * @license http://framework.zend.com/license/new-bsd New BSD License | |
8 | */ | |
9 | ||
10 | namespace Zend\Mvc\View\Http; | |
11 | ||
12 | use Zend\EventManager\AbstractListenerAggregate; | |
13 | use Zend\EventManager\EventManagerInterface; | |
14 | use Zend\Mvc\Application; | |
15 | use Zend\Mvc\MvcEvent; | |
16 | use Zend\Stdlib\ResponseInterface as Response; | |
17 | use Zend\View\Model\ModelInterface as ViewModel; | |
18 | use Zend\View\View; | |
19 | ||
20 | class DefaultRenderingStrategy extends AbstractListenerAggregate | |
21 | { | |
22 | /** | |
23 | * Layout template - template used in root ViewModel of MVC event. | |
24 | * | |
25 | * @var string | |
26 | */ | |
27 | protected $layoutTemplate = 'layout'; | |
28 | ||
29 | /** | |
30 | * @var View | |
31 | */ | |
32 | protected $view; | |
33 | ||
34 | /** | |
35 | * Set view | |
36 | * | |
37 | * @param View $view | |
38 | * @return DefaultRenderingStrategy | |
39 | */ | |
40 | public function __construct(View $view) | |
41 | { | |
42 | $this->view = $view; | |
43 | } | |
44 | ||
45 | /** | |
46 | * {@inheritDoc} | |
47 | */ | |
48 | public function attach(EventManagerInterface $events, $priority = 1) | |
49 | { | |
50 | $this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER, [$this, 'render'], -10000); | |
51 | $this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER_ERROR, [$this, 'render'], -10000); | |
52 | } | |
53 | ||
54 | /** | |
55 | * Set layout template value | |
56 | * | |
57 | * @param string $layoutTemplate | |
58 | * @return DefaultRenderingStrategy | |
59 | */ | |
60 | public function setLayoutTemplate($layoutTemplate) | |
61 | { | |
62 | $this->layoutTemplate = (string) $layoutTemplate; | |
63 | return $this; | |
64 | } | |
65 | ||
66 | /** | |
67 | * Get layout template value | |
68 | * | |
69 | * @return string | |
70 | */ | |
71 | public function getLayoutTemplate() | |
72 | { | |
73 | return $this->layoutTemplate; | |
74 | } | |
75 | ||
76 | /** | |
77 | * Render the view | |
78 | * | |
79 | * @param MvcEvent $e | |
80 | * @return Response|null | |
81 | * @throws \Exception | |
82 | */ | |
83 | public function render(MvcEvent $e) | |
84 | { | |
85 | $result = $e->getResult(); | |
86 | if ($result instanceof Response) { | |
87 | return $result; | |
88 | } | |
89 | ||
90 | // Martial arguments | |
91 | $request = $e->getRequest(); | |
92 | $response = $e->getResponse(); | |
93 | $viewModel = $e->getViewModel(); | |
94 | if (!$viewModel instanceof ViewModel) { | |
95 | return; | |
96 | } | |
97 | ||
98 | $view = $this->view; | |
99 | $view->setRequest($request); | |
100 | $view->setResponse($response); | |
101 | ||
102 | $caughtException = null; | |
103 | ||
104 | try { | |
105 | $view->render($viewModel); | |
106 | } catch (\Throwable $ex) { | |
107 | $caughtException = $ex; | |
108 | } catch (\Exception $ex) { // @TODO clean up once PHP 7 requirement is enforced | |
109 | $caughtException = $ex; | |
110 | } | |
111 | ||
112 | if ($caughtException !== null) { | |
113 | if ($e->getName() === MvcEvent::EVENT_RENDER_ERROR) { | |
114 | throw $caughtException; | |
115 | } | |
116 | ||
117 | $application = $e->getApplication(); | |
118 | $events = $application->getEventManager(); | |
119 | ||
120 | $e->setError(Application::ERROR_EXCEPTION); | |
121 | $e->setParam('exception', $caughtException); | |
122 | $e->setName(MvcEvent::EVENT_RENDER_ERROR); | |
123 | $events->triggerEvent($e); | |
124 | } | |
125 | ||
126 | return $response; | |
127 | } | |
128 | } |