9
9
10
10
namespace Zend \Mvc \Service ;
11
11
12
+ use Interop \Container \ContainerInterface ;
12
13
use Zend \Console \Console ;
13
14
use Zend \Mvc \Exception ;
14
15
use Zend \Mvc \Router \RouteMatch ;
15
16
use Zend \ServiceManager \ConfigInterface ;
16
17
use Zend \ServiceManager \ServiceLocatorInterface ;
17
18
use Zend \View \Helper as ViewHelper ;
19
+ use Zend \View \HelperPluginManager ;
18
20
use Zend \View \Helper \HelperInterface as ViewHelperInterface ;
19
21
20
22
class ViewHelperManagerFactory extends AbstractPluginManagerFactory
21
23
{
22
- const PLUGIN_MANAGER_CLASS = ' Zend\View\ HelperPluginManager' ;
24
+ const PLUGIN_MANAGER_CLASS = HelperPluginManager::class ;
23
25
24
26
/**
25
27
* An array of helper configuration classes to ensure are on the helper_map stack.
@@ -43,29 +45,92 @@ public function createService(ServiceLocatorInterface $serviceLocator)
43
45
{
44
46
$ plugins = parent ::createService ($ serviceLocator );
45
47
48
+ // Configure default helpers from other components
49
+ $ plugins = $ this ->configureHelpers ($ plugins );
50
+
51
+ // Override plugin factories
52
+ $ plugins = $ this ->injectOverrideFactories ($ plugins , $ serviceLocator );
53
+
54
+ return $ plugins ;
55
+ }
56
+
57
+ /**
58
+ * Configure helpers from other components.
59
+ *
60
+ * Loops through the list of default helper configuration classes, and uses
61
+ * each to configure the helper plugin manager.
62
+ *
63
+ * @param HelperPluginManager $plugins
64
+ * @return HelperPluginManager
65
+ */
66
+ private function configureHelpers (HelperPluginManager $ plugins )
67
+ {
46
68
foreach ($ this ->defaultHelperMapClasses as $ configClass ) {
47
- if (is_string ($ configClass ) && class_exists ($ configClass )) {
48
- $ config = new $ configClass ;
49
-
50
- if (! $ config instanceof ConfigInterface) {
51
- throw new Exception \ RuntimeException ( sprintf (
52
- ' Invalid service manager configuration class provided; received "%s", expected class implementing %s ' ,
53
- $ configClass ,
54
- ' Zend\ServiceManager\ConfigInterface '
55
- ));
56
- }
57
-
58
- $ config -> configureServiceManager ( $ plugins );
69
+ if (! is_string ($ configClass ) || ! class_exists ($ configClass )) {
70
+ continue ;
71
+ }
72
+
73
+ $ config = new $ configClass ;
74
+
75
+ if (! $ config instanceof ConfigInterface) {
76
+ throw new Exception \ RuntimeException ( sprintf (
77
+ ' Invalid service manager configuration class provided; received "%s", expected class implementing %s ' ,
78
+ $ configClass ,
79
+ ' Zend\ServiceManager\ConfigInterface '
80
+ ) );
59
81
}
82
+
83
+ $ config ->configureServiceManager ($ plugins );
60
84
}
61
85
62
- // Configure URL view helper with router
63
- $ plugins ->setFactory ('url ' , function () use ($ serviceLocator ) {
86
+ return $ plugins ;
87
+ }
88
+
89
+ /**
90
+ * Inject override factories into the plugin manager.
91
+ *
92
+ * @param HelperPluginManager $plugins
93
+ * @param ContainerInterface $services
94
+ * @return HelperPluginManager
95
+ */
96
+ private function injectOverrideFactories (HelperPluginManager $ plugins , ContainerInterface $ services )
97
+ {
98
+ // Configure URL view helper
99
+ $ urlFactory = $ this ->createUrlHelperFactory ($ services );
100
+ $ plugins ->setFactory (ViewHelper \Url::class, $ urlFactory );
101
+ $ plugins ->setFactory ('zendviewhelperurl ' , $ urlFactory );
102
+
103
+ // Configure base path helper
104
+ $ basePathFactory = $ this ->createBasePathHelperFactory ($ services );
105
+ $ plugins ->setFactory (ViewHelper \BasePath::class, $ basePathFactory );
106
+ $ plugins ->setFactory ('zendviewhelperbasepath ' , $ basePathFactory );
107
+
108
+ // Configure doctype view helper
109
+ $ doctypeFactory = $ this ->createDoctypeHelperFactory ($ services );
110
+ $ plugins ->setFactory (ViewHelper \doctype::class, $ doctypeFactory );
111
+ $ plugins ->setFactory ('zendviewhelperdoctype ' , $ doctypeFactory );
112
+
113
+ return $ plugins ;
114
+ }
115
+
116
+ /**
117
+ * Create and return a factory for creating a URL helper.
118
+ *
119
+ * Retrieves the application and router from the servicemanager,
120
+ * and the route match from the MvcEvent composed by the application,
121
+ * using them to configure the helper.
122
+ *
123
+ * @param ContainerInterface $services
124
+ * @return callable
125
+ */
126
+ private function createUrlHelperFactory (ContainerInterface $ services )
127
+ {
128
+ return function () use ($ services ) {
64
129
$ helper = new ViewHelper \Url ;
65
130
$ router = Console::isConsole () ? 'HttpRouter ' : 'Router ' ;
66
- $ helper ->setRouter ($ serviceLocator ->get ($ router ));
131
+ $ helper ->setRouter ($ services ->get ($ router ));
67
132
68
- $ match = $ serviceLocator ->get ('application ' )
133
+ $ match = $ services ->get ('application ' )
69
134
->getMvcEvent ()
70
135
->getRouteMatch ()
71
136
;
@@ -75,10 +140,21 @@ public function createService(ServiceLocatorInterface $serviceLocator)
75
140
}
76
141
77
142
return $ helper ;
78
- });
143
+ };
144
+ }
79
145
80
- $ plugins ->setFactory ('basepath ' , function () use ($ serviceLocator ) {
81
- $ config = $ serviceLocator ->has ('Config ' ) ? $ serviceLocator ->get ('Config ' ) : [];
146
+ /**
147
+ * Create and return a factory for creating a BasePath helper.
148
+ *
149
+ * Uses configuration and request services to configure the helper.
150
+ *
151
+ * @param ContainerInterface $services
152
+ * @return callable
153
+ */
154
+ private function createBasePathHelperFactory (ContainerInterface $ services )
155
+ {
156
+ return function () use ($ services ) {
157
+ $ config = $ services ->has ('config ' ) ? $ services ->get ('config ' ) : [];
82
158
$ basePathHelper = new ViewHelper \BasePath ;
83
159
84
160
if (Console::isConsole ()
@@ -96,31 +172,35 @@ public function createService(ServiceLocatorInterface $serviceLocator)
96
172
return $ basePathHelper ;
97
173
}
98
174
99
- $ request = $ serviceLocator ->get ('Request ' );
175
+ $ request = $ services ->get ('Request ' );
100
176
101
177
if (is_callable ([$ request , 'getBasePath ' ])) {
102
178
$ basePathHelper ->setBasePath ($ request ->getBasePath ());
103
179
}
104
180
105
181
return $ basePathHelper ;
106
- });
107
-
108
- /**
109
- * Configure doctype view helper with doctype from configuration, if available.
110
- *
111
- * Other view helpers depend on this to decide which spec to generate their tags
112
- * based on. This is why it must be set early instead of later in the layout phtml.
113
- */
114
- $ plugins ->setFactory ('doctype ' , function () use ($ serviceLocator ) {
115
- $ config = $ serviceLocator ->has ('Config ' ) ? $ serviceLocator ->get ('Config ' ) : [];
182
+ };
183
+ }
184
+
185
+ /**
186
+ * Create and return a Doctype helper factory.
187
+ *
188
+ * Other view helpers depend on this to decide which spec to generate their tags
189
+ * based on. This is why it must be set early instead of later in the layout phtml.
190
+ *
191
+ * @param ContainerInterface $services
192
+ * @return callable
193
+ */
194
+ private function createDoctypeHelperFactory (ContainerInterface $ services )
195
+ {
196
+ return function () use ($ services ) {
197
+ $ config = $ services ->has ('config ' ) ? $ services ->get ('config ' ) : [];
116
198
$ config = isset ($ config ['view_manager ' ]) ? $ config ['view_manager ' ] : [];
117
199
$ doctypeHelper = new ViewHelper \Doctype ;
118
200
if (isset ($ config ['doctype ' ]) && $ config ['doctype ' ]) {
119
201
$ doctypeHelper ->setDoctype ($ config ['doctype ' ]);
120
202
}
121
203
return $ doctypeHelper ;
122
- });
123
-
124
- return $ plugins ;
204
+ };
125
205
}
126
206
}
0 commit comments