3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Service
;
12 use Interop\Container\ContainerInterface
;
13 use Zend\Console\Console
;
14 use Zend\Mvc\Router\RouteMatch
;
15 use Zend\ServiceManager\ConfigInterface
;
16 use Zend\ServiceManager\Exception\ServiceNotCreatedException
;
17 use Zend\View\Helper
as ViewHelper
;
18 use Zend\View\HelperPluginManager
;
20 class ViewHelperManagerFactory
extends AbstractPluginManagerFactory
22 const PLUGIN_MANAGER_CLASS
= HelperPluginManager
::class;
25 * An array of helper configuration classes to ensure are on the helper_map stack.
27 * These are *not* imported; that way they can be optional dependencies.
29 * @todo Re-enable these once their components have been updated to zend-servicemanager v3
32 protected $defaultHelperMapClasses = [
33 'Zend\Form\View\HelperConfig',
34 'Zend\I18n\View\HelperConfig',
35 'Zend\Navigation\View\HelperConfig',
39 * Create and return the view helper manager
41 * @param ContainerInterface $container
42 * @return HelperPluginManager
43 * @throws ServiceNotCreatedException
45 public function __invoke(ContainerInterface
$container, $requestedName, array $options = null)
47 $options = $options ?
: [];
48 $options['factories'] = isset($options['factories']) ?
$options['factories'] : [];
49 $plugins = parent
::__invoke($container, $requestedName, $options);
51 // Configure default helpers from other components
52 $plugins = $this->configureHelpers($plugins);
54 // Override plugin factories
55 $plugins = $this->injectOverrideFactories($plugins, $container);
61 * Configure helpers from other components.
63 * Loops through the list of default helper configuration classes, and uses
64 * each to configure the helper plugin manager.
66 * @param HelperPluginManager $plugins
67 * @return HelperPluginManager
69 private function configureHelpers(HelperPluginManager
$plugins)
71 foreach ($this->defaultHelperMapClasses
as $configClass) {
72 if (! is_string($configClass) ||
! class_exists($configClass)) {
76 $config = new $configClass();
78 if (! $config instanceof ConfigInterface
) {
79 throw new ServiceNotCreatedException(sprintf(
80 'Invalid service manager configuration class provided; received "%s", expected class implementing %s',
82 ConfigInterface
::class
86 $config->configureServiceManager($plugins);
93 * Inject override factories into the plugin manager.
95 * @param HelperPluginManager $plugins
96 * @param ContainerInterface $services
97 * @return HelperPluginManager
99 private function injectOverrideFactories(HelperPluginManager
$plugins, ContainerInterface
$services)
101 // Configure URL view helper
102 $urlFactory = $this->createUrlHelperFactory($services);
103 $plugins->setFactory(ViewHelper\Url
::class, $urlFactory);
104 $plugins->setFactory('zendviewhelperurl', $urlFactory);
106 // Configure base path helper
107 $basePathFactory = $this->createBasePathHelperFactory($services);
108 $plugins->setFactory(ViewHelper\BasePath
::class, $basePathFactory);
109 $plugins->setFactory('zendviewhelperbasepath', $basePathFactory);
111 // Configure doctype view helper
112 $doctypeFactory = $this->createDoctypeHelperFactory($services);
113 $plugins->setFactory(ViewHelper\Doctype
::class, $doctypeFactory);
114 $plugins->setFactory('zendviewhelperdoctype', $doctypeFactory);
120 * Create and return a factory for creating a URL helper.
122 * Retrieves the application and router from the servicemanager,
123 * and the route match from the MvcEvent composed by the application,
124 * using them to configure the helper.
126 * @param ContainerInterface $services
129 private function createUrlHelperFactory(ContainerInterface
$services)
131 return function () use ($services) {
132 $helper = new ViewHelper\Url
;
133 $router = Console
::isConsole() ?
'HttpRouter' : 'Router';
134 $helper->setRouter($services->get($router));
136 $match = $services->get('Application')
141 if ($match instanceof RouteMatch
) {
142 $helper->setRouteMatch($match);
150 * Create and return a factory for creating a BasePath helper.
152 * Uses configuration and request services to configure the helper.
154 * @param ContainerInterface $services
157 private function createBasePathHelperFactory(ContainerInterface
$services)
159 return function () use ($services) {
160 $config = $services->has('config') ?
$services->get('config') : [];
161 $helper = new ViewHelper\BasePath
;
163 if (Console
::isConsole()
164 && isset($config['view_manager']['base_path_console'])
166 $helper->setBasePath($config['view_manager']['base_path_console']);
170 if (isset($config['view_manager']) && isset($config['view_manager']['base_path'])) {
171 $helper->setBasePath($config['view_manager']['base_path']);
175 $request = $services->get('Request');
177 if (is_callable([$request, 'getBasePath'])) {
178 $helper->setBasePath($request->getBasePath());
186 * Create and return a Doctype helper factory.
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.
191 * @param ContainerInterface $services
194 private function createDoctypeHelperFactory(ContainerInterface
$services)
196 return function () use ($services) {
197 $config = $services->has('config') ?
$services->get('config') : [];
198 $config = isset($config['view_manager']) ?
$config['view_manager'] : [];
199 $helper = new ViewHelper\Doctype
;
200 if (isset($config['doctype']) && $config['doctype']) {
201 $helper->setDoctype($config['doctype']);