Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
urlmanager.php
1<?php
2
3namespace Bitrix\Main\Engine;
4
5
9
10final class UrlManager
11{
12 const AJAX_END_POINT = '/bitrix/services/main/ajax.php';
13
14 const ABSOLUTE_URL = true;
15
17 private static $instance;
18
19 private function __clone()
20 {}
21
22 private function __construct()
23 {}
24
28 public static function getInstance()
29 {
30 if (!isset(self::$instance))
31 {
32 self::$instance = new static;
33 }
34
35 return self::$instance;
36 }
37
50 public function create($action, $params = [], $absolute = false)
51 {
52 $uri = $this->getEndPoint($absolute);
53 $uri->addParams([
54 'action' => $action,
55 ]);
56
57 if (defined('SITE_ID') && !Context::getCurrent()->getRequest()->isAdminSection())
58 {
59 $uri->addParams([
60 'SITE_ID' => SITE_ID,
61 ]);
62 }
63
64 $uri->addParams($params);
65
66 return $uri;
67 }
68
82 public function createByController(Controller $controller, $action, $params = array(), $absolute = false)
83 {
84 $name = Resolver::getNameByController($controller);
85 if (!$controller->isLocatedUnderPsr4())
86 {
87 $name = mb_strtolower($name);
88 }
89
90 list($vendor) = $this->getVendorAndModule($controller->getModuleId());
91 if ($vendor === 'bitrix')
92 {
93 $name = mb_substr($name, mb_strlen('bitrix:'));
94 }
95
96 return $this->create(
97 $name . '.' . $action,
98 $params,
99 $absolute
100 );
101 }
102
103 protected function getVendorAndModule($moduleId)
104 {
105 $parts = explode('.', $moduleId);
106 if (!isset($parts[1]))
107 {
108 return ['bitrix', $moduleId];
109 }
110
111 if ($parts[0] === 'bitrix')
112 {
113 return ['bitrix', $moduleId];
114 }
115
116 return [$parts[0], $moduleId];
117 }
118
133 public function createByComponentController(Controller $controller, $action, $params = array(), $absolute = false)
134 {
135 $reflector = new \ReflectionClass($controller);
136 $path = dirname($reflector->getFileName());
137 if (DIRECTORY_SEPARATOR === '\\')
138 {
139 $path = str_replace('\\', '/', $path);
140 }
141 $pathWithoutLocal = mb_substr($path, mb_strpos($path, '/components/') + mb_strlen('/components/'));
142 list($vendor, $componentName) = explode('/', $pathWithoutLocal);
143
144 if (!$componentName)
145 {
146 $componentName = $vendor;
147 }
148 else
149 {
150 $componentName = "{$vendor}:{$componentName}";
151 }
152
153 $params['c'] = $componentName;
154 $params['mode'] = Router::COMPONENT_MODE_AJAX;
155
156 return $this->create(
157 $action,
158 $params,
159 $absolute
160 );
161 }
162
177 public function createByBitrixComponent(\CBitrixComponent $component, $action, $params = array(), $absolute = false)
178 {
179 $params['c'] = $component->getName();
180 $params['mode'] = Router::COMPONENT_MODE_CLASS;
181
182 return $this->create(
183 $action,
184 $params,
185 $absolute
186 );
187 }
188
198 public function getEndPoint($absolute = false)
199 {
200 $endPoint = self::AJAX_END_POINT;
201 if ($absolute === self::ABSOLUTE_URL)
202 {
203 $endPoint = $this->getHostUrl() . $endPoint;
204 }
205
206 return new Uri($endPoint);
207 }
208
214 public function getHostUrl(): string
215 {
216 $request = Context::getCurrent()->getRequest();
217
218 $protocol = $request->isHttps() ? 'https' : 'http';
219 $port = (int)$request->getServerPort();
220
221 if (defined('SITE_SERVER_NAME') && SITE_SERVER_NAME)
222 {
223 $host = SITE_SERVER_NAME;
224 }
225 else
226 {
227 $host = Option::get('main', 'server_name', $request->getHttpHost()) ? : $request->getHttpHost();
228 }
229
230 $portSuffix = '';
231 if ($port && !in_array($port, [443, 80], true))
232 {
233 $portSuffix = ':' . $port;
234 }
235 $parsedUri = new Uri($protocol . '://' . $host . $portSuffix);
236
237 return rtrim($parsedUri->getLocator(), '/');
238 }
239}
static getCurrent()
Definition context.php:241
static getNameByController(Controller $controller)
Definition resolver.php:234
createByBitrixComponent(\CBitrixComponent $component, $action, $params=array(), $absolute=false)
createByComponentController(Controller $controller, $action, $params=array(), $absolute=false)
createByController(Controller $controller, $action, $params=array(), $absolute=false)
create($action, $params=[], $absolute=false)