@@ -47,7 +47,29 @@ class ViewHelperManagerFactory extends AbstractPluginManagerFactory
47
47
public function __invoke (ContainerInterface $ container , $ requestedName , array $ options = null )
48
48
{
49
49
$ options = $ options ?: [];
50
+ $ options ['factories ' ] = isset ($ options ['factories ' ]) ? $ options ['factories ' ] : [];
51
+ $ plugins = parent ::__invoke ($ container , $ requestedName , $ options );
52
+
53
+ // Configure default helpers from other components
54
+ $ plugins = $ this ->configureHelpers ($ plugins );
50
55
56
+ // Override plugin factories
57
+ $ plugins = $ this ->injectOverrideFactories ($ plugins , $ serviceLocator );
58
+
59
+ return $ plugins ;
60
+ }
61
+
62
+ /**
63
+ * Configure helpers from other components.
64
+ *
65
+ * Loops through the list of default helper configuration classes, and uses
66
+ * each to configure the helper plugin manager.
67
+ *
68
+ * @param HelperPluginManager $plugins
69
+ * @return HelperPluginManager
70
+ */
71
+ private function configureHelpers (HelperPluginManager $ plugins )
72
+ {
51
73
foreach ($ this ->defaultHelperMapClasses as $ configClass ) {
52
74
if (! is_string ($ configClass ) || ! class_exists ($ configClass )) {
53
75
continue ;
@@ -66,35 +88,55 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
66
88
$ options = ArrayUtils::merge ($ options , $ config ->toArray ());
67
89
}
68
90
69
- $ config = $ container ->has ('config ' ) ? $ container ->get ('config ' ) : [];
70
-
71
- $ options ['factories ' ] = isset ($ options ['factories ' ]) ? $ options ['factories ' ] : [];
72
-
73
- // Configure URL view helper factory
74
- $ options ['factories ' ][ViewHelper \Url::class] = $ this ->createUrlHelperFactory ();
75
-
76
- // Configure basepath view helper factory
77
- $ options ['factories ' ][ViewHelper \BasePath::class] = $ this ->createBasePathHelperFactory ($ config );
78
-
79
- // Configure doctype view helper factory
80
- $ options ['factories ' ][ViewHelper \Doctype::class] = $ this ->createDoctypeHelperFactory ();
91
+ $ plugins ->configure ($ options );
92
+ return $ plugins ;
93
+ }
81
94
82
- return parent ::__invoke ($ container , $ requestedName , $ options );
95
+ /**
96
+ * Inject override factories into the plugin manager.
97
+ *
98
+ * @param HelperPluginManager $plugins
99
+ * @param ContainerInterface $services
100
+ * @return HelperPluginManager
101
+ */
102
+ private function injectOverrideFactories (HelperPluginManager $ plugins , ContainerInterface $ services )
103
+ {
104
+ // Configure URL view helper
105
+ $ urlFactory = $ this ->createUrlHelperFactory ($ services );
106
+ $ plugins ->setFactory (ViewHelper \Url::class, $ urlFactory );
107
+ $ plugins ->setFactory ('zendviewhelperurl ' , $ urlFactory );
108
+
109
+ // Configure base path helper
110
+ $ basePathFactory = $ this ->createBasePathHelperFactory ($ services );
111
+ $ plugins ->setFactory (ViewHelper \BasePath::class, $ basePathFactory );
112
+ $ plugins ->setFactory ('zendviewhelperbasepath ' , $ basePathFactory );
113
+
114
+ // Configure doctype view helper
115
+ $ doctypeFactory = $ this ->createDoctypeHelperFactory ($ services );
116
+ $ plugins ->setFactory (ViewHelper \doctype::class, $ doctypeFactory );
117
+ $ plugins ->setFactory ('zendviewhelperdoctype ' , $ doctypeFactory );
118
+
119
+ return $ plugins ;
83
120
}
84
121
85
122
/**
86
- * Create a factory for the "url" view helper
123
+ * Create and return a factory for creating a URL helper.
124
+ *
125
+ * Retrieves the application and router from the servicemanager,
126
+ * and the route match from the MvcEvent composed by the application,
127
+ * using them to configure the helper.
87
128
*
129
+ * @param ContainerInterface $services
88
130
* @return callable
89
131
*/
90
- private function createUrlHelperFactory ()
132
+ private function createUrlHelperFactory (ContainerInterface $ services )
91
133
{
92
- return function ($ container , $ name , array $ options = null ) {
134
+ return function () use ( $ services ) {
93
135
$ helper = new ViewHelper \Url ;
94
136
$ router = Console::isConsole () ? 'HttpRouter ' : 'Router ' ;
95
- $ helper ->setRouter ($ container ->get ($ router ));
137
+ $ helper ->setRouter ($ services ->get ($ router ));
96
138
97
- $ match = $ container ->get ('application ' )
139
+ $ match = $ services ->get ('application ' )
98
140
->getMvcEvent ()
99
141
->getRouteMatch ()
100
142
;
@@ -107,16 +149,17 @@ private function createUrlHelperFactory()
107
149
};
108
150
}
109
151
110
- /**
111
- * Create a factory for the "basepath" view helper
152
+ * Create and return a factory for creating a BasePath helper.
112
153
*
113
- * @param array $config
154
+ * Uses configuration and request services to configure the helper.
155
+ *
156
+ * @param ContainerInterface $ services
114
157
* @return callable
115
158
*/
116
- private function createBasePathHelperFactory ($ config )
159
+ private function createBasePathHelperFactory (ContainerInterface $ services )
117
160
{
118
- return function ($ container , $ name , array $ options = null ) {
119
- $ config = $ container ->has ('config ' ) ? $ container ->get ('config ' ) : [];
161
+ return function () use ( $ services ) {
162
+ $ config = $ services ->has ('config ' ) ? $ services ->get ('config ' ) : [];
120
163
$ helper = new ViewHelper \BasePath ;
121
164
122
165
if (Console::isConsole ()
@@ -131,7 +174,8 @@ private function createBasePathHelperFactory($config)
131
174
return $ helper ;
132
175
}
133
176
134
- $ request = $ container ->get ('Request ' );
177
+ $ request = $ services ->get ('Request ' );
178
+
135
179
if (is_callable ([$ request , 'getBasePath ' ])) {
136
180
$ helper ->setBasePath ($ request ->getBasePath ());
137
181
}
@@ -141,19 +185,18 @@ private function createBasePathHelperFactory($config)
141
185
}
142
186
143
187
/**
144
- * Configure doctype view helper with doctype from configuration, if available .
188
+ * Create and return a Doctype helper factory .
145
189
*
146
190
* Other view helpers depend on this to decide which spec to generate their tags
147
- * based on.
148
- *
149
- * This is why it must be set early instead of later in the layout phtml.
191
+ * based on. This is why it must be set early instead of later in the layout phtml.
150
192
*
193
+ * @param ContainerInterface $services
151
194
* @return callable
152
195
*/
153
- private function createDoctypeHelperFactory ()
196
+ private function createDoctypeHelperFactory (ContainerInterface $ services )
154
197
{
155
- return function ($ container , $ name , array $ options = null ) {
156
- $ config = $ container ->has ('config ' ) ? $ container ->get ('config ' ) : [];
198
+ return function () use ( $ services ) {
199
+ $ config = $ services ->has ('config ' ) ? $ services ->get ('config ' ) : [];
157
200
$ config = isset ($ config ['view_manager ' ]) ? $ config ['view_manager ' ] : [];
158
201
$ helper = new ViewHelper \Doctype ;
159
202
if (isset ($ config ['doctype ' ]) && $ config ['doctype ' ]) {
0 commit comments