99
1010namespace Zend \Mvc \Service ;
1111
12+ use Interop \Container \ContainerInterface ;
1213use Zend \Console \Console ;
1314use Zend \Mvc \Exception ;
1415use Zend \Mvc \Router \RouteMatch ;
1516use Zend \ServiceManager \ConfigInterface ;
1617use Zend \ServiceManager \ServiceLocatorInterface ;
1718use Zend \View \Helper as ViewHelper ;
19+ use Zend \View \HelperPluginManager ;
1820use Zend \View \Helper \HelperInterface as ViewHelperInterface ;
1921
2022class ViewHelperManagerFactory extends AbstractPluginManagerFactory
2123{
22- const PLUGIN_MANAGER_CLASS = ' Zend\View\ HelperPluginManager' ;
24+ const PLUGIN_MANAGER_CLASS = HelperPluginManager::class ;
2325
2426 /**
2527 * An array of helper configuration classes to ensure are on the helper_map stack.
@@ -43,29 +45,92 @@ public function createService(ServiceLocatorInterface $serviceLocator)
4345 {
4446 $ plugins = parent ::createService ($ serviceLocator );
4547
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+ {
4668 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+ ) );
5981 }
82+
83+ $ config ->configureServiceManager ($ plugins );
6084 }
6185
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 ) {
64129 $ helper = new ViewHelper \Url ;
65130 $ router = Console::isConsole () ? 'HttpRouter ' : 'Router ' ;
66- $ helper ->setRouter ($ serviceLocator ->get ($ router ));
131+ $ helper ->setRouter ($ services ->get ($ router ));
67132
68- $ match = $ serviceLocator ->get ('application ' )
133+ $ match = $ services ->get ('application ' )
69134 ->getMvcEvent ()
70135 ->getRouteMatch ()
71136 ;
@@ -75,10 +140,21 @@ public function createService(ServiceLocatorInterface $serviceLocator)
75140 }
76141
77142 return $ helper ;
78- });
143+ };
144+ }
79145
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 ' ) : [];
82158 $ basePathHelper = new ViewHelper \BasePath ;
83159
84160 if (Console::isConsole ()
@@ -96,31 +172,35 @@ public function createService(ServiceLocatorInterface $serviceLocator)
96172 return $ basePathHelper ;
97173 }
98174
99- $ request = $ serviceLocator ->get ('Request ' );
175+ $ request = $ services ->get ('Request ' );
100176
101177 if (is_callable ([$ request , 'getBasePath ' ])) {
102178 $ basePathHelper ->setBasePath ($ request ->getBasePath ());
103179 }
104180
105181 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 ' ) : [];
116198 $ config = isset ($ config ['view_manager ' ]) ? $ config ['view_manager ' ] : [];
117199 $ doctypeHelper = new ViewHelper \Doctype ;
118200 if (isset ($ config ['doctype ' ]) && $ config ['doctype ' ]) {
119201 $ doctypeHelper ->setDoctype ($ config ['doctype ' ]);
120202 }
121203 return $ doctypeHelper ;
122- });
123-
124- return $ plugins ;
204+ };
125205 }
126206}
0 commit comments