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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Mvc\View\Http;
use ArrayAccess;
use Traversable;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Mvc\MvcEvent;
use Zend\ServiceManager\ServiceManager;
use Zend\View\HelperPluginManager as ViewHelperManager;
use Zend\View\Resolver as ViewResolver;
use Zend\View\View;
/**
* Prepares the view layer
*
* Instantiates and configures all classes related to the view layer, including
* the renderer (and its associated resolver(s) and helper manager), the view
* object (and its associated rendering strategies), and the various MVC
* strategies and listeners.
*
* Defines and manages the following services:
*
* - ViewHelperManager (also aliased to Zend\View\HelperPluginManager)
* - ViewTemplateMapResolver (also aliased to Zend\View\Resolver\TemplateMapResolver)
* - ViewTemplatePathStack (also aliased to Zend\View\Resolver\TemplatePathStack)
* - ViewResolver (also aliased to Zend\View\Resolver\AggregateResolver and ResolverInterface)
* - ViewRenderer (also aliased to Zend\View\Renderer\PhpRenderer and RendererInterface)
* - ViewPhpRendererStrategy (also aliased to Zend\View\Strategy\PhpRendererStrategy)
* - View (also aliased to Zend\View\View)
* - DefaultRenderingStrategy (also aliased to Zend\Mvc\View\Http\DefaultRenderingStrategy)
* - ExceptionStrategy (also aliased to Zend\Mvc\View\Http\ExceptionStrategy)
* - RouteNotFoundStrategy (also aliased to Zend\Mvc\View\Http\RouteNotFoundStrategy and 404Strategy)
* - ViewModel
*/
class ViewManager extends AbstractListenerAggregate
{
/**
* @var object application configuration service
*/
protected $config;
/**
* @var MvcEvent
*/
protected $event;
/**
* @var ServiceManager
*/
protected $services;
/**@+
* Various properties representing strategies and objects instantiated and
* configured by the view manager
*/
protected $helperManager;
protected $mvcRenderingStrategy;
protected $renderer;
protected $rendererStrategy;
protected $resolver;
protected $view;
protected $viewModel;
/**@-*/
/**
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events, $priority = 1)
{
$this->listeners[] = $events->attach(MvcEvent::EVENT_BOOTSTRAP, [$this, 'onBootstrap'], 10000);
}
/**
* Prepares the view layer
*
* @param $event
* @return void
*/
public function onBootstrap($event)
{
$application = $event->getApplication();
$services = $application->getServiceManager();
$config = $services->get('config');
$events = $application->getEventManager();
$sharedEvents = $events->getSharedManager();
$this->config = isset($config['view_manager']) && (is_array($config['view_manager']) || $config['view_manager'] instanceof ArrayAccess)
? $config['view_manager']
: [];
$this->services = $services;
$this->event = $event;
$routeNotFoundStrategy = $services->get('HttpRouteNotFoundStrategy');
$exceptionStrategy = $services->get('HttpExceptionStrategy');
$mvcRenderingStrategy = $services->get('HttpDefaultRenderingStrategy');
$this->injectViewModelIntoPlugin();
$injectTemplateListener = $services->get('Zend\Mvc\View\Http\InjectTemplateListener');
$createViewModelListener = new CreateViewModelListener();
$injectViewModelListener = new InjectViewModelListener();
$this->registerMvcRenderingStrategies($events);
$this->registerViewStrategies();
$routeNotFoundStrategy->attach($events);
$exceptionStrategy->attach($events);
$events->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$injectViewModelListener, 'injectViewModel'], -100);
$events->attach(MvcEvent::EVENT_RENDER_ERROR, [$injectViewModelListener, 'injectViewModel'], -100);
$mvcRenderingStrategy->attach($events);
$sharedEvents->attach('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, [$createViewModelListener, 'createViewModelFromArray'], -80);
$sharedEvents->attach('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, [$routeNotFoundStrategy, 'prepareNotFoundViewModel'], -90);
$sharedEvents->attach('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, [$createViewModelListener, 'createViewModelFromNull'], -80);
$sharedEvents->attach('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, [$injectTemplateListener, 'injectTemplate'], -90);
$sharedEvents->attach('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, [$injectViewModelListener, 'injectViewModel'], -100);
}
/**
* Retrieves the View instance
*
* @return View
*/
public function getView()
{
if ($this->view) {
return $this->view;
}
$this->view = $this->services->get(View::class);
return $this->view;
}
/**
* Configures the MvcEvent view model to ensure it has the template injected
*
* @return \Zend\View\Model\ModelInterface
*/
public function getViewModel()
{
if ($this->viewModel) {
return $this->viewModel;
}
$this->viewModel = $model = $this->event->getViewModel();
$layoutTemplate = $this->services->get('HttpDefaultRenderingStrategy')->getLayoutTemplate();
$model->setTemplate($layoutTemplate);
return $this->viewModel;
}
/**
* Register additional mvc rendering strategies
*
* If there is a "mvc_strategies" key of the view manager configuration, loop
* through it. Pull each as a service from the service manager, and, if it
* is a ListenerAggregate, attach it to the view, at priority 100. This
* latter allows each to trigger before the default mvc rendering strategy,
* and for them to trigger in the order they are registered.
*
* @param EventManagerInterface $events
* @return void
*/
protected function registerMvcRenderingStrategies(EventManagerInterface $events)
{
if (! isset($this->config['mvc_strategies'])) {
return;
}
$mvcStrategies = $this->config['mvc_strategies'];
if (is_string($mvcStrategies)) {
$mvcStrategies = [$mvcStrategies];
}
if (! is_array($mvcStrategies) && ! $mvcStrategies instanceof Traversable) {
return;
}
foreach ($mvcStrategies as $mvcStrategy) {
if (! is_string($mvcStrategy)) {
continue;
}
$listener = $this->services->get($mvcStrategy);
if ($listener instanceof ListenerAggregateInterface) {
$listener->attach($events, 100);
}
}
}
/**
* Register additional view strategies
*
* If there is a "strategies" key of the view manager configuration, loop
* through it. Pull each as a service from the service manager, and, if it
* is a ListenerAggregate, attach it to the view, at priority 100. This
* latter allows each to trigger before the default strategy, and for them
* to trigger in the order they are registered.
*
* @return void
*/
protected function registerViewStrategies()
{
if (! isset($this->config['strategies'])) {
return;
}
$strategies = $this->config['strategies'];
if (is_string($strategies)) {
$strategies = [$strategies];
}
if (! is_array($strategies) && ! $strategies instanceof Traversable) {
return;
}
$view = $this->getView();
$events = $view->getEventManager();
foreach ($strategies as $strategy) {
if (! is_string($strategy)) {
continue;
}
$listener = $this->services->get($strategy);
if ($listener instanceof ListenerAggregateInterface) {
$listener->attach($events, 100);
}
}
}
/**
* Injects the ViewModel view helper with the root view model.
*/
private function injectViewModelIntoPlugin()
{
$model = $this->getViewModel();
$plugins = $this->services->get('ViewHelperManager');
$plugin = $plugins->get('viewmodel');
$plugin->setRoot($model);
}
}