2020-08-31 10:11:06 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
declare(strict_types=1);
|
2020-08-31 10:11:06 +08:00
|
|
|
|
|
|
|
|
|
|
namespace ZM\Command;
|
|
|
|
|
|
|
2022-12-17 21:53:08 +08:00
|
|
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2022-12-31 22:59:03 +08:00
|
|
|
|
use ZM\Store\FileSystem;
|
2023-02-01 16:28:30 +08:00
|
|
|
|
use ZM\Store\PharHelper;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
|
2023-02-01 16:28:30 +08:00
|
|
|
|
#[AsCommand(name: 'build', description: '将炸毛框架项目构建一个 Phar 包')]
|
2020-08-31 10:11:06 +08:00
|
|
|
|
class BuildCommand extends Command
|
|
|
|
|
|
{
|
2022-12-17 22:18:50 +08:00
|
|
|
|
use NonPharLoadModeOnly;
|
|
|
|
|
|
|
2020-08-31 10:11:06 +08:00
|
|
|
|
/**
|
2022-08-01 16:31:54 +08:00
|
|
|
|
* 配置
|
2020-08-31 10:11:06 +08:00
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
protected function configure()
|
|
|
|
|
|
{
|
2023-02-01 16:28:30 +08:00
|
|
|
|
$this->setHelp('此功能将会把整个框架项目打包为 Phar' . PHP_EOL . '默认会启用压缩功能,通过去除文件中的注释和空格,以减小文件大小,但可能增加构建耗时');
|
|
|
|
|
|
$this->addOption('build-dir', 'D', InputOption::VALUE_REQUIRED, '指定输出文件夹(默认为 build/)', WORKING_DIR . '/build');
|
|
|
|
|
|
$this->addOption('target', 'T', InputOption::VALUE_REQUIRED, '指定输出文件名称(默认为 zm.phar)', 'zm.phar');
|
2022-12-31 23:57:56 +08:00
|
|
|
|
$this->addOption('no-compress', null, InputOption::VALUE_NONE, '是否不压缩文件,以减小构建耗时');
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-01 16:28:30 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @throws \PharException
|
|
|
|
|
|
*/
|
2022-12-31 22:59:03 +08:00
|
|
|
|
protected function handle(): int
|
2022-03-15 18:05:33 +08:00
|
|
|
|
{
|
2023-02-01 16:28:30 +08:00
|
|
|
|
// 确认可写 Phar
|
|
|
|
|
|
PharHelper::ensurePharWritable();
|
2022-12-31 22:59:03 +08:00
|
|
|
|
|
2022-12-31 23:01:00 +08:00
|
|
|
|
$target = $this->input->getOption('target');
|
2023-02-01 16:28:30 +08:00
|
|
|
|
$build_dir = $this->input->getOption('build-dir');
|
|
|
|
|
|
if (FileSystem::isRelativePath($build_dir)) {
|
|
|
|
|
|
$build_dir = WORKING_DIR . '/' . $build_dir;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
}
|
2023-02-01 16:28:30 +08:00
|
|
|
|
$target = $build_dir . '/' . $target;
|
|
|
|
|
|
// 确认 Phar 文件可以写入
|
|
|
|
|
|
PharHelper::ensurePharFileWritable($target);
|
|
|
|
|
|
|
2022-12-31 22:59:03 +08:00
|
|
|
|
$this->comment("目标文件:{$target}");
|
|
|
|
|
|
|
2022-12-31 23:57:56 +08:00
|
|
|
|
if (file_exists($target)) {
|
|
|
|
|
|
$this->comment('目标文件已存在,正在删除...');
|
|
|
|
|
|
unlink($target);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-31 22:59:03 +08:00
|
|
|
|
$this->info('正在构建 Phar 包');
|
|
|
|
|
|
|
2022-12-31 23:57:56 +08:00
|
|
|
|
$this->build(
|
|
|
|
|
|
$target,
|
2023-02-01 16:30:17 +08:00
|
|
|
|
LOAD_MODE === LOAD_MODE_SRC ? 'src/entry.php' : 'vendor/zhamao/framework/src/entry.php',
|
2022-12-31 23:57:56 +08:00
|
|
|
|
);
|
2022-12-31 22:59:03 +08:00
|
|
|
|
|
|
|
|
|
|
$this->info('Phar 包构建完成');
|
|
|
|
|
|
|
|
|
|
|
|
return self::SUCCESS;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-31 22:59:03 +08:00
|
|
|
|
private function build(string $target, string $entry): void
|
|
|
|
|
|
{
|
2023-01-01 00:18:27 +08:00
|
|
|
|
$phar = new \Phar($target, 0);
|
2022-12-31 22:59:03 +08:00
|
|
|
|
|
|
|
|
|
|
$phar->startBuffering();
|
|
|
|
|
|
$files = FileSystem::scanDirFiles(SOURCE_ROOT_DIR, true, true);
|
2023-02-01 16:27:05 +08:00
|
|
|
|
$separator = '\\' . DIRECTORY_SEPARATOR;
|
2023-01-01 00:18:27 +08:00
|
|
|
|
// 只打包 bin / config / resources / src / vendor 目录以及 composer.json / composer.lock / entry.php
|
2023-02-01 16:27:05 +08:00
|
|
|
|
$files = array_filter($files, function ($file) use ($separator) {
|
|
|
|
|
|
return preg_match('/^(bin|config|resources|src|vendor)' . $separator . '|^(composer\\.json|README\\.md)$/', $file);
|
2022-12-31 22:59:03 +08:00
|
|
|
|
});
|
|
|
|
|
|
sort($files);
|
|
|
|
|
|
|
2022-12-31 23:57:56 +08:00
|
|
|
|
if ($this->input->getOption('no-compress')) {
|
|
|
|
|
|
foreach ($this->progress()->iterate($files) as $file) {
|
|
|
|
|
|
$phar->addFile($file, $file);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
foreach ($this->progress()->iterate($files) as $file) {
|
|
|
|
|
|
$phar->addFromString($file, php_strip_whitespace($file));
|
|
|
|
|
|
}
|
2022-12-31 22:59:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$phar->setStub(
|
|
|
|
|
|
'#!/usr/bin/env php' . PHP_EOL .
|
|
|
|
|
|
$phar::createDefaultStub($entry)
|
|
|
|
|
|
);
|
|
|
|
|
|
$phar->stopBuffering();
|
|
|
|
|
|
}
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|