This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
/
DefaultRenderingStrategy.php
126 lines (109 loc) · 3.19 KB
/
DefaultRenderingStrategy.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* @link http://github.com/zendframework/zend-mvc for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-mvc/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Mvc\View\Http;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Application;
use Zend\Mvc\MvcEvent;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\View\Model\ModelInterface as ViewModel;
use Zend\View\View;
class DefaultRenderingStrategy extends AbstractListenerAggregate
{
/**
* Layout template - template used in root ViewModel of MVC event.
*
* @var string
*/
protected $layoutTemplate = 'layout';
/**
* @var View
*/
protected $view;
/**
* Set view
*
* @param View $view
* @return DefaultRenderingStrategy
*/
public function __construct(View $view)
{
$this->view = $view;
}
/**
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events, $priority = 1)
{
$this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER, [$this, 'render'], -10000);
$this->listeners[] = $events->attach(MvcEvent::EVENT_RENDER_ERROR, [$this, 'render'], -10000);
}
/**
* Set layout template value
*
* @param string $layoutTemplate
* @return DefaultRenderingStrategy
*/
public function setLayoutTemplate($layoutTemplate)
{
$this->layoutTemplate = (string) $layoutTemplate;
return $this;
}
/**
* Get layout template value
*
* @return string
*/
public function getLayoutTemplate()
{
return $this->layoutTemplate;
}
/**
* Render the view
*
* @param MvcEvent $e
* @return Response|null
* @throws \Exception
*/
public function render(MvcEvent $e)
{
$result = $e->getResult();
if ($result instanceof Response) {
return $result;
}
// Martial arguments
$request = $e->getRequest();
$response = $e->getResponse();
$viewModel = $e->getViewModel();
if (! $viewModel instanceof ViewModel) {
return;
}
$view = $this->view;
$view->setRequest($request);
$view->setResponse($response);
$caughtException = null;
try {
$view->render($viewModel);
} catch (\Throwable $ex) {
$caughtException = $ex;
} catch (\Exception $ex) { // @TODO clean up once PHP 7 requirement is enforced
$caughtException = $ex;
}
if ($caughtException !== null) {
if ($e->getName() === MvcEvent::EVENT_RENDER_ERROR) {
throw $caughtException;
}
$application = $e->getApplication();
$events = $application->getEventManager();
$e->setError(Application::ERROR_EXCEPTION);
$e->setParam('exception', $caughtException);
$e->setName(MvcEvent::EVENT_RENDER_ERROR);
$events->triggerEvent($e);
}
return $response;
}
}