use zend router
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Mvc / Service / ViewHelperManagerFactory.php
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
4 *
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
8 */
9
10 namespace Zend\Mvc\Service;
11
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;
19
20 class ViewHelperManagerFactory extends AbstractPluginManagerFactory
21 {
22 const PLUGIN_MANAGER_CLASS = HelperPluginManager::class;
23
24 /**
25 * An array of helper configuration classes to ensure are on the helper_map stack.
26 *
27 * These are *not* imported; that way they can be optional dependencies.
28 *
29 * @todo Re-enable these once their components have been updated to zend-servicemanager v3
30 * @var array
31 */
32 protected $defaultHelperMapClasses = [
33 'Zend\Form\View\HelperConfig',
34 'Zend\I18n\View\HelperConfig',
35 'Zend\Navigation\View\HelperConfig',
36 ];
37
38 /**
39 * Create and return the view helper manager
40 *
41 * @param ContainerInterface $container
42 * @return HelperPluginManager
43 * @throws ServiceNotCreatedException
44 */
45 public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
46 {
47 $options = $options ?: [];
48 $options['factories'] = isset($options['factories']) ? $options['factories'] : [];
49 $plugins = parent::__invoke($container, $requestedName, $options);
50
51 // Configure default helpers from other components
52 $plugins = $this->configureHelpers($plugins);
53
54 // Override plugin factories
55 $plugins = $this->injectOverrideFactories($plugins, $container);
56
57 return $plugins;
58 }
59
60 /**
61 * Configure helpers from other components.
62 *
63 * Loops through the list of default helper configuration classes, and uses
64 * each to configure the helper plugin manager.
65 *
66 * @param HelperPluginManager $plugins
67 * @return HelperPluginManager
68 */
69 private function configureHelpers(HelperPluginManager $plugins)
70 {
71 foreach ($this->defaultHelperMapClasses as $configClass) {
72 if (! is_string($configClass) || ! class_exists($configClass)) {
73 continue;
74 }
75
76 $config = new $configClass();
77
78 if (! $config instanceof ConfigInterface) {
79 throw new ServiceNotCreatedException(sprintf(
80 'Invalid service manager configuration class provided; received "%s", expected class implementing %s',
81 $configClass,
82 ConfigInterface::class
83 ));
84 }
85
86 $config->configureServiceManager($plugins);
87 }
88
89 return $plugins;
90 }
91
92 /**
93 * Inject override factories into the plugin manager.
94 *
95 * @param HelperPluginManager $plugins
96 * @param ContainerInterface $services
97 * @return HelperPluginManager
98 */
99 private function injectOverrideFactories(HelperPluginManager $plugins, ContainerInterface $services)
100 {
101 // Configure URL view helper
102 $urlFactory = $this->createUrlHelperFactory($services);
103 $plugins->setFactory(ViewHelper\Url::class, $urlFactory);
104 $plugins->setFactory('zendviewhelperurl', $urlFactory);
105
106 // Configure base path helper
107 $basePathFactory = $this->createBasePathHelperFactory($services);
108 $plugins->setFactory(ViewHelper\BasePath::class, $basePathFactory);
109 $plugins->setFactory('zendviewhelperbasepath', $basePathFactory);
110
111 // Configure doctype view helper
112 $doctypeFactory = $this->createDoctypeHelperFactory($services);
113 $plugins->setFactory(ViewHelper\Doctype::class, $doctypeFactory);
114 $plugins->setFactory('zendviewhelperdoctype', $doctypeFactory);
115
116 return $plugins;
117 }
118
119 /**
120 * Create and return a factory for creating a URL helper.
121 *
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.
125 *
126 * @param ContainerInterface $services
127 * @return callable
128 */
129 private function createUrlHelperFactory(ContainerInterface $services)
130 {
131 return function () use ($services) {
132 $helper = new ViewHelper\Url;
133 $router = Console::isConsole() ? 'HttpRouter' : 'Router';
134 $helper->setRouter($services->get($router));
135
136 $match = $services->get('Application')
137 ->getMvcEvent()
138 ->getRouteMatch()
139 ;
140
141 if ($match instanceof RouteMatch) {
142 $helper->setRouteMatch($match);
143 }
144
145 return $helper;
146 };
147 }
148
149 /**
150 * Create and return a factory for creating a BasePath helper.
151 *
152 * Uses configuration and request services to configure the helper.
153 *
154 * @param ContainerInterface $services
155 * @return callable
156 */
157 private function createBasePathHelperFactory(ContainerInterface $services)
158 {
159 return function () use ($services) {
160 $config = $services->has('config') ? $services->get('config') : [];
161 $helper = new ViewHelper\BasePath;
162
163 if (Console::isConsole()
164 && isset($config['view_manager']['base_path_console'])
165 ) {
166 $helper->setBasePath($config['view_manager']['base_path_console']);
167 return $helper;
168 }
169
170 if (isset($config['view_manager']) && isset($config['view_manager']['base_path'])) {
171 $helper->setBasePath($config['view_manager']['base_path']);
172 return $helper;
173 }
174
175 $request = $services->get('Request');
176
177 if (is_callable([$request, 'getBasePath'])) {
178 $helper->setBasePath($request->getBasePath());
179 }
180
181 return $helper;
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') : [];
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']);
202 }
203 return $helper;
204 };
205 }
206 }