Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
crawler.php
1<?php
2
3namespace Bitrix\Main\Engine;
4
5
9
10final class Crawler
11{
13 private static $instance;
14
15 private function __construct()
16 {
17 }
18
19 private function __clone()
20 {
21 }
22
27 public static function getInstance()
28 {
29 if (!isset(self::$instance))
30 {
31 self::$instance = new Crawler;
32 }
33
34 return self::$instance;
35 }
36
37 public function listActionsByModule($module)
38 {
39 $actions = array();
40 foreach ($this->getNamespaces($module) as $namespace)
41 {
42 $actions = array_merge(
43 $actions,
44 $this->getActionsFromControllers($namespace)
45 );
46 }
47
48 return array_unique($actions);
49 }
50
51 private function getActionsFromControllers($namespace)
52 {
53 $actions = array();
54 foreach ($this->getFilesUnderNamespace($namespace) as $className)
55 {
56 try
57 {
58 $reflectionClass = new \ReflectionClass($className);
59 if ($reflectionClass->isAbstract())
60 {
61 continue;
62 }
63
64 $classNamespace = mb_strtolower(trim($reflectionClass->getNamespaceName(), '\\'));
65 $namespace = mb_strtolower(trim($namespace, '\\'));
66
67 if (strpos($classNamespace, $namespace) === false)
68 {
69 continue;
70 }
71
72 if (!$reflectionClass->isSubclassOf(Controller::className()))
73 {
74 continue;
75 }
76
77 $controllerName = strtr($reflectionClass->getName(), '\\', '.');
78 $controllerName = mb_strtolower($controllerName);
79
80 $controller = $this->constructController($reflectionClass);
81 foreach ($controller->listNameActions() as $actionName)
82 {
83 $actions[] = $controllerName.'.'.mb_strtolower($actionName);
84 }
85 }
86 catch (\ReflectionException $exception)
87 {}
88 }
89
90 return $actions;
91 }
92
93 private function getFilesUnderNamespace($namespace)
94 {
95 $path = ltrim($namespace, "\\"); // fix web env
96 $path = strtolower($path);
97
98 $documentRoot = Context::getCurrent()->getServer()->getDocumentRoot();
99
100 if (preg_match("#[^\\\\/a-zA-Z0-9_]#", $path))
101 {
102 return array();
103 }
104
105 $path = str_replace('\\', '/', $path);
106 $pathParts = explode("/", $path);
107
108 if ($pathParts[0] === "bitrix")
109 {
110 array_shift($pathParts);
111
112 if (empty($pathParts))
113 {
114 return array();
115 }
116
117 $module = array_shift($pathParts);
118 if ($module == null || empty($pathParts))
119 {
120 return array();
121 }
122 }
123 else
124 {
125 $module1 = array_shift($pathParts);
126 $module2 = array_shift($pathParts);
127 if ($module1 == null || $module2 == null || empty($pathParts))
128 {
129 return array();
130 }
131
132 $module = $module1 . "." . $module2;
133 }
134
135 $rootFolder = $documentRoot . "/bitrix/modules/" . $module . "/lib/" . implode("/", $pathParts);
136
137 $classes = array();
138 foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($rootFolder)) as $file)
139 {
141 if (!$file->isFile())
142 {
143 continue;
144 }
145
146 if ($file->getPath() === $rootFolder)
147 {
148 $classes[] = $namespace . '\\' . $file->getBasename('.php');
149 continue;
150 }
151
152 $relativeFolder = trim(mb_substr($file->getPath(), mb_strlen($rootFolder)), '\\/');
153 $classes[] = $namespace . '\\' . strtr($relativeFolder, array('/' => '\\')) . '\\' . $file->getBasename('.php');
154 }
155
156 return $classes;
157 }
158
159 private function getNamespaces($module)
160 {
161 $controllersConfig = Configuration::getInstance($module);
162 if (!$controllersConfig['controllers'] || !$controllersConfig['controllers']['namespaces'])
163 {
164 return array();
165 }
166
167 $namespaces = array();
168 foreach ($controllersConfig['controllers']['namespaces'] as $key => $namespace)
169 {
170 if (is_string($key))
171 {
172 $namespaces[] = $key;
173 }
174 else
175 {
176 $namespaces[] = $namespace;
177 }
178 }
179
180 return $namespaces;
181 }
182
188 private function constructController(\ReflectionClass $reflectionClass)
189 {
192 $controller = $reflectionClass->newInstanceArgs();
193
194 return $controller;
195 }
196}
static getCurrent()
Definition context.php:241