1 | <?php |
||
16 | class Module extends \samson\core\CompressableExternalModule |
||
|
|||
17 | { |
||
18 | const EVENT_ROUTE_FOUND = 'router.route.found'; |
||
19 | |||
20 | /** @var string Module identifier */ |
||
21 | public $id = 'router'; |
||
22 | |||
23 | /** @var string Default controller module identifier */ |
||
24 | public $defaultModule = 'main'; |
||
25 | |||
26 | /** @var bool Use automatic locale resolving with browser response headers */ |
||
27 | public $browserLocaleRedirect = false; |
||
28 | |||
29 | /** @var string Path to routing logic cache file */ |
||
30 | protected $cacheFile; |
||
31 | |||
32 | /** @var string Current URL path */ |
||
33 | protected $requestURI; |
||
34 | |||
35 | /** |
||
36 | * Old generic "main_page" route callback searcher to match old logic. |
||
37 | * |
||
38 | * @return Route Default application route "/" |
||
39 | */ |
||
40 | protected function findGenericDefaultAction() |
||
65 | |||
66 | /** |
||
67 | * Module initialization. |
||
68 | * |
||
69 | * @param array $params Initialization parameters collection |
||
70 | * @return bool Initialization result |
||
71 | */ |
||
72 | public function init(array $params = array()) |
||
73 | { |
||
74 | //[PHPCOMPRESSOR(remove,start)] |
||
75 | // Create SamsonPHP routing table from loaded modules |
||
76 | $rg = new GenericRouteGenerator($this->system->module_stack); |
||
77 | |||
78 | // Generate web-application routes |
||
79 | $routes = $rg->generate(); |
||
80 | $routes->add($this->findGenericDefaultAction()); |
||
81 | |||
82 | // Create cache marker |
||
83 | $this->cacheFile = $routes->hash().'.php'; |
||
84 | // If we need to refresh cache |
||
85 | if ($this->cache_refresh($this->cacheFile)) { |
||
86 | $generator = new Structure($routes, new \samsonphp\generator\Generator()); |
||
87 | // Generate routing logic function |
||
88 | $routerLogic = $generator->generate(); |
||
89 | |||
90 | // Store router logic in cache |
||
91 | file_put_contents($this->cacheFile, '<?php '."\n".$routerLogic); |
||
92 | } |
||
93 | |||
94 | require($this->cacheFile); |
||
95 | //[PHPCOMPRESSOR(remove,end)] |
||
96 | |||
97 | // Set locale resolver mode |
||
98 | SamsonLocale::$leaveDefaultLocale = $this->browserLocaleRedirect; |
||
99 | |||
100 | // This should be change to receive path as a parameter on initialization |
||
101 | $pathParts = array_values(array_filter(explode(Route::DELIMITER, $_SERVER['REQUEST_URI']))); |
||
102 | // Parse URL and store locale found bug |
||
103 | $localeFound = SamsonLocale::parseURL($pathParts, $this->browserLocaleRedirect); |
||
104 | // Gather URL path parts with removed locale placeholder |
||
105 | $this->requestURI = implode(Route::DELIMITER, $pathParts); |
||
106 | |||
107 | // Get localization data |
||
108 | $current = SamsonLocale::current(); |
||
109 | $default = SamsonLocale::$defaultLocale; |
||
110 | |||
111 | // Browser agent language detection logic |
||
112 | if ($this->browserLocaleRedirect && !$localeFound) { |
||
113 | // Redirect to browser language |
||
114 | $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); |
||
115 | // Is browser language supported by application |
||
116 | $langSupport = in_array($lang, SamsonLocale::get(), true); |
||
117 | |||
118 | /** |
||
119 | * If browser language header is supported by our web-application and we are already not on that locale |
||
120 | * and current locale is not default. |
||
121 | */ |
||
122 | if ($current === $default && $current !== $lang && $langSupport) { |
||
123 | header('Location: http://'.$_SERVER['HTTP_HOST'].'/'.$lang.'/'.$this->requestURI);exit; |
||
124 | } elseif (!$langSupport || $lang === $current) { |
||
125 | SamsonLocale::$leaveDefaultLocale = false; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | // Subscribe to samsonphp\core routing event |
||
130 | \samsonphp\event\Event::subscribe('core.routing', array($this, 'router')); |
||
131 | |||
132 | // Continue initialization |
||
133 | return parent::init($params); |
||
134 | } |
||
135 | |||
136 | /** @see \samson\core\CompressableExternalModule::afterCompress() */ |
||
137 | public function afterCompress(&$obj = null, array &$code = null) |
||
142 | |||
143 | /** |
||
144 | * Define if HTTP request is asynchronous. |
||
145 | * |
||
146 | * @return bool True if request is asynchronous |
||
147 | */ |
||
148 | public function isAsynchronousRequest() |
||
152 | |||
153 | /** |
||
154 | * Parse route parameters received from router logic function. |
||
155 | * |
||
156 | * @param callable $callback Route instance |
||
157 | * @param array $receivedParameters Collection of parsed parameters |
||
158 | * @return array Collection of route callback needed parameters |
||
159 | */ |
||
160 | protected function parseParameters($callback, array $receivedParameters) |
||
184 | |||
185 | /** |
||
186 | * SamsonPHP core.routing event handler |
||
187 | * |
||
188 | * @param SystemInterface $core Pointer to core object |
||
189 | * @param mixed $result Return value as routing result |
||
190 | * @return bool Routing result |
||
191 | */ |
||
192 | public function router(SystemInterface &$core, &$result) |
||
267 | } |
||
268 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.