CodeIgniter4/system/ComposerScripts.php

175 lines
5.2 KiB
PHP
Raw Normal View History

2019-03-27 09:21:05 -07:00
<?php
declare(strict_types=1);
/**
2021-07-19 21:32:33 +08:00
* This file is part of CodeIgniter 4 framework.
*
2020-10-24 16:38:41 +08:00
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
2021-07-19 21:32:33 +08:00
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
2019-03-27 09:21:05 -07:00
namespace CodeIgniter;
2021-05-01 22:12:19 +08:00
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
2019-04-19 17:56:57 +05:30
/**
2021-05-01 22:12:19 +08:00
* This class is used by Composer during installs and updates
* to move files to locations within the system folder so that end-users
* do not need to use Composer to install a package, but can simply
2021-05-01 22:12:19 +08:00
* download.
*
2015-12-18 23:42:54 -06:00
* @codeCoverageIgnore
2021-05-01 22:12:19 +08:00
*
* @internal
*/
2021-05-01 22:12:19 +08:00
final class ComposerScripts
{
2021-06-04 22:51:52 +08:00
/**
* Path to the ThirdParty directory.
*/
private static string $path = __DIR__ . '/ThirdParty/';
2021-06-04 22:51:52 +08:00
/**
* Direct dependencies of CodeIgniter to copy
* contents to `system/ThirdParty/`.
*
* @var array<string, array<string, string>>
*/
private static array $dependencies = [
2021-06-04 22:51:52 +08:00
'kint-src' => [
2021-12-27 09:50:10 +09:00
'license' => __DIR__ . '/../vendor/kint-php/kint/LICENSE',
'from' => __DIR__ . '/../vendor/kint-php/kint/src/',
'to' => __DIR__ . '/ThirdParty/Kint/',
2021-06-04 22:51:52 +08:00
],
'kint-resources' => [
'from' => __DIR__ . '/../vendor/kint-php/kint/resources/',
'to' => __DIR__ . '/ThirdParty/Kint/resources/',
],
'escaper' => [
2021-12-27 09:50:10 +09:00
'license' => __DIR__ . '/../vendor/laminas/laminas-escaper/LICENSE.md',
'from' => __DIR__ . '/../vendor/laminas/laminas-escaper/src/',
'to' => __DIR__ . '/ThirdParty/Escaper/',
2021-06-04 22:51:52 +08:00
],
'psr-log' => [
2021-12-27 09:50:10 +09:00
'license' => __DIR__ . '/../vendor/psr/log/LICENSE',
2023-09-24 17:45:22 +09:00
'from' => __DIR__ . '/../vendor/psr/log/src/',
2021-12-27 09:50:10 +09:00
'to' => __DIR__ . '/ThirdParty/PSR/Log/',
2021-06-04 22:51:52 +08:00
],
];
/**
* This static method is called by Composer after every update event,
* i.e., `composer install`, `composer update`, `composer remove`.
*/
public static function postUpdate()
{
self::recursiveDelete(self::$path);
foreach (self::$dependencies as $key => $dependency) {
// Kint may be removed.
2024-02-25 17:30:08 +09:00
if (! is_dir($dependency['from']) && str_starts_with($key, 'kint')) {
continue;
}
2021-06-04 22:51:52 +08:00
self::recursiveMirror($dependency['from'], $dependency['to']);
2021-12-27 09:50:10 +09:00
if (isset($dependency['license'])) {
$license = basename($dependency['license']);
copy($dependency['license'], $dependency['to'] . '/' . $license);
}
2021-06-04 22:51:52 +08:00
}
self::copyKintInitFiles();
}
/**
* Recursively remove the contents of the previous `system/ThirdParty`.
*/
private static function recursiveDelete(string $directory): void
{
2021-06-07 19:06:26 +08:00
if (! is_dir($directory)) {
echo sprintf('Cannot recursively delete "%s" as it does not exist.', $directory) . PHP_EOL;
return;
2021-06-04 22:51:52 +08:00
}
/** @var SplFileInfo $file */
foreach (new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(rtrim($directory, '\\/'), FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST,
2021-06-07 19:06:26 +08:00
) as $file) {
2021-06-04 22:51:52 +08:00
$path = $file->getPathname();
2021-06-07 19:06:26 +08:00
if ($file->isDir()) {
2021-06-04 22:51:52 +08:00
@rmdir($path);
2021-06-07 19:06:26 +08:00
} else {
2021-06-04 22:51:52 +08:00
@unlink($path);
}
}
}
/**
* Recursively copy the files and directories of the origin directory
* into the target directory, i.e. "mirror" its contents.
*/
private static function recursiveMirror(string $originDir, string $targetDir): void
{
$originDir = rtrim($originDir, '\\/');
$targetDir = rtrim($targetDir, '\\/');
2021-06-07 19:06:26 +08:00
if (! is_dir($originDir)) {
2021-06-04 22:51:52 +08:00
echo sprintf('The origin directory "%s" was not found.', $originDir);
2021-06-04 22:51:52 +08:00
exit(1);
}
2021-06-07 19:06:26 +08:00
if (is_dir($targetDir)) {
2021-06-04 22:51:52 +08:00
echo sprintf('The target directory "%s" is existing. Run %s::recursiveDelete(\'%s\') first.', $targetDir, self::class, $targetDir);
2021-06-04 22:51:52 +08:00
exit(1);
}
if (! @mkdir($targetDir, 0755, true)) {
echo sprintf('Cannot create the target directory: "%s"', $targetDir) . PHP_EOL;
exit(1);
}
2021-06-04 22:51:52 +08:00
$dirLen = strlen($originDir);
/** @var SplFileInfo $file */
foreach (new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($originDir, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
2021-06-07 19:06:26 +08:00
) as $file) {
2021-06-04 22:51:52 +08:00
$origin = $file->getPathname();
$target = $targetDir . substr($origin, $dirLen);
2021-06-07 19:06:26 +08:00
if ($file->isDir()) {
2021-06-04 22:51:52 +08:00
@mkdir($target, 0755);
2021-06-07 19:06:26 +08:00
} else {
2021-06-04 22:51:52 +08:00
@copy($origin, $target);
}
}
}
/**
* Copy Kint's init files into `system/ThirdParty/Kint/`
*/
private static function copyKintInitFiles(): void
{
$originDir = self::$dependencies['kint-src']['from'] . '../';
$targetDir = self::$dependencies['kint-src']['to'];
2021-06-07 19:06:26 +08:00
foreach (['init.php', 'init_helpers.php'] as $kintInit) {
2021-06-04 22:51:52 +08:00
@copy($originDir . $kintInit, $targetDir . $kintInit);
}
}
}