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
| <?php // 禁止缓冲输出,以便立即看到错误信息 ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
// 确保Phar扩展可用 if (!extension_loaded('phar')) { die('Phar extension is not loaded.'); }
// PHAR文件的名称和目标路径 $pharName = 'app.phar'; $projectDir = './src'; // 你的项目源代码目录
// 删除已存在的PHAR文件以避免冲突 if (file_exists($pharName)) { unlink($pharName); }
// 创建新的Phar实例 $phar = new Phar($pharName, FilesystemIterator::CURRENT_AS_FILEINFO, $pharName); $phar->startBuffering();
$phar->buildFromDirectory($projectDir);
// 压缩文件(可选) $phar->compressFiles(Phar::GZ);
$stubContent = "#!/usr/bin/env ./swoole-cli <?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); define('IN_PHAR', true); Phar::mapPhar('app.phar'); require 'phar://app.phar/index.php'; __HALT_COMPILER();";
$phar->setStub($stubContent); $phar->stopBuffering();
echo "PHAR file created successfully.\n";
|