Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ControllerResolver.php
1<?php
2
4
7
9{
10 const DEFAULT_VENDOR = 'bitrix';
11
12 public static function createController(string $controllerName, array $options = []): ?UploaderController
13 {
14 [$moduleId, $className] = self::resolveName($controllerName);
15
16 if (!is_string($className))
17 {
18 return null;
19 }
20
21 if (is_string($moduleId) && self::canIncludeModule($moduleId))
22 {
23 Loader::includeModule($moduleId);
24 }
25
26 try
27 {
28 $controllerClass = new \ReflectionClass($className);
29 if ($controllerClass->isAbstract())
30 {
31 return null;
32 }
33
34 if (!$controllerClass->isSubclassOf(UploaderController::class))
35 {
36 return null;
37 }
38
40 $controller = $controllerClass->newInstance($options);
41
42 // $baseClass = new \ReflectionClass(UploaderController::class);
43 // $moduleIdProperty = $baseClass->getProperty('moduleId');
44 // $moduleIdProperty->setAccessible(true);
45 // $moduleIdProperty->setValue($controller, $moduleId);
46 //
47 // $nameProperty = $baseClass->getProperty('name');
48 // $nameProperty->setAccessible(true);
49 // $nameProperty->setValue($controller, $controllerName);
50
51 if (!$controller->isAvailable())
52 {
53 return null;
54 }
55
56 return $controller;
57 }
58 catch (\ReflectionException $exception)
59 {
60 $application = HttpApplication::getInstance();
61 $exceptionHandler = $application->getExceptionHandler();
62 $exceptionHandler->writeToLog($exception);
63 }
64
65 return null;
66 }
67
68 public static function resolveName(string $controllerName): array
69 {
70 $controllerName = trim($controllerName);
71 if (mb_strlen($controllerName) < 1)
72 {
73 return [null, null];
74 }
75
76 [$vendor, $controllerName] = self::resolveVendor($controllerName);
77 [$moduleId, $className] = self::resolveModuleAndClass($controllerName);
78 $moduleId = self::refineModuleName($vendor, $moduleId);
79
80 $className = self::buildClassName($vendor, $moduleId, $className);
81
82 return [$moduleId, $className];
83 }
84
85 public static function getNameByController(UploaderController $controller): string
86 {
87 $parts = explode('\\', get_class($controller));
88 $vendor = mb_strtolower(array_shift($parts));
89 $moduleId = mb_strtolower(array_shift($parts));
90
91 $parts = array_map(
92 function ($part) {
93 return lcfirst($part);
94 },
95 $parts
96 );
97
98 if ($vendor === self::DEFAULT_VENDOR)
99 {
100 return $moduleId . '.' . implode('.', $parts);
101 }
102 else
103 {
104 return $vendor . ':' . $moduleId . '.' . implode('.', $parts);
105 }
106 }
107
108 private static function buildClassName(string $vendor, string $moduleId, string $className): string
109 {
110 if ($vendor === self::DEFAULT_VENDOR)
111 {
112 $moduleId = ucfirst($moduleId);
113 $namespace = "\\Bitrix\\{$moduleId}";
114 }
115 else
116 {
117 $moduleParts = explode('.', $moduleId);
118 $moduleParts = array_map(
119 function ($part) {
120 return ucfirst(trim(trim($part), '\\'));
121 },
122 $moduleParts
123 );
124
125 $namespace = "\\" . join('\\', $moduleParts);
126 }
127
128 $classNameParts = explode('.', $className);
129 $classNameParts = array_map(
130 function ($part) {
131 return ucfirst(trim(trim($part), '\\'));
132 },
133 $classNameParts
134 );
135
136 if (!$classNameParts)
137 {
138 return $namespace;
139 }
140
141 return "{$namespace}\\" . join('\\', $classNameParts);
142 }
143
144 private static function resolveModuleAndClass(string $controllerName): array
145 {
146 $parts = explode('.', $controllerName);
147 $moduleId = array_shift($parts);
148 $className = implode('.', $parts);
149
150 return [$moduleId, $className];
151 }
152
153 private static function resolveVendor(string $controllerName): array
154 {
155 [$vendor, $controllerName] = explode(':', $controllerName) + [null, null];
156
157 if (!$controllerName)
158 {
159 $controllerName = $vendor;
160 $vendor = self::DEFAULT_VENDOR;
161 }
162
163 return [$vendor, $controllerName];
164 }
165
166 private static function refineModuleName($vendor, $moduleId): string
167 {
168 if ($vendor === self::DEFAULT_VENDOR)
169 {
170 return mb_strtolower($moduleId);
171 }
172
173 return mb_strtolower($vendor . '.' . $moduleId);
174 }
175
176 private static function canIncludeModule(string $moduleId): bool
177 {
178 $settings = \Bitrix\Main\Config\Configuration::getInstance($moduleId)->get('ui.uploader');
179 if (empty($settings) || !is_array($settings))
180 {
181 return false;
182 }
183
184 return isset($settings['allowUseControllers']) && $settings['allowUseControllers'] === true;
185 }
186}
static getNameByController(UploaderController $controller)
static resolveName(string $controllerName)