-
Notifications
You must be signed in to change notification settings - Fork 1
/
Module.php
318 lines (274 loc) · 11 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
* Libra-CMS (http://www.ejoom.com/libra-cms/)
*/
namespace LibraLocale;
use Locale;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\ModuleEvent;
use Zend\ModuleManager\ModuleManager;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module implements AutoloaderProviderInterface, ConfigProviderInterface
{
// Disable locale for route
const DISABLE_LOCALE = 'disable_locale';
// Parameter name that will be set into RouteMatchs
const LOCALE_ROUTE_PARAM = 'locale';
/**
* Runtime locale alias
* @var string
*/
public static $alias;
/**
* Runtime locale tag
* @var string
*/
public static $locale;
protected static $options;
public function init(ModuleManager $moduleManager)
{
$moduleManager->getEventManager()->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, array($this, 'setOptions'), 1010);
$moduleManager->getEventManager()->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, array($this, 'addLocaleToRoutes'), 1000);
// a little hacky but it avoid rewriting of many codes
$moduleManager->getEventManager()->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, array($this, 'overrideViewHelperUrl'));
//cann't do it in the ViewHelperConfig so we do it by factory
$moduleManager->getEventManager()->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, array($this, 'addViewHelperLocale'));
}
// override url view helper with new locale aware
public function overrideViewHelperUrl(ModuleEvent $e)
{
$serviceLocator = $e->getParam('ServiceManager');
$viewHelperManager = $serviceLocator->get('ViewHelperManager');
// Configure URL view helper with router
$viewHelperManager->setFactory('url', function ($sm) use ($serviceLocator) {
$helper = new View\Helper\Url;
$router = \Zend\Console\Console::isConsole() ? 'HttpRouter' : 'Router';
$helper->setRouter($serviceLocator->get($router));
$match = $serviceLocator->get('application')
->getMvcEvent()
->getRouteMatch()
;
if ($match instanceof \Zend\Mvc\Router\RouteMatch) {
$helper->setRouteMatch($match);
}
return $helper;
});
}
// override url view helper with new locale aware
public function addViewHelperLocale(ModuleEvent $e)
{
$serviceLocator = $e->getParam('ServiceManager');
$viewHelperManager = $serviceLocator->get('ViewHelperManager');
// Configure URL view helper with router
$viewHelperManager->setFactory('locale', function ($sm) use ($serviceLocator) {
$helper = new View\Helper\Locale();
$match = $serviceLocator->get('application')
->getMvcEvent()
->getRouteMatch()
;
if ($match instanceof \Zend\Mvc\Router\RouteMatch) {
$helper->setRouteMatch($match);
}
return $helper;
});
}
public function addLocaleToRoutes(ModuleEvent $e)
{
$config = $e->getConfigListener()->getMergedConfig(false);
$routes = &$config['router']['routes'];
if (static::getOption('redirect_from_locale_tag')) {
$availableValueArray = array_unique(
array_merge(
array_keys(static::getLocales()),
array_values(static::getLocales())
)
);
} else {
$availableValueArray = array_keys(static::getLocales());
}
$localeConstraint = '(' . implode($availableValueArray, '|') . ')';
//$localeConstraint = '[a-zA-Z][a-zA-Z0-9_-]*';
foreach ($routes as $key => &$route) {
if ($key === 'admin') continue;
if (isset($route['options'][self::DISABLE_LOCALE])
&& $route['options'][self::DISABLE_LOCALE] !== false
) {
continue;
}
if (strtolower($route['type']) === 'literal' || $route['type'] === 'Zend\Mvc\Router\Http\Literal') {
$route['type'] = 'Segment';
}
if (strtolower($route['type']) === 'segment' || $route['type'] === 'Zend\Mvc\Router\Http\Segment') {
if (strpos($route['options']['route'], ':locale') === false) {
$route['options']['route'] = '[/:locale]' . $route['options']['route'];
}
if (!isset($route['options']['constraints'])) $route['options']['constraints'] = array();
$route['options']['constraints'] = array_merge(
$route['options']['constraints'],
array('locale' => $localeConstraint)
);
} elseif (strtolower($route['type']) === 'regex' || $route['type'] === 'Zend\Mvc\Router\Http\Regex') {
if (strpos($route['options']['route'], '<locale>') === false) {
$route['options']['route'] = '(/(?P<locale>'.$localeConstraint.'))?' . $route['options']['route'];
$route['options']['spec'] = '/locale' . $route['options']['spec']; //@todo needs to make optional on assemling
}
} else {
//throw thomesing
}
if (!isset($route['options']['defaults'])) $route['options']['defaults'] = array();
$route['options']['defaults'] = array_merge(
$route['options']['defaults'],
array('locale' => static::getDefaultLocaleAlias())
);
}
$e->getConfigListener()->setMergedConfig($config);
}
protected function _redirect(MvcEvent $e, $localeAlias = null, $routeName = null)
{
$routeMatch = $e->getRouteMatch();
$statusCode = static::getOption('redirect_code');
if ($localeAlias !== null) {
$routeMatch->setParam(self::LOCALE_ROUTE_PARAM, $localeAlias);
}
$router = $e->getRouter();
if ($routeName === null) {
$routeName = $routeMatch->getMatchedRouteName();
}
$routeMatchParams = $routeMatch->getParams();
if (isset($routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER])) {
$routeMatchParams['controller'] = $routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER];
unset($routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER]);
}
if (isset($routeMatchParams[ModuleRouteListener::MODULE_NAMESPACE])) {
unset($routeMatchParams[ModuleRouteListener::MODULE_NAMESPACE]);
}
$url = $router->assemble($routeMatchParams, array('name' => $routeName));
$response = $e->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(isset($statusCode) ? $statusCode : 302);
return $response;
}
public function redirectFromLocaleTag(MvcEvent $e)
{
$routeMatch = $e->getRouteMatch();
$localeAlias = $routeMatch->getParam(self::LOCALE_ROUTE_PARAM);
if ($localeAlias === null) return 0; //do nothing
// redirects to the closest alias if you typed a locale tag
if (!key_exists($localeAlias, static::getLocales())) {
$locale = $localeAlias;
//$newLocale = Locale::lookup(static::getLocales(), $locale, false, static::getDefaultLocaleAlias());
$newAlias = array_search($locale, static::getLocales());
//$newAlias = static::getAliasByLocale($newLocale);
if ($newAlias !== $localeAlias) { // avoiding infinite redirect
return $this->_redirect($e, $newAlias);
}
}
return 0; //return success
}
/**
* set locale system-wide
* @param \Zend\Mvc\MvcEvent $e
*/
public function setDefaultLocale(MvcEvent $e)
{
static::$alias = $e->getRouteMatch()->getParam(self::LOCALE_ROUTE_PARAM);
static::$locale = static::getLocaleByAlias(static::$alias);
if (Locale::setDefault(static::$locale) === false) {
throw new \RuntimeException(sprintf(
'Not valid locale %s to set it as default',
static::$locale
));
}
// Set translator
$translator = $e->getApplication()->getServiceManager()->get('MvcTranslator');
$parsedLocale = \Locale::parseLocale(static::$locale);
$languageAndRegion = $parsedLocale['language'] . '-' . $parsedLocale['region'];
$translator->setLocale($languageAndRegion);
}
/**
* executes on boostrap
* @param \Zend\Mvc\MvcEvent $e
* @return null
*/
public function onBootstrap(MvcEvent $e)
{
if (static::getOption('redirect_from_locale_tag')) {
$e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_ROUTE, array($this, 'redirectFromLocaleTag'));
}
$e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_ROUTE, array($this, 'setDefaultLocale'));
return;
}
public function setOptions(ModuleEvent $e)
{
$config = $e->getConfigListener()->getMergedConfig(false);
static::$options = $config['libra_locale'];
}
public static function getOption($option)
{
if (!isset(static::$options[$option])) {
return null;
}
return static::$options[$option];
}
/**
* Get list of of available and allowed aliases and locales in format
* 'alias' => 'locale tag',
* @return array
*/
public static function getLocales()
{
return static::getOption('locales');
}
/**
* Return locale tag by alias.
* If alias doesn't exist will return default locale
*
* @param string $alias
* @return sting
*/
public static function getLocaleByAlias($alias)
{
$locales = static::getLocales();
if (!array_key_exists($alias, $locales)) {
return false;
//$alias = static::getDefaultLocaleAlias();
}
return $locales[$alias];
}
public static function getAliasByLocale($locale)
{
$key = array_search($locale, static::getLocales());
return is_string($key) ? $key : $locale;
}
public static function getDefaultLocaleAlias()
{
$aliasOrTag = static::getOption('default');
// explicit take locale alias
$alias = static::getAliasByLocale($aliasOrTag);
return $alias;
}
public function getControllerPluginConfig()
{
return array(
'invokables' => array(
'url' => 'LibraLocale\Mvc\Controller\Plugin\Url',
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}