1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
router.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\Engine;
4
5
6use Bitrix\Main\Component\ParameterSigner;
7use Bitrix\Main\Config\Configuration;
8use Bitrix\Main\Engine\Component\ComponentController;
9use Bitrix\Main\Engine\Contract\Controllerable;
10use Bitrix\Main\HttpRequest;
11use Bitrix\Main\Loader;
12use Bitrix\Main\ModuleManager;
13use Bitrix\Main\Security;
14use Bitrix\Main\SystemException;
15
16final class Router
17{
18 public const COMPONENT_MODE_AJAX = 'ajax';
19 public const COMPONENT_MODE_CLASS = 'class';
20
22 public const EXCEPTION_INVALID_COMPONENT = 2210202;
23 public const EXCEPTION_INVALID_AJAX_MODE = 2210203;
24 public const EXCEPTION_NO_CONFIGURATION = 2210204;
25 public const EXCEPTION_NO_MODULE = 2210205;
26 public const EXCEPTION_INVALID_MODULE_NAME = 22102051;
27 public const EXCEPTION_INVALID_COMPONENT_NAME = 2210206;
28 public const EXCEPTION_NO_COMPONENT = 2210207;
29 public const EXCEPTION_NO_COMPONENT_AJAX_CLASS = 2210208;
30
32 protected $module = 'main';
33 protected $action = 'index';
34 protected $component;
35 protected $mode;
36
40 private $request;
41
46 public function __construct(HttpRequest $request)
47 {
48 $this->request = $request;
49
50 $this->component = $this->request->getQuery('c') ?: null;
51 $this->mode = $this->request->getQuery('mode') ?: null;
52 $this->action = $this->request->getQuery('action');
53
54 if (!\is_string($this->component))
55 {
56 $this->component = null;
57 }
58 if (!\is_string($this->mode))
59 {
60 $this->component = null;
61 }
62 if (!\is_string($this->action))
63 {
64 $this->action = null;
65 }
66
67 if ($this->action && !$this->component)
68 {
69 list($this->vendor, $this->action) = $this->resolveVendor($this->action);
70 list($module, $this->action) = $this->resolveModuleAndAction($this->action);
71
72 $this->module = $this->refineModuleName($this->vendor, $module);
73 }
74 }
75
76 private function resolveModuleAndAction($action)
77 {
78 $actionParts = explode('.', $action);
79 $module = array_shift($actionParts);
80 $action = implode('.', $actionParts);
81
82 return [
84 ];
85 }
86
87 private function resolveVendor($action)
88 {
89 list($vendor, $action) = explode(':', $action) + [null, null];
90 if (!$action)
91 {
94 }
95
96 return [
98 ];
99 }
100
101 protected function refineModuleName($vendor, $module)
102 {
104 {
105 return $module;
106 }
107
108 return $vendor . '.' . $module;
109 }
110
123 protected function buildComponent($componentName, $signedParameters = null, $template = null)
124 {
125 $class = \CBitrixComponent::includeComponentClass($componentName);
126 if (!is_subclass_of($class, 'CBitrixComponent'))
127 {
128 return null;
129 }
130
131 $parameters = array();
132 if ($signedParameters)
133 {
134 $parameters = ParameterSigner::unsignParameters($componentName, $signedParameters);
135 }
136
138 $component = new $class();
139
140 if (!($component instanceof Controllerable))
141 {
142 throw new SystemException(
143 "The component {$this->component} must be implement interface \Bitrix\Main\Engine\Contract\Controllerable",
144 self::EXCEPTION_INVALID_COMPONENT_INTERFACE
145 );
146 }
147
148 $component->initComponent($componentName, $template);
149 $component->onIncludeComponentLang();
150 $component->arParams = $component->onPrepareComponentParams($parameters);
151 $component->__prepareComponentParams($component->arParams);
152
153 return $component;
154 }
155
159 public function getControllerAndAction()
160 {
161 if ($this->component)
162 {
163 return $this->getComponentControllerAndAction();
164 }
165
166 $this->includeModule($this->module);
167 $controllerAndAction = Resolver::getControllerAndAction($this->vendor, $this->module, $this->action);
168 if ($controllerAndAction)
169 {
170 return $controllerAndAction;
171 }
172 //default ajax class
173 $ajaxClass = DefaultController::className();
174
176 return array(new $ajaxClass, $this->action);
177 }
178
179 private function getComponentControllerAndAction()
180 {
181 $componentAsString = var_export($this->component, true);
182 if ($this->mode === self::COMPONENT_MODE_CLASS)
183 {
184 $component = $this->buildComponent($this->component, $this->request->getPost('signedParameters'));
185 if (!$component)
186 {
187 throw new SystemException(
188 "Could not build component instance {$componentAsString}",
189 self::EXCEPTION_INVALID_COMPONENT
190 );
191 }
192
193 return array(new ComponentController($component), $this->action);
194 }
195 elseif ($this->mode === self::COMPONENT_MODE_AJAX)
196 {
197 $ajaxClass = $this->includeComponentAjaxClass($this->component);
198
199 $controller = ControllerBuilder::build($ajaxClass, [
200 'scope' => Controller::SCOPE_AJAX,
201 'currentUser' => CurrentUser::get(),
202 ]);
203
204 return [$controller, $this->action];
205 }
206 else
207 {
208 $modeAsString = var_export($this->mode, true);
209 throw new SystemException(
210 "Unknown ajax mode ({$modeAsString}) to work {$componentAsString}",
211 self::EXCEPTION_INVALID_AJAX_MODE
212 );
213 }
214 }
215
216 private function includeModule($module)
217 {
219 {
220 throw new SystemException(
221 "Invalid module name {$module}",
222 self::EXCEPTION_INVALID_MODULE_NAME
223 );
224 }
225
226 if (!Configuration::getInstance($module)->get('controllers'))
227 {
228 throw new SystemException(
229 "Could not find configuration 'controllers' for module {$module}. Probably module is not installed or not configured properly.",
230 self::EXCEPTION_NO_CONFIGURATION
231 );
232 }
233
235 {
236 throw new SystemException("Could not find module {$module}", self::EXCEPTION_NO_MODULE);
237 }
238 }
239
240 private function includeComponentAjaxClass($name)
241 {
242 $path2Comp = \CComponentEngine::makeComponentPath($name);
243 if ($path2Comp === '')
244 {
245 throw new SystemException("{$name} is not a valid component name", self::EXCEPTION_INVALID_COMPONENT_NAME);
246 }
247
248 $componentPath = getLocalPath("components" . $path2Comp);
249 if ($componentPath === false)
250 {
251 throw new SystemException("Could not find component by name {$name}", self::EXCEPTION_NO_COMPONENT);
252 }
253
254 $ajaxClass = $this->getAjaxClassForPath($componentPath);
255 if (!$ajaxClass)
256 {
257 throw new SystemException("Could not find ajax class {$componentPath}", self::EXCEPTION_NO_COMPONENT_AJAX_CLASS);
258 }
259
260 return $ajaxClass;
261 }
262
263 private function getAjaxClassForPath($componentPath)
264 {
265 $filename = \Bitrix\Main\Application::getDocumentRoot() . $componentPath . '/ajax.php';
266 if (!file_exists($filename) || !is_file($filename))
267 {
268 return null;
269 }
270
271 $beforeClasses = get_declared_classes();
272 $beforeClassesCount = count($beforeClasses);
273 include_once($filename);
274 $afterClasses = get_declared_classes();
275 $afterClassesCount = count($afterClasses);
276 $furthestClass = null;
277 for ($i = $afterClassesCount - 1; $i >= $beforeClassesCount; $i--)
278 {
279 if (
280 is_subclass_of($afterClasses[$i], Controller::class) ||
281 ($furthestClass && is_subclass_of($afterClasses[$i], $furthestClass))
282 )
283 {
284 $furthestClass = $afterClasses[$i];
285 }
286 }
287
288 return $furthestClass;
289 }
290
295 public function setRequest(HttpRequest $request)
296 {
297 $this->request = $request;
298
299 return $this;
300 }
301
305 public function getVendor()
306 {
307 return $this->vendor;
308 }
309
313 public function getModule()
314 {
315 return $this->module;
316 }
317
321 public function getAction()
322 {
323 return $this->action;
324 }
325
329 public function getComponent()
330 {
331 return $this->component;
332 }
333
337 public function getMode()
338 {
339 return $this->mode;
340 }
341}
static className()
Определения controller.php:79
static get()
Определения currentuser.php:33
const DEFAULT_VENDOR
Определения resolver.php:11
static getControllerAndAction($vendor, $module, $action, $scope=Controller::SCOPE_AJAX)
Определения resolver.php:23
const COMPONENT_MODE_AJAX
Определения router.php:18
getAction()
Определения router.php:321
getVendor()
Определения router.php:305
$component
Определения router.php:34
getComponent()
Определения router.php:329
setRequest(HttpRequest $request)
Определения router.php:295
const EXCEPTION_NO_MODULE
Определения router.php:25
const COMPONENT_MODE_CLASS
Определения router.php:19
getModule()
Определения router.php:313
getMode()
Определения router.php:337
const EXCEPTION_INVALID_COMPONENT_INTERFACE
Определения router.php:21
const EXCEPTION_NO_CONFIGURATION
Определения router.php:24
const EXCEPTION_NO_COMPONENT_AJAX_CLASS
Определения router.php:29
getControllerAndAction()
Определения router.php:159
const EXCEPTION_NO_COMPONENT
Определения router.php:28
const EXCEPTION_INVALID_MODULE_NAME
Определения router.php:26
const EXCEPTION_INVALID_COMPONENT
Определения router.php:22
__construct(HttpRequest $request)
Определения router.php:46
refineModuleName($vendor, $module)
Определения router.php:101
const EXCEPTION_INVALID_COMPONENT_NAME
Определения router.php:27
const EXCEPTION_INVALID_AJAX_MODE
Определения router.php:23
static includeModule($moduleName)
Определения loader.php:67
static isValidModule(string $moduleName)
Определения modulemanager.php:184
if( $strWarning=="") if($strWarning=="") $componentPath
Определения component_props2.php:197
$componentName
Определения component_props2.php:49
$filename
Определения file_edit.php:47
$template
Определения file_edit.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
getLocalPath($path, $baseFolder="/bitrix")
Определения tools.php:5092
$name
Определения menu_edit.php:35
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$i
Определения factura.php:643
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$action
Определения file_dialog.php:21