3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
16
use FilesystemIterator;
17
use RecursiveDirectoryIterator;
18
use RecursiveIteratorIterator;
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
31
final class ComposerScripts
34
* Path to the ThirdParty directory.
36
private static string $path = __DIR__ . '/ThirdParty/';
39
* Direct dependencies of CodeIgniter to copy
40
* contents to `system/ThirdParty/`.
42
* @var array<string, array<string, string>>
44
private static array $dependencies = [
46
'license' => __DIR__ . '/../vendor/kint-php/kint/LICENSE',
47
'from' => __DIR__ . '/../vendor/kint-php/kint/src/',
48
'to' => __DIR__ . '/ThirdParty/Kint/',
51
'from' => __DIR__ . '/../vendor/kint-php/kint/resources/',
52
'to' => __DIR__ . '/ThirdParty/Kint/resources/',
55
'license' => __DIR__ . '/../vendor/laminas/laminas-escaper/LICENSE.md',
56
'from' => __DIR__ . '/../vendor/laminas/laminas-escaper/src/',
57
'to' => __DIR__ . '/ThirdParty/Escaper/',
60
'license' => __DIR__ . '/../vendor/psr/log/LICENSE',
61
'from' => __DIR__ . '/../vendor/psr/log/src/',
62
'to' => __DIR__ . '/ThirdParty/PSR/Log/',
67
* This static method is called by Composer after every update event,
68
* i.e., `composer install`, `composer update`, `composer remove`.
70
public static function postUpdate()
72
self::recursiveDelete(self::$path);
74
foreach (self::$dependencies as $key => $dependency) {
75
// Kint may be removed.
76
if (! is_dir($dependency['from']) && str_starts_with($key, 'kint')) {
80
self::recursiveMirror($dependency['from'], $dependency['to']);
82
if (isset($dependency['license'])) {
83
$license = basename($dependency['license']);
84
copy($dependency['license'], $dependency['to'] . '/' . $license);
88
self::copyKintInitFiles();
92
* Recursively remove the contents of the previous `system/ThirdParty`.
94
private static function recursiveDelete(string $directory): void
96
if (! is_dir($directory)) {
97
echo sprintf('Cannot recursively delete "%s" as it does not exist.', $directory) . PHP_EOL;
102
/** @var SplFileInfo $file */
103
foreach (new RecursiveIteratorIterator(
104
new RecursiveDirectoryIterator(rtrim($directory, '\\/'), FilesystemIterator::SKIP_DOTS),
105
RecursiveIteratorIterator::CHILD_FIRST
107
$path = $file->getPathname();
109
if ($file->isDir()) {
118
* Recursively copy the files and directories of the origin directory
119
* into the target directory, i.e. "mirror" its contents.
121
private static function recursiveMirror(string $originDir, string $targetDir): void
123
$originDir = rtrim($originDir, '\\/');
124
$targetDir = rtrim($targetDir, '\\/');
126
if (! is_dir($originDir)) {
127
echo sprintf('The origin directory "%s" was not found.', $originDir);
132
if (is_dir($targetDir)) {
133
echo sprintf('The target directory "%s" is existing. Run %s::recursiveDelete(\'%s\') first.', $targetDir, self::class, $targetDir);
138
if (! @mkdir($targetDir, 0755, true)) {
139
echo sprintf('Cannot create the target directory: "%s"', $targetDir) . PHP_EOL;
144
$dirLen = strlen($originDir);
146
/** @var SplFileInfo $file */
147
foreach (new RecursiveIteratorIterator(
148
new RecursiveDirectoryIterator($originDir, FilesystemIterator::SKIP_DOTS),
149
RecursiveIteratorIterator::SELF_FIRST
151
$origin = $file->getPathname();
152
$target = $targetDir . substr($origin, $dirLen);
154
if ($file->isDir()) {
155
@mkdir($target, 0755);
157
@copy($origin, $target);
163
* Copy Kint's init files into `system/ThirdParty/Kint/`
165
private static function copyKintInitFiles(): void
167
$originDir = self::$dependencies['kint-src']['from'] . '../';
168
$targetDir = self::$dependencies['kint-src']['to'];
170
foreach (['init.php', 'init_helpers.php'] as $kintInit) {
171
@copy($originDir . $kintInit, $targetDir . $kintInit);