Repository navigation
Expand file tree
/
Copy pathBootstrapFilesIncluder.php
More file actions
62 lines (50 loc) · 1.87 KB
/
Copy pathBootstrapFilesIncluder.php
File metadata and controls
62 lines (50 loc) · 1.87 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
<?php
declare(strict_types=1);
namespace Rector\Autoloading;
use PHPStan\DependencyInjection\Container;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Exception\ShouldNotHappenException;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
use Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Autoloading\BootstrapFilesIncluderTest
*/
final class BootstrapFilesIncluder
{
/**
* Inspired by
* @see https://github.com/phpstan/phpstan-src/commit/aad1bf888ab7b5808898ee5fe2228bb8bb4e4cf1
*/
public function includeBootstrapFiles(Container $container): void
{
$bootstrapFiles = SimpleParameterProvider::provideArrayParameter(Option::BOOTSTRAP_FILES);
Assert::allString($bootstrapFiles);
/** @var string[] $bootstrapFiles */
foreach ($bootstrapFiles as $bootstrapFile) {
if (! is_file($bootstrapFile)) {
throw new ShouldNotHappenException(sprintf('Bootstrap file "%s" does not exist.', $bootstrapFile));
}
// mimic PHPStan bootstrap file inclusion (bootstrap files have access to the global $container variable)
(static function (string $file) use ($container): void {
require $file;
})($bootstrapFile);
}
$this->requireRectorStubs();
}
private function requireRectorStubs(): void
{
$stubsRectorDirectory = realpath(__DIR__ . '/../../stubs-rector');
if ($stubsRectorDirectory === false) {
return;
}
$dir = new RecursiveDirectoryIterator($stubsRectorDirectory, RecursiveDirectoryIterator::SKIP_DOTS);
$stubs = new RecursiveIteratorIterator($dir);
foreach ($stubs as $stub) {
/** @var SplFileInfo $stub */
require_once $stub->getRealPath();
}
}
}