简体   繁体   English

如何正确创建,然后需要PHAR文件?

[英]How do I correctly create, and then require a PHAR file?

I'm currently trying to package library code, which would then be shipped out to people actually trying to use that library code. 我目前正在尝试打包库代码,然后将其发送给实际尝试使用该库代码的人。 After creating the PHAR file, I'm trying to verify that it was done correctly with a simple test script. 创建PHAR文件后,我正在尝试使用简单的测试脚本验证它是否已正确完成。 At some point in the create-PHAR -> use-PHAR process, I'm doing something wrong. create-PHAR - > use-PHAR进程的某个时刻,我做错了什么。

How do I correctly create, and then require a PHAR file? 如何正确创建,然后需要PHAR文件?

To keep the making and validation of the PHAR easy, I've limited everything down to a simplified version of the problem, and still cannot proceed. 为了使PHAR的制作和验证变得简单,我将所有内容限制为问题的简化版本,但仍然无法继续。

Here's my files: 这是我的文件:

~/phar-creation-and-require-test/
    mylibrary.php
    testoflibrary.php
    make-phar.php
    make-phar.sh
    mylibrary.phar (after being created)

mylibrary.php contents: mylibrary.php内容:

<?
class FooClass {
    private $foonum;

    function FooClass() {
        $this->foonum = 42;
    }
}
?>

make-phar.php contents: make-phar.php内容:

<?php
if ($argc < 3) {
    print 'You must specify files to package!';
    exit(1);
}
$output_file = $argv[1];
$project_path = './';
$input_files = array_slice($argv, 2);

$phar = new Phar($output_file);
foreach ($input_files as &$input_file) {
    $phar->addFile($project_path, $input_file);
}

$phar->setDefaultStub('mylibrary.php');

Which is called by make-phar.sh : 这是由make-phar.sh调用的:

#!/usr/bin/env bash

rm mylibrary.phar
php --define phar.readonly=0 ./make-phar.php mylibrary.phar \
    phar-index.php

I can run make-phar.sh without any errors, and mylibrary.phar gets created. 我可以运行make-phar.sh而不会出现任何错误,并且会创建mylibrary.phar

The test script testoflibrary.php is thus: 因此测试脚本testoflibrary.php是:

<?php
require_once 'phar://' . __DIR__ . '/mylibrary.phar';  // This doesn't work.
//require_once 'mylibrary.php';  // This would work, if un-commented.

$foo = new FooClass();
print_r($foo);

When I run it with php testoflibrary.php , I get this error: 当我用php testoflibrary.php运行它时,我收到此错误:

Fatal error: Class 'FooClass' not found in /Users/myusername/phar-creation-and-require-test/testoflibrary.php on line 5

To get to this state, I've been reading the docs , this tutorial , and also this tutorial . 为了达到这种状态,我一直在阅读文档本教程以及本教程 This SO question does not seem to give the information I need, nor this question , and I can't seem to find any other relevant questions/answers here on SO. 这个SO问题似乎没有提供我需要的信息,也没有提供这个问题 ,而且我似乎无法在此找到任何其他相关问题/答案。

So, the question (again) is, 所以,问题(再次)是,

How do I correctly create, and then require a PHAR file? 如何正确创建,然后需要PHAR文件?

Adding files one at a time (even with a single file) will fail. 一次添加一个文件(即使只有一个文件)也会失败。 Just build the PHAR file from the whole directory containing all your library's source files, all at once. 只需从包含所有库源文件的整个目录中构建PHAR文件即可。

eg With your project structure like this: 例如,您的项目结构如下:

libraryproject/
    src/
        subfolder1/
            athing.php
            anotherthing.php
        athing.php
        anotherthing.php
        phar-index.php       (main file that imports the rest of the library)
    make-phar.sh
    make-phar.php
    mylibrary.phar           (after creation)
    test-the-lib-works.php

The make-phar.php script should be like this: make-phar.php脚本应该是这样的:

<?php
$phar = new Phar('mylibrary.phar');
$phar->buildFromDirectory('src/');  // This does the thing you actually want.
$phar->setDefaultStub('phar-index.php');

Then the make-phar.sh script is this: #!/usr/bin/env bash 然后make-phar.sh脚本是这样的:#!/ usr / bin / env bash

rm mylibrary.phar
php --define phar.readonly=0 ./make-phar.php

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

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