2021-04-16 15:29:04 +00:00
|
|
|
<?php
|
2021-04-17 12:09:58 +00:00
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2024-01-02 02:40:38 +00:00
|
|
|
namespace Rector\Bootstrap;
|
2021-04-16 15:29:04 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\RectorInstaller\GeneratedConfig;
|
2021-04-17 12:09:58 +00:00
|
|
|
use ReflectionClass;
|
2021-04-16 15:29:04 +00:00
|
|
|
final class ExtensionConfigResolver
|
|
|
|
{
|
|
|
|
/**
|
2022-12-23 17:10:25 +00:00
|
|
|
* @api
|
2022-09-01 09:30:44 +00:00
|
|
|
* @return string[]
|
2021-04-16 15:29:04 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function provide() : array
|
2021-04-16 15:29:04 +00:00
|
|
|
{
|
2022-09-01 09:30:44 +00:00
|
|
|
$configFilePaths = [];
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!\class_exists('Rector\\RectorInstaller\\GeneratedConfig')) {
|
2022-09-01 09:30:44 +00:00
|
|
|
return $configFilePaths;
|
2021-04-16 15:29:04 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$generatedConfigReflectionClass = new ReflectionClass('Rector\\RectorInstaller\\GeneratedConfig');
|
2021-05-09 20:15:43 +00:00
|
|
|
if ($generatedConfigReflectionClass->getFileName() === \false) {
|
2022-09-01 09:30:44 +00:00
|
|
|
return $configFilePaths;
|
2021-04-16 15:29:04 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$generatedConfigDirectory = \dirname($generatedConfigReflectionClass->getFileName());
|
2022-06-07 08:22:29 +00:00
|
|
|
foreach (GeneratedConfig::EXTENSIONS as $extensionConfig) {
|
2021-09-27 12:31:20 +00:00
|
|
|
/** @var string[] $includedFiles */
|
2021-09-27 15:43:15 +00:00
|
|
|
$includedFiles = $extensionConfig['extra']['includes'] ?? [];
|
2021-07-25 11:10:57 +00:00
|
|
|
foreach ($includedFiles as $includedFile) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$includedFilePath = $this->resolveIncludeFilePath($extensionConfig, $generatedConfigDirectory, $includedFile);
|
2021-04-16 15:29:04 +00:00
|
|
|
if ($includedFilePath === null) {
|
2021-09-27 12:31:20 +00:00
|
|
|
/** @var string $installPath */
|
|
|
|
$installPath = $extensionConfig['install_path'];
|
|
|
|
$includedFilePath = \sprintf('%s/%s', $installPath, $includedFile);
|
2021-04-16 15:29:04 +00:00
|
|
|
}
|
2022-09-01 09:30:44 +00:00
|
|
|
$configFilePaths[] = $includedFilePath;
|
2021-04-16 15:29:04 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-01 09:30:44 +00:00
|
|
|
return $configFilePaths;
|
2021-04-16 15:29:04 +00:00
|
|
|
}
|
2021-04-17 12:09:58 +00:00
|
|
|
/**
|
|
|
|
* @param array<string, mixed> $extensionConfig
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function resolveIncludeFilePath(array $extensionConfig, string $generatedConfigDirectory, string $includedFile) : ?string
|
|
|
|
{
|
|
|
|
if (!isset($extensionConfig['relative_install_path'])) {
|
2021-04-17 12:09:58 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-08-05 08:31:23 +00:00
|
|
|
$includedFilePath = \sprintf('%s/%s/%s', $generatedConfigDirectory, (string) $extensionConfig['relative_install_path'], $includedFile);
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!\file_exists($includedFilePath)) {
|
2021-04-17 12:09:58 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!\is_readable($includedFilePath)) {
|
2021-04-17 12:09:58 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $includedFilePath;
|
|
|
|
}
|
2021-04-16 15:29:04 +00:00
|
|
|
}
|