如何将 php 脚本转换为 phar 文件?

how to convert a php script into a phar file?

该项目的目标是将 php 脚本转换为 .phar

我的项目由: 1 个 index.php 将类调用到 /lib 和 /database 文件夹

目标是分发尽可能少的文件(最好是 2 个:index.php 和 assets.phar,这将包括 /lib 和 /database 中的所有文件)甚至 1 个(index.phar)

我尝试使用 empir 并取得了成功:以前有人这样做过吗?

有可用的教程吗?

使用the Phar class。例如,将其放入 compress.php:

$phar = new Phar('index.phar');
$phar->addFile('index.php');
$phar->buildFromDirectory('lib');
$phar->buildFromDirectory('database');

运行 php compress.php 来自命令行。瞧,你有一个 Phar。

我使用以下脚本:

<?php
$phar = new \Phar(__DIR__ . '/index.phar');    
$phar->startBuffering(); // For performance reasons. Ordinarily, every time a file within a Phar archive is created or modified in any way, the entire Phar archive will be recreated with the changes.  
$phar->addFile('index.php');
$phar->addFile('composer.json');
$phar->addFile('composer.lock');
$phar->buildFromDirectory(__DIR__ . '/vendor');
$phar->stopBuffering();

运行 将以下命令添加到 运行 上一个脚本:

$ php -dphar.readonly=0 index.php

(在 PHP 中默认禁用创建 phar 存档。)