Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
resolver.php
1<?php
2
3namespace Bitrix\Main\Engine;
4
5
8
9final class Resolver
10{
11 const DEFAULT_VENDOR = 'bitrix';
12
23 public static function getControllerAndAction($vendor, $module, $action, $scope = Controller::SCOPE_AJAX)
24 {
25 $parts = explode('.', $action);
26 $actionName = array_pop($parts);
27
28 $controllerClass = self::buildControllerClassName($vendor, $module, $parts);
29 if (!$controllerClass)
30 {
31 return null;
32 }
33
34 try
35 {
36 $controller = ControllerBuilder::build($controllerClass, [
37 'scope' => $scope,
38 'currentUser' => CurrentUser::get(),
39 ]);
40
41 return [$controller, $actionName];
42 }
43 catch (ObjectException $exception)
44 {}
45
46 return null;
47 }
48
49 private static function buildControllerClassName($vendor, $module, array $actionParts)
50 {
51 $controllerName = array_pop($actionParts);
52
53 if (self::isOldFullQualifiedName($vendor, $module, $actionParts))
54 {
55 $actionParts[] = $controllerName;
56 $className = implode('\\', $actionParts);
57 if (!self::checkClassUnderAllowedNamespaces($module, $className))
58 {
59 return null;
60 }
61
62 return $className;
63 }
64
65 $namespaces = self::listAllowedNamespaces($module);
66
67 $aliases = array_change_key_case($namespaces, CASE_LOWER);
68 $probablyPrefix = mb_strtolower(reset($actionParts));
69 if (isset($aliases[$probablyPrefix]))
70 {
71 $alias = $aliases[$probablyPrefix];
72 array_shift($actionParts); //drop prefix
73 array_push($actionParts, $controllerName);
74
75 return $alias . '\\' . implode('\\', $actionParts);
76 }
77
78 $furtherControllerClassName = self::buildClassNameByAction(
79 $vendor,
80 $module,
81 array_merge($actionParts, [$controllerName])
82 );
83 if (self::checkClassUnderAllowedNamespaces($module, $furtherControllerClassName))
84 {
85 return $furtherControllerClassName;
86 }
87
88 $defaultNamespaceByModule = self::getDefaultNamespaceByModule($module);
89 if (!$defaultNamespaceByModule)
90 {
91 return null;
92 }
93
94 $defaultPath = strtr($defaultNamespaceByModule, ['\\' => '.']);
95
96 // do not lower if probably psr4
97 $firstLetter = mb_substr($controllerName, 0, 1);
98
99 if ($firstLetter === mb_strtolower($firstLetter))
100 {
101 $defaultPath = mb_strtolower($defaultPath);
102 }
103
104
105 array_unshift($actionParts, ...explode('.', $defaultPath));
106 array_push($actionParts, $controllerName);
107
108 return implode('\\', $actionParts);
109 }
110
117 public static function getDefaultNamespaceByModule($module)
118 {
119 $controllersConfig = Configuration::getInstance($module);
120 if (!$controllersConfig['controllers'] || !isset($controllersConfig['controllers']['defaultNamespace']))
121 {
122 return null;
123 }
124
125 return $controllersConfig['controllers']['defaultNamespace'];
126 }
127
137 private static function isOldFullQualifiedName($vendor, $module, array $actionParts)
138 {
139 if ($vendor !== self::DEFAULT_VENDOR)
140 {
141 return false;
142 }
143
144 if (!isset($actionParts[0]) || !isset($actionParts[1]))
145 {
146 return false;
147 }
148
149 return $actionParts[0] === $vendor && $actionParts[1] === $module;
150 }
151
152 private static function listAllowedNamespaces($module)
153 {
154 $controllersConfig = Configuration::getInstance($module);
155 if (!$controllersConfig['controllers'])
156 {
157 return [];
158 }
159
160 $namespaces = [];
161 if (isset($controllersConfig['controllers']['namespaces']))
162 {
163 foreach ($controllersConfig['controllers']['namespaces'] as $key => $namespace)
164 {
165 if (is_int($key))
166 {
167 $namespaces[] = $namespace;
168 }
169 else
170 {
171 $namespaces[$namespace] = $key;
172 }
173 }
174 }
175
176 if (isset($controllersConfig['controllers']['defaultNamespace']))
177 {
178 $namespaces[] = $controllersConfig['controllers']['defaultNamespace'];
179 }
180
181 return $namespaces;
182 }
183
184 private static function checkClassUnderAllowedNamespaces($module, $class)
185 {
186 $namespaces = self::listAllowedNamespaces($module);
187 foreach ($namespaces as $namespace)
188 {
189 if (mb_stripos(trim($class, '\\'), trim($namespace, '\\') . '\\') === 0)
190 {
191 return true;
192 }
193 }
194
195 return false;
196 }
197
206 private static function buildClassNameByAction($vendor, $module, array $actionParts)
207 {
208 if($vendor === self::DEFAULT_VENDOR)
209 {
210 $namespace = "\\Bitrix\\$module";
211 }
212 else
213 {
214 $namespace = "\\" . strtr($module, ['.' => '\\']);
215 }
216
217 if (!$actionParts)
218 {
219 return $namespace;
220 }
221
222 return "{$namespace}\\" . trim(implode('\\', $actionParts), '\\');
223 }
224
234 public static function getNameByController(Controller $controller)
235 {
236 $parts = explode('\\', get_class($controller));
237 $vendor = mb_strtolower(array_shift($parts));
238 //lower case for module name
239 $parts[0] = mb_strtolower($parts[0]);
240
241 return $vendor . ':' . implode('.', $parts);
242 }
243}
static getDefaultNamespaceByModule($module)
Definition resolver.php:117
static getNameByController(Controller $controller)
Definition resolver.php:234
static getControllerAndAction($vendor, $module, $action, $scope=Controller::SCOPE_AJAX)
Definition resolver.php:23