9
9
use Symfony \Component \Console \Exception \CommandNotFoundException ;
10
10
use Symfony \Component \DependencyInjection \ContainerInterface ;
11
11
use Symfony \Component \HttpKernel \Bundle \Bundle ;
12
+ use Bitrix \Main \ModuleManager ;
12
13
13
14
/**
14
15
* Class ConsoleCommandConfigurator
17
18
* @since 10.12.2020
18
19
* @since 20.12.2020 Рефакторинг. Форк нативного способа подключения команд.
19
20
* @since 26.02.2021 Убрал array_merge в цикле.
21
+ * @since 12.01.2021 Подхватывание команд установленных битриксовых модулей.
20
22
*/
21
23
class ConsoleCommandConfigurator
22
24
{
@@ -126,6 +128,51 @@ public function setAutoExit(bool $autoexit) : void
126
128
$ this ->application ->setAutoExit ($ autoexit );
127
129
}
128
130
131
+ /**
132
+ * Регистрация команд битриксовых модулей.
133
+ *
134
+ * @return void
135
+ *
136
+ * @since 12.01.2021
137
+ *
138
+ * @internal Формат файла cli.php в папке модуля.
139
+ * return [
140
+ * new SampleCommand(), // Должен наследоваться от \Symfony\Component\Console\Command\Command
141
+ * container()->get('sample.command') // Из контейнера
142
+ * ]
143
+ */
144
+ private function registerModuleCommands () : void
145
+ {
146
+ // Проверка - в Битриксе мы или нет.
147
+ if (!class_exists (ModuleManager::class)) {
148
+ return ;
149
+ }
150
+
151
+ $ result = [];
152
+
153
+ $ documentRoot = $ this ->container ->getParameter ('kernel.project_dir ' );
154
+
155
+ foreach (glob ($ documentRoot . '/local/modules/*/cli.php ' ) as $ path ) {
156
+ $ moduleName = $ this ->getModuleNameByPath ($ path );
157
+ if (ModuleManager::isModuleInstalled ($ moduleName )) {
158
+ $ result = require_once $ path ;
159
+ }
160
+ }
161
+
162
+ foreach (glob ($ documentRoot . '/bitrix/modules/*/cli.php ' ) as $ path ) {
163
+ $ moduleName = $ this ->getModuleNameByPath ($ path );
164
+ if (ModuleManager::isModuleInstalled ($ moduleName )) {
165
+ $ result = require_once $ path ;
166
+ }
167
+ }
168
+
169
+ foreach ((array )$ result as $ item ) {
170
+ if (is_subclass_of ($ item , Command::class)) {
171
+ $ this ->application ->add ($ item );
172
+ }
173
+ }
174
+ }
175
+
129
176
/**
130
177
* Регистрация команд.
131
178
*
@@ -160,5 +207,31 @@ private function registerCommands() : void
160
207
}
161
208
}
162
209
}
210
+
211
+ $ this ->registerModuleCommands ();
212
+ }
213
+
214
+ /**
215
+ * Название битриксового модуля по пути.
216
+ *
217
+ * @param string $path
218
+ *
219
+ * @return string
220
+ * @since 12.01.2021
221
+ */
222
+ private function getModuleNameByPath (string $ path ): string
223
+ {
224
+ $ documentRoot = $ this ->container ->getParameter ('kernel.project_dir ' );
225
+
226
+ $ path = str_replace (
227
+ [
228
+ $ documentRoot . '/bitrix/modules/ ' ,
229
+ $ documentRoot . '/local/modules/ ' ,
230
+ ],
231
+ '' ,
232
+ $ path
233
+ );
234
+
235
+ return current (explode ('/ ' , $ path ));
163
236
}
164
- }
237
+ }
0 commit comments