ci4

Форк
0
/
ComposerScripts.php 
174 строки · 5.2 Кб
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter;
15

16
use FilesystemIterator;
17
use RecursiveDirectoryIterator;
18
use RecursiveIteratorIterator;
19
use SplFileInfo;
20

21
/**
22
 * This class is used by Composer during installs and updates
23
 * to move files to locations within the system folder so that end-users
24
 * do not need to use Composer to install a package, but can simply
25
 * download.
26
 *
27
 * @codeCoverageIgnore
28
 *
29
 * @internal
30
 */
31
final class ComposerScripts
32
{
33
    /**
34
     * Path to the ThirdParty directory.
35
     */
36
    private static string $path = __DIR__ . '/ThirdParty/';
37

38
    /**
39
     * Direct dependencies of CodeIgniter to copy
40
     * contents to `system/ThirdParty/`.
41
     *
42
     * @var array<string, array<string, string>>
43
     */
44
    private static array $dependencies = [
45
        'kint-src' => [
46
            'license' => __DIR__ . '/../vendor/kint-php/kint/LICENSE',
47
            'from'    => __DIR__ . '/../vendor/kint-php/kint/src/',
48
            'to'      => __DIR__ . '/ThirdParty/Kint/',
49
        ],
50
        'kint-resources' => [
51
            'from' => __DIR__ . '/../vendor/kint-php/kint/resources/',
52
            'to'   => __DIR__ . '/ThirdParty/Kint/resources/',
53
        ],
54
        'escaper' => [
55
            'license' => __DIR__ . '/../vendor/laminas/laminas-escaper/LICENSE.md',
56
            'from'    => __DIR__ . '/../vendor/laminas/laminas-escaper/src/',
57
            'to'      => __DIR__ . '/ThirdParty/Escaper/',
58
        ],
59
        'psr-log' => [
60
            'license' => __DIR__ . '/../vendor/psr/log/LICENSE',
61
            'from'    => __DIR__ . '/../vendor/psr/log/src/',
62
            'to'      => __DIR__ . '/ThirdParty/PSR/Log/',
63
        ],
64
    ];
65

66
    /**
67
     * This static method is called by Composer after every update event,
68
     * i.e., `composer install`, `composer update`, `composer remove`.
69
     */
70
    public static function postUpdate()
71
    {
72
        self::recursiveDelete(self::$path);
73

74
        foreach (self::$dependencies as $key => $dependency) {
75
            // Kint may be removed.
76
            if (! is_dir($dependency['from']) && str_starts_with($key, 'kint')) {
77
                continue;
78
            }
79

80
            self::recursiveMirror($dependency['from'], $dependency['to']);
81

82
            if (isset($dependency['license'])) {
83
                $license = basename($dependency['license']);
84
                copy($dependency['license'], $dependency['to'] . '/' . $license);
85
            }
86
        }
87

88
        self::copyKintInitFiles();
89
    }
90

91
    /**
92
     * Recursively remove the contents of the previous `system/ThirdParty`.
93
     */
94
    private static function recursiveDelete(string $directory): void
95
    {
96
        if (! is_dir($directory)) {
97
            echo sprintf('Cannot recursively delete "%s" as it does not exist.', $directory) . PHP_EOL;
98

99
            return;
100
        }
101

102
        /** @var SplFileInfo $file */
103
        foreach (new RecursiveIteratorIterator(
104
            new RecursiveDirectoryIterator(rtrim($directory, '\\/'), FilesystemIterator::SKIP_DOTS),
105
            RecursiveIteratorIterator::CHILD_FIRST
106
        ) as $file) {
107
            $path = $file->getPathname();
108

109
            if ($file->isDir()) {
110
                @rmdir($path);
111
            } else {
112
                @unlink($path);
113
            }
114
        }
115
    }
116

117
    /**
118
     * Recursively copy the files and directories of the origin directory
119
     * into the target directory, i.e. "mirror" its contents.
120
     */
121
    private static function recursiveMirror(string $originDir, string $targetDir): void
122
    {
123
        $originDir = rtrim($originDir, '\\/');
124
        $targetDir = rtrim($targetDir, '\\/');
125

126
        if (! is_dir($originDir)) {
127
            echo sprintf('The origin directory "%s" was not found.', $originDir);
128

129
            exit(1);
130
        }
131

132
        if (is_dir($targetDir)) {
133
            echo sprintf('The target directory "%s" is existing. Run %s::recursiveDelete(\'%s\') first.', $targetDir, self::class, $targetDir);
134

135
            exit(1);
136
        }
137

138
        if (! @mkdir($targetDir, 0755, true)) {
139
            echo sprintf('Cannot create the target directory: "%s"', $targetDir) . PHP_EOL;
140

141
            exit(1);
142
        }
143

144
        $dirLen = strlen($originDir);
145

146
        /** @var SplFileInfo $file */
147
        foreach (new RecursiveIteratorIterator(
148
            new RecursiveDirectoryIterator($originDir, FilesystemIterator::SKIP_DOTS),
149
            RecursiveIteratorIterator::SELF_FIRST
150
        ) as $file) {
151
            $origin = $file->getPathname();
152
            $target = $targetDir . substr($origin, $dirLen);
153

154
            if ($file->isDir()) {
155
                @mkdir($target, 0755);
156
            } else {
157
                @copy($origin, $target);
158
            }
159
        }
160
    }
161

162
    /**
163
     * Copy Kint's init files into `system/ThirdParty/Kint/`
164
     */
165
    private static function copyKintInitFiles(): void
166
    {
167
        $originDir = self::$dependencies['kint-src']['from'] . '../';
168
        $targetDir = self::$dependencies['kint-src']['to'];
169

170
        foreach (['init.php', 'init_helpers.php'] as $kintInit) {
171
            @copy($originDir . $kintInit, $targetDir . $kintInit);
172
        }
173
    }
174
}
175

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.