startBuffering(); // 添加文件的辅助函数 function addDirectory(Phar $phar, string $dir, string $localPath = ''): int { $count = 0; $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS) ); foreach ($iterator as $file) { if ($file->isFile()) { $filePath = $file->getPathname(); $relativePath = $localPath . '/' . $iterator->getSubPathname(); $phar->addFile($filePath, ltrim($relativePath, '/')); $count++; } } return $count; } // 添加 src 目录 echo "添加 src/...\n"; $count = addDirectory($phar, __DIR__ . '/src', 'src'); echo " 添加了 $count 个文件\n"; // 添加 vendor 目录 echo "添加 vendor/...\n"; $count = addDirectory($phar, __DIR__ . '/vendor', 'vendor'); echo " 添加了 $count 个文件\n"; // 添加 bin/game if (file_exists(__DIR__ . '/bin/game')) { $phar->addFile(__DIR__ . '/bin/game', 'bin/game'); echo "添加 bin/game\n"; } // 创建启动脚本 $stub = <<<'STUB' #!/usr/bin/env php run(); __HALT_COMPILER(); STUB; $phar->setStub($stub); $phar->stopBuffering(); // 压缩 echo "压缩中...\n"; $phar->compressFiles(Phar::GZ); // 设置可执行权限 chmod($pharFile, 0755); $size = round(filesize($pharFile) / 1024, 2); echo "\n打包完成!\n"; echo "文件: $pharFile\n"; echo "大小: {$size} KB\n"; echo "运行: php build/hanli-idle.phar\n";