如何通过composer为PHP CLI提供脚本(独立和依赖) [英] How can I provide a script for PHP CLI via composer (as standalone, and as dependency)
问题描述
我正在尝试编写一个要从命令行运行的PHP脚本。我想使用composer来管理其依赖项,并使其可以作为对其他项目的依赖项进行安装。我还想保持自己(具有依赖项)使用它的能力。
I am attempting to write a PHP script that I would like to run from the command line. I want to use composer to manage its dependencies, as well as to make it available to be installed as a dependency to other projects. I also want to maintain the ability to use it on its own (with its dependencies).
当前, main.php
是我的入口点(我将从命令行执行的内容)。在构建和测试它时,我是按照独立的心态进行的。这样的 main.php
需要像这样的自动加载:
Currently, main.php
is my "entry point" (what I would execute from the command line). As I built and tested it, I did so in the "stand alone" mindset. A such, main.php
requires autoload like so:
<?php
require_once __DIR__.'/../vendor/autoload.php';
继续保持独立的心态,我将其设置为:
Continuing in stand alone mindset, I set it up like so:
-
git clone
打包到位 -
cd软件包
-
作曲者安装
git clone
package into placecd package
composer install
这将产生以下目录设置
This produces the following directory setup"
package
|-composer.json
|
|-src
| |-ClassOne.php
| |
| |-ClassTwo.php
| |
| |-main.php
|
|-vendor
|-autoload.php
|
|-composer
|
|-3rdpaty_1
|
|-3rdpaty_2
这很好-我可以运行 php src / main.php
能够找到所需的类,因为它加载 __ DIR __。'.. / vendor / autoload.php'
。
This works well--I can run php src/main.php
which is able to find the classes it needs because it loads __DIR__.'../vendor/autoload.php'
.
我遇到麻烦的地方是当我想将软件包安装为对另一个专家的依赖时ject(以使脚本可在此处运行)。我将软件包添加到 composer.json
并安装了它。尝试运行 php vendor / compnay / package / src / main.php
失败,因为必要的 autoload.php
在另一个位置:
Where I run in to trouble is when I want to install the package as a dependency to another project (in order to have the script available to run there). I add my package to the composer.json
and it gets installed. Attempting to run php vendor/compnay/package/src/main.php
fails, however, because the necessary autoload.php
is in a different place:
dependent_project
|-composer.json
|
|-some_code
|
|-vendor
|-autoload.php
|
|-composer
|
|-company
| |-package
| |-composer.json
| |
| |-src
| |-ClassOne.php
| |
| |-ClassTwo.php
| |
| |-main.php
|
|-other_vendor_1
|
|-other_vendor_2
我知道出了什么问题,但是我不确定如何解决它。如何使用作曲家提供这样的东西?我搜索了很多东西,但是看不到有人问或回答相同的问题。我注意到 composer.json
的 bin
属性,并开始研究这个想法,但是我仍然找不到有关如何正确设置脚本以在不同上下文中找到所需内容的大量信息。
I understand what is wrong, but I am not sure how to fix it. How does one provide such a thing using composer? I've searched around a lot but I don't see anyone asking or answering the same question. I notice the bin
property of composer.json
and started looking into that idea, but I'm still not finding a lot of info on how to properly set up my script to find what it needs in the different contexts.
我考虑过尝试使用 include
if
中的code>,并在失败的另一条路径上运行第二个 include
,但这不会
I considered trying to use include
in an if
, and running a second include
to the other path on fail, but that doesn't seem like the proper way to go about it.
推荐答案
一种常见的做法是查看两个位置的自动加载文件。例如,请参阅 Behat :
A common practice is to look into both locations for an autoload file. See for instance this snippet used by Behat:
function includeIfExists($file)
{
if (file_exists($file)) {
return include $file;
}
}
if ((!$loader = includeIfExists(__DIR__.'/../vendor/autoload.php')) && (!$loader = includeIfExists(__DIR__.'/../../../autoload.php'))) {
fwrite(STDERR,
'You must set up the project dependencies, run the following commands:'.PHP_EOL.
'curl -s http://getcomposer.org/installer | php'.PHP_EOL.
'php composer.phar install'.PHP_EOL
);
exit(1);
}
这篇关于如何通过composer为PHP CLI提供脚本(独立和依赖)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!