为简单应用程序生成PHAR [英] Generating a PHAR for a simple application

查看:199
本文介绍了为简单应用程序生成PHAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony2控制台库来构建CLI工具。我有一些基本的工作,现在我想把它作为一个phar。我已经阅读了几个例子,但我看到的是非常简单的(3个文件,没有命名空间等)。

I'm experimenting in building CLI tools using the Symfony2 console library. I've got something basic working and now I want to package it as a phar. I've read a few examples but the ones I've seen are very simple (3 files, no namespaces, etc).

在我的 src / 目录我有以下:

以上 src / 我有一个 console.php ,我执行以运行应用程序。我也有一个 vendors / dir,因为我使用composer来安装依赖项。 console.php 非常简单:

Above src/ I have a console.php that I execute to run the app. I also have a vendors/ dir as I'm using composer to install dependencies. console.php is very simple:

#!/usr/bin/env php
<?php

set_time_limit(0);
$loader = require 'vendor/autoload.php';

use Symfony\Component\Console\Application;
use Bendihossan\Pinfo\Command\EnvironmentCommand;
use Bendihossan\Pinfo\Command\ExtensionsCommand;
use Bendihossan\Pinfo\Command\RunAllCommand;

$console = new Application();

$console->add(new RunAllCommand());
$console->add(new EnvironmentCommand);
$console->add(new ExtensionsCommand);

$console->run();

根据我的理解,建立phar我想我需要包括 console.php 作为存根和 src / 中的所有其他项加上 vendors /

From what (little) I understand about build a phar I think I need to include console.php as the stub and everything else in src/ plus all my dependencies in vendors/.

查看 phpmaster.com 他们使用 file_get_contents 手动指定要包含在phar中的每个文件,但是我需要维护我的目录结构,以便使用作曲家的自动加载器并保持PSR-0目录结构。

Looking at the example code on phpmaster.com they specify every file manually to be included in the phar using file_get_contents, but I need to maintain my directory structure in order to use the composer's autoloader and keep to PSR-0 directory structure.

有一个简单的方法来创建一个.phar和维护我的目录结构,作曲家的自动加载器?

推荐答案

我建议你看看 Composer的编译器 (最初由Fabien Potencier在Silex创建)。在这个类中,你可以看到一个大的控制台应用程序如Composer创建 .phar 文件。

一些有趣的部分:

// line 49
$phar = new \Phar($pharFile, 0, 'composer.phar');
$phar->setSignatureAlgorithm(\Phar::SHA1);

$phar->startBuffering();

Phar#startBuffering 开始创建phar文件。

Phar#startBuffering starts the creation of the phar file.

// Line 54
$finder = new Finder();
$finder->files()
    ->ignoreVCS(true)
    ->name('*.php')
    ->notName('Compiler.php')
    ->notName('ClassLoader.php')
    ->in(__DIR__.'/..')

在这里,Composer使用 Symfony2 Finder组件找到 src 目录中的每个文件(除了此文件和自动加载器)。

Here, Composer uses the Symfony2 Finder Component to find every file in the src directory (except this file and the autoloader).

// Line 63
foreach ($finder as $file) {
    $this->addFile($phar, $file);
}

这里,Composer遍历每个找到的文件,并将其添加到Phar归档中。 (您可以在 Compiler#addFile 方法。 php#L116-130> line 116 )。

Here, Composer iterates over every found file and adds it to the Phar archive. (you can see the Compiler#addFile method on line 116).

这一过程重复了几次。然后在第93行,使用Composer自动加载器:

This is repeated some times. And then at line 93, the Composer autoloader is used:

$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/autoload.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_namespaces.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_classmap.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_real.php'));

因为Phar是一个流,目录结构保存在phar文件中,Composer自动加载器仍然

Because Phar is a stream, the directory structure is kept in the phar file and the Composer autoloader is still able to load the classes.

然后,最后,添加存根并停止缓冲:

Then, at the end, the stubs are added and the buffering stopped:

$phar->setStub($this->getStub());

$phar->stopBuffering();

(参见 Compiler#getStub 第173行)。 Phar#stopBuffering 方法停止创建phar并将其保存

(see the Compiler#getStub method at line 173). The Phar#stopBuffering method stops the creation of the phar and saves it to the phar file.

为了完成这个故事,Composer创建了一个非常简单的CLI 编译文件,它运行此命令。

To make this story complete, Composer creates a very simple CLI compile file which runs this command.

这篇关于为简单应用程序生成PHAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆