-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathComposerScripts.php
112 lines (89 loc) · 3.23 KB
/
ComposerScripts.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) 2023 CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\PHPStan;
use FilesystemIterator;
use JsonException;
use Phar;
use PharException;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
use UnexpectedValueException;
/**
* @internal
*/
final class ComposerScripts
{
private static string $vscodeSettingsJson = __DIR__ . '/../.vscode/settings.json';
public static function postUpdate(): void
{
if (is_file(self::$vscodeSettingsJson)) {
self::recursiveDelete(__DIR__ . '/../vendor/phpstan/phpstan-phar');
self::extractPhpstanPhar();
self::updateVscodeIntelephenseEnvironmentIncludePaths();
}
}
private static function recursiveDelete(string $directory): void
{
if (! is_dir($directory)) {
echo sprintf('Cannot recursively delete "%s" as it does not exist.', $directory) . PHP_EOL;
return;
}
/** @var SplFileInfo $file */
foreach (new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(rtrim($directory, '\\/'), FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST,
) as $file) {
$path = $file->getPathname();
if ($file->isDir()) {
rmdir($path);
} else {
unlink($path);
}
}
}
private static function extractPhpstanPhar(): void
{
try {
(new Phar(__DIR__ . '/../vendor/phpstan/phpstan/phpstan.phar'))->extractTo(__DIR__ . '/../vendor/phpstan/phpstan-phar', null, true);
} catch (PharException|UnexpectedValueException $e) {
echo $e->getMessage();
exit(1);
}
}
private static function updateVscodeIntelephenseEnvironmentIncludePaths(): void
{
$contents = @file_get_contents(self::$vscodeSettingsJson);
if ($contents === false) {
echo sprintf('Cannot get the contents of %s as it is probably missing or unreadable.', self::$vscodeSettingsJson);
exit(1);
}
$settingsJson = json_decode($contents, true);
$settingsJson['intelephense.environment.includePaths'] ??= [];
if (! in_array('vendor/phpstan/phpstan-phar/', $settingsJson['intelephense.environment.includePaths'], true)) {
$settingsJson['intelephense.environment.includePaths'][] = 'vendor/phpstan/phpstan-phar/';
}
ksort($settingsJson);
try {
$newContents = json_encode($settingsJson, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
if ($newContents === $contents) {
return;
}
if (file_put_contents(self::$vscodeSettingsJson, $newContents) === false) {
echo 'Cannot save the new contents to .vscode/settings.json.';
exit(1);
}
} catch (JsonException $e) {
echo $e->getMessage();
exit(1);
}
}
}