简体   繁体   English

如何将一个PHP脚本转换成Phar文件?

[英]how to convert a php script into a phar file?

The goal of the project is to convert a php script into .phar 该项目的目标是将php脚本转换为.phar

My project is made of : 1 single index.php that calls classes into /lib and /database folders 我的项目由: 1个单独的index.php组成,该类将类调用到/ lib和/ database文件夹中

the goal is to have as less files as possible to distribute (ideally 2 : index.php and assets.phar which will include all files from /lib and /database) or even 1 (index.phar) 目标是要分发的文件尽可能少(理想情况下为2:index.php和asset.phar,其中将包括/ lib和/ database中的所有文件)甚至1(index.phar)

I tried with empir with any success: has someone has done that before ? 我尝试使用empir取得了任何成功:以前有人这样做过吗?

Any tutorial available ? 有教程可用吗?

Use the Phar class . 使用Phar For example, put this in compress.php : 例如,将其放在compress.php

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

Run php compress.php from the command line. 从命令行运行php compress.php Voilà, you have yourself a Phar. Voilà,您有一个Phar。

I use the following script: 我使用以下脚本:

<?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();

Run the following command to run the previous script: 运行以下命令以运行先前的脚本:

$ php -dphar.readonly=0 index.php

(Creating of phar archives is disabled by default in PHP.) (默认情况下,PHP中不创建phar存档。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM