-
Notifications
You must be signed in to change notification settings - Fork 72
/
ModelStubProvider.php
76 lines (57 loc) · 2.16 KB
/
ModelStubProvider.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
<?php
namespace Psalm\LaravelPlugin\Providers;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\LaravelPlugin\Fakes\FakeFilesystem;
use Psalm\LaravelPlugin\Fakes\FakeModelsCommand;
use Psalm\LaravelPlugin\Handlers\Eloquent\Schema\SchemaAggregator;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use function glob;
use function method_exists;
use function unlink;
final class ModelStubProvider implements GeneratesStubs
{
/** @var list<class-string<\Illuminate\Database\Eloquent\Model>> */
private static array $model_classes = [];
public static function generateStubFile(): void
{
$app = ApplicationProvider::getApp();
if (!method_exists($app, 'databasePath')) {
throw new \RuntimeException('Unsupported Application type.');
}
$migrations_directory = $app->databasePath('migrations/');
$project_analyzer = ProjectAnalyzer::getInstance();
$codebase = $project_analyzer->getCodebase();
$schema_aggregator = new SchemaAggregator();
foreach (glob($migrations_directory . '*.php') as $file) {
$schema_aggregator->addStatements($codebase->getStatementsForFile($file));
}
$fake_filesystem = new FakeFilesystem();
$models_generator_command = new FakeModelsCommand(
$fake_filesystem,
$schema_aggregator
);
$models_generator_command->setLaravel($app);
@unlink(self::getStubFileLocation());
$fake_filesystem->setDestination(self::getStubFileLocation());
$models_generator_command->run(
new ArrayInput([
'--nowrite' => true,
'--reset' => true,
]),
new NullOutput()
);
self::$model_classes = $models_generator_command->getModels();
}
public static function getStubFileLocation(): string
{
return CacheDirectoryProvider::getCacheLocation() . '/models.stubphp';
}
/**
* @return list<class-string<\Illuminate\Database\Eloquent\Model>>
*/
public static function getModelClasses(): array
{
return self::$model_classes;
}
}