1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
locatorcodesservice.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Main\Cli\Command\Dev\Service;
6
7use Bitrix\Main\Application;
8use Bitrix\Main\Cli\Command\Dev\Service\Locator\LocatorCodesDto;
9use Bitrix\Main\Cli\Command\Dev\Templates\LocatorCodesTemplate;
10use Bitrix\Main\Cli\Helper\Renderer;
11use Bitrix\Main\Cli\Helper\Renderer\Template;
12use Bitrix\Main\Config\Configuration;
13use ReflectionFunction;
14use Symfony\Component\Console\Exception\InvalidArgumentException;
15
17{
18 public function generateContent(LocatorCodesDto $dto): string
19 {
20 return $this->getTemplate($dto)->getContent();
21 }
22
23 public function generateFile(LocatorCodesDto $dto): void
24 {
25 $filePath = $this->getFilePath($dto->module);
26 $withTag = !file_exists($filePath);
27 $template = $this->getTemplate($dto, $withTag);
28 $renderer = new Renderer();
29
30 $renderer->replaceFileContent(
31 $filePath,
33 "#region autogenerated services for module {$dto->module}",
34 "#endregion",
35 );
36 }
37
38 private function getServicesByModule(string $module): array
39 {
40 $config = Configuration::getInstance($module);
41 $serviceConfig = $config->get('services');
42
43 $services = [];
44 foreach ($serviceConfig as $serviceName => $serviceData)
45 {
46 $serviceClass = $this->getServiceClass($serviceData);
47 if ($serviceClass !== null)
48 {
49 $services[$serviceName] = $serviceClass;
50 }
51 }
52
53 return $services;
54 }
55
56 private function getServiceClass(array $serviceData): ?string
57 {
58 $className = $serviceData['className'] ?? null;
59 if ($className !== null)
60 {
61 return '\\' . $className . '::class';
62 }
63
64 $constructor = $serviceData['constructor'] ?? null;
65 if ($constructor !== null)
66 {
67 $reflector = new ReflectionFunction($constructor);
68 if ($reflector->hasReturnType())
69 {
70 return '\\' .$reflector->getReturnType() . '::class';
71 }
72 }
73
74 return null;
75 }
76
77 private function getFilePath(string $module): string
78 {
79 $bitrixPath = Application::getDocumentRoot() . Application::getPersonalRoot() . '/modules/' . $module;
80 if (is_dir($bitrixPath))
81 {
82 return $bitrixPath . '/.phpstorm.meta.php';
83 }
84
85 $localPath = Application::getDocumentRoot() . '/local/modules/' . $module;
86 if (is_dir($localPath))
87 {
88 return $localPath . '/.phpstorm.meta.php';
89 }
90
91 throw new InvalidArgumentException('No such module');
92 }
93
94 private function getTemplate(LocatorCodesDto $dto, bool $withTag = false): Template
95 {
96 $services = $this->getServicesByModule($dto->module);
97
98 return new LocatorCodesTemplate($dto->code, $dto->module, $services, $withTag);
99 }
100}
static getDocumentRoot()
Определения application.php:736
static getPersonalRoot()
Определения application.php:762
$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
$config
Определения quickway.php:69
$localPath
Определения template_copy.php:184