forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigInitializer.php
More file actions
99 lines (81 loc) · 3.35 KB
/
ConfigInitializer.php
File metadata and controls
99 lines (81 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
declare(strict_types=1);
namespace Rector\Configuration;
use Nette\Utils\FileSystem;
use Rector\Bootstrap\RectorConfigsResolver;
use Rector\Contract\Rector\RectorInterface;
use Rector\FileSystem\InitFilePathsResolver;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
final readonly class ConfigInitializer
{
/**
* @param RectorInterface[] $rectors
*/
public function __construct(
private array $rectors,
private InitFilePathsResolver $initFilePathsResolver,
private SymfonyStyle $symfonyStyle,
) {
}
public function createConfig(string $projectDirectory): void
{
$commonRectorConfigPath = $projectDirectory . '/' . RectorConfigsResolver::DEFAULT_CONFIG_FILE;
$distRectorConfigPath = $projectDirectory . '/' . RectorConfigsResolver::DEFAULT_DIST_CONFIG_FILE;
if (file_exists($commonRectorConfigPath)) {
$this->symfonyStyle->warning(
'Register rules or sets in your "' . RectorConfigsResolver::DEFAULT_CONFIG_FILE . '" config'
);
return;
}
if (file_exists($distRectorConfigPath)) {
$this->symfonyStyle->warning(
'Register rules or sets in your "' . RectorConfigsResolver::DEFAULT_DIST_CONFIG_FILE . '" config'
);
return;
}
$response = $this->symfonyStyle->ask(
'No "' . RectorConfigsResolver::DEFAULT_CONFIG_FILE . '" config found. Should we generate it for you?',
'yes'
);
// be tolerant about input
if (! in_array($response, ['yes', 'YES', 'y', 'Y'], true)) {
// okay, nothing we can do
return;
}
$configContents = FileSystem::read(__DIR__ . '/../../templates/rector.php.dist');
$configContents = $this->replacePathsContents($configContents, $projectDirectory);
FileSystem::write($commonRectorConfigPath, $configContents, null);
$this->symfonyStyle->success('The config is added now. Re-run command to make Rector do the work!');
}
public function areSomeRectorsLoaded(): bool
{
$activeRectors = $this->filterActiveRectors($this->rectors);
return $activeRectors !== [];
}
/**
* @param RectorInterface[] $rectors
* @return RectorInterface[]
*/
private function filterActiveRectors(array $rectors): array
{
return array_filter(
$rectors,
static fn (RectorInterface $rector): bool => ! $rector instanceof PostRectorInterface
);
}
private function replacePathsContents(string $rectorPhpTemplateContents, string $projectDirectory): string
{
$projectPhpDirectories = $this->initFilePathsResolver->resolve($projectDirectory);
// fallback to default 'src' in case of empty one
if ($projectPhpDirectories === []) {
$projectPhpDirectories[] = 'src';
}
$projectPhpDirectoriesContents = '';
foreach ($projectPhpDirectories as $projectPhpDirectory) {
$projectPhpDirectoriesContents .= " __DIR__ . '/" . $projectPhpDirectory . "'," . PHP_EOL;
}
$projectPhpDirectoriesContents = rtrim($projectPhpDirectoriesContents);
return str_replace('__PATHS__', $projectPhpDirectoriesContents, $rectorPhpTemplateContents);
}
}