-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRectorContainerConfigurator.php
More file actions
99 lines (85 loc) · 2.88 KB
/
RectorContainerConfigurator.php
File metadata and controls
99 lines (85 loc) · 2.88 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 Crmplease\Coder;
use Rector\Core\Configuration\Configuration;
use Rector\Core\DependencyInjection\RectorContainerFactory;
use Rector\Core\Set\Set;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symplify\SetConfigResolver\ConfigResolver;
use Symplify\SmartFileSystem\Exception\FileNotFoundException;
use function array_merge;
/**
* @author Mougrim <rinat@mougrim.ru>
* Based on https://github.com/rectorphp/rector/blob/v0.6.14/bin/rector
* @see https://github.com/rectorphp/rector/blob/v0.6.14/bin/rector
* @see \RectorConfigsResolver
*/
class RectorContainerConfigurator
{
private $config;
private $configResolver;
public function __construct(Config $config)
{
$this->config = $config;
$this->configResolver = new ConfigResolver();
}
/**
* @return string[]
* @throws FileNotFoundException
*/
protected function provide(): array
{
$configs = [];
// Detect configuration from --set
$input = new ArgvInput([]);
$setConfig = $this->configResolver->resolveSetFromInputAndDirectory($input, Set::SET_DIRECTORY);
if ($setConfig !== null) {
$configs[] = $setConfig;
}
// And from --config or default one
$inputOrFallbackConfig = $this->configResolver->resolveFromInputWithFallback(
$input,
['rector.yaml']
);
if ($inputOrFallbackConfig !== null) {
$configs[] = $inputOrFallbackConfig;
}
// resolve: parameters > sets
$parameterSetsConfigs = $this->configResolver->resolveFromParameterSetsFromConfigFiles(
$configs,
Set::SET_DIRECTORY
);
if ($parameterSetsConfigs !== []) {
$configs = array_merge($configs, $parameterSetsConfigs);
}
return $configs;
}
/**
* @return string|null
*/
protected function getFirstResolvedConfig(): ?string
{
return $this->configResolver->getFirstResolvedConfig();
}
/**
* @return ContainerInterface
* @throws FileNotFoundException
*/
public function configureContainer(): ContainerInterface
{
$configs = $this->provide();
$configs[] = __DIR__ . '/../rector.yaml';
if ($this->config->getRectorConfigPath()) {
$configs[] = $this->config->getRectorConfigPath();
}
// Build DI container
$rectorContainerFactory = new RectorContainerFactory();
/** @var ContainerInterface $container */
$container = $rectorContainerFactory->createFromConfigs($configs);
/** @var Configuration $configuration */
$configuration = $container->get(Configuration::class);
$configuration->setFirstResolverConfig($this->getFirstResolvedConfig());
return $container;
}
}