Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
router.php
1<?php
9namespace Bitrix\Main\Routing;
10
13
18class Router
19{
21 protected $routes = [];
22
24 protected $routesByName = [];
25
27 protected $configurations = [];
28
29 public function registerConfiguration($configuration)
30 {
31 $this->configurations[] = $configuration;
32 }
33
34 public function releaseRoutes()
35 {
36 // go recursively through routes tree
37 $i = -1;
38 while (isset($this->configurations[++$i]))
39 {
40 $this->routes = array_merge($this->routes, $this->configurations[$i]->release());
41 }
42
43 // reindex
44 $this->reindexRoutes();
45
46 // don't need them anymore
47 $this->configurations = [];
48 }
49
50 protected function reindexRoutes()
51 {
52 $this->routesByName = [];
53
54 foreach ($this->routes as $route)
55 {
56 if ($route->getOptions() && $route->getOptions()->hasName())
57 {
58 $this->routesByName[$route->getOptions()->getFullName()] = $route;
59 }
60 }
61 }
62
68 public function match($request)
69 {
70 $path = urldecode($this->getUriPath($request));
71
72 foreach ($this->routes as $route)
73 {
74 if ($matchResult = $route->match($path))
75 {
76 // check method
77 if (!empty($route->getOptions()->getMethods())
78 && !in_array($request->getRequestMethod(), $route->getOptions()->getMethods(), true))
79 {
80 continue;
81 }
82
83 if (is_array($matchResult))
84 {
85 $route->getParametersValues()->setValues($matchResult);
86 }
87
88 return $route;
89 }
90 }
91 }
92
98 protected function getUriPath($request)
99 {
100 // cut GET parameters
101 $path = str_replace(
102 '?'.$request->getServer()->get('QUERY_STRING'),
103 '',
104 $request->getRequestUri()
105 );
106
107 // cut scheme and domain
108 $scheme = $request->isHttps() ? 'https://' : 'http://';
109 $schemeLen = strlen($scheme);
110
111 if (substr($path, 0, $schemeLen) === $scheme)
112 {
113 $pathSlashPos = strpos(substr($path, $schemeLen), '/') + $schemeLen;
114 $path = substr($path, $pathSlashPos);
115 }
116
117 return $path;
118 }
119
120 public function url($url, $parameters = [])
121 {
122 // scheme, domain?
123 $finalUrl = $url;
124
125 if (!empty($parameters))
126 {
127 $finalUrl .= '?'.http_build_query($parameters);
128 }
129
130 return $finalUrl;
131 }
132
133 public function route($name, $parameters = [])
134 {
135 if (!empty($this->routesByName[$name]))
136 {
137 // route should be compiled
138 $route = $this->routesByName[$name];
139 $route->compile();
140
141 $uri = $route->getUri();
142
143 if (!empty($routeParameters = $route->getParameters()))
144 {
145 foreach ($routeParameters as $parameterName => $pattern)
146 {
147 if (array_key_exists($parameterName, $parameters))
148 {
149 // get from user
150 $value = $parameters[$parameterName];
151
152 // remove from user list
153 unset($parameters[$parameterName]);
154 }
155 elseif ($route->getOptions() && $route->getOptions()->hasDefault($parameterName))
156 {
157 $value = $route->getOptions()->getDefault($parameterName);
158 }
159 else
160 {
162 }
163
164 // check with pattern?
165
166 $uri = str_replace("{{$parameterName}}", urlencode($value), $uri);
167 }
168 }
169
170 // additional parameters as query string
171 if (!empty($parameters))
172 {
173 $uri .= '?'.http_build_query($parameters);
174 }
175
176 return $uri;
177 }
178 }
179
183 public function getRoutes()
184 {
185 return $this->routes;
186 }
187}
registerConfiguration($configuration)
Definition router.php:29
url($url, $parameters=[])
Definition router.php:120
route($name, $parameters=[])
Definition router.php:133