-
Notifications
You must be signed in to change notification settings - Fork 16
/
ApplicationFactory.php
85 lines (76 loc) 路 2.75 KB
/
ApplicationFactory.php
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
<?php
namespace Castor\Console;
use Castor\Console\Command\CompileCommand;
use Castor\Console\Command\DebugCommand;
use Castor\Console\Command\RepackCommand;
use Castor\ContextRegistry;
use Castor\EventDispatcher;
use Castor\ExpressionLanguage;
use Castor\Fingerprint\FingerprintHelper;
use Castor\FunctionFinder;
use Castor\Monolog\Processor\ProcessProcessor;
use Castor\PathHelper;
use Castor\PlatformUtil;
use Castor\Remote\Composer;
use Castor\Remote\EventSubscriber\RemoteImportSubscriber;
use Castor\Remote\Importer;
use Castor\Stub\StubsGenerator;
use Castor\WaitForHelper;
use Monolog\Logger;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpClient\HttpClient;
/** @internal */
class ApplicationFactory
{
public static function create(): SymfonyApplication
{
try {
$rootDir = PathHelper::getRoot();
} catch (\RuntimeException $e) {
return new CastorFileNotFoundApplication($e);
}
$class = Application::class;
if (class_exists(\RepackedApplication::class)) {
$class = \RepackedApplication::class;
}
$contextRegistry = new ContextRegistry();
$httpClient = HttpClient::create([
'headers' => [
'User-Agent' => 'Castor/' . Application::VERSION,
],
]);
$cacheDir = PlatformUtil::getCacheDirectory();
$cache = new FilesystemAdapter(directory: $cacheDir);
$logger = new Logger('castor', [], [new ProcessProcessor()]);
$fs = new Filesystem();
$importer = new Importer($logger, new Composer($logger), $fs);
$eventDispatcher = new EventDispatcher(logger: $logger);
$eventDispatcher->addSubscriber(new RemoteImportSubscriber($importer));
/** @var SymfonyApplication */
// @phpstan-ignore-next-line
$application = new $class(
$rootDir,
new FunctionFinder($cache, $rootDir),
$contextRegistry,
$eventDispatcher,
new ExpressionLanguage($contextRegistry),
new StubsGenerator($logger),
$logger,
$fs,
new WaitForHelper($httpClient, $logger),
new FingerprintHelper($cache),
$importer,
$httpClient,
$cache,
);
$application->setDispatcher($eventDispatcher);
$application->add(new DebugCommand($rootDir, $cacheDir, $contextRegistry));
if (!class_exists(\RepackedApplication::class)) {
$application->add(new RepackCommand());
$application->add(new CompileCommand($httpClient, $fs));
}
return $application;
}
}