如何配置Composer软件包以进行全局安装? [英] How to configure a composer package to be globally installed?

查看:320
本文介绍了如何配置Composer软件包以进行全局安装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

我实际上已经发布了此软件包,以便您可以亲自了解问题.

I actually published this package so you can see the problem for yourself.

在此处查看 naomik/yamldump

我正在尝试制作一个小的CLI工具,并将其与composer打包在一起.

I'm trying to make a little CLI tool and package it up with composer.

下面是该程序的极为简化的版本,但足以说明我遇到的问题.

Below is an extremely simplified version of the program, but it's enough to demonstrate the problem I'm encountering.

该项目具有一个依赖关系和一个二进制"文件

The project has one dependency, and one "binary" file

composer.json

{
  "name": "naomik/yamldump",
  "version": "0.2.0",
  "bin": [
    "bin/yamldump"
  ],
  "require": {
    "symfony/yaml": "2.5.3"
  }
}

bin/yamldump

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

// use Yaml namespace
use Symfony\Component\Yaml as Yaml;

// autoload
require_once "vendor/autoload.php";

// read yaml
$yaml = file_get_contents(sprintf("%s/%s", getcwd(), $argv[1]));

// create parser
$parser = new Yaml\Parser();

// parse the yaml
var_dump($parser->parse($yaml));


因此,当我在全球范围内安装它时,我会得到


So when I install it globally, I get this

$ composer global require naomik/yamldump=dev-master

文件已安装到

  • ~/.composer/vendor/bin/yamldump -> ../naomik/yamldump/bin/yamldump
  • ~/.composer/vendor/naomik/yamldump/
  • ~/.composer/vendor/symfony/yaml/
  • ~/.composer/vendor/bin/yamldump -> ../naomik/yamldump/bin/yamldump
  • ~/.composer/vendor/naomik/yamldump/
  • ~/.composer/vendor/symfony/yaml/

这是一个问题,因为我不打算全局安装symfony/yaml ,而且我的软件包的vendor/autoload.php不能再在正确的位置找到Yaml软件包.

This is a problem, because I did not intend to globally install symfony/yaml and my package's vendor/autoload.php can no longer find the Yaml package in the proper location.

我不 全局安装symfony/yaml,但是composer global require这样安装软件包对我来说很有意义:

I don't mind that symfony/yaml was installed globally, but it would make sense to me that composer global require would install the package like this:

  • ~/.composer/vendor/bin/yamldump -> ../naomik/yamldump/bin/yamldump
  • ~/.composer/vendor/naomik/yamldump/
  • ~/.composer/vendor/naomik/yamldump/vendor/symfony/yaml/
  • ~/.composer/vendor/bin/yamldump -> ../naomik/yamldump/bin/yamldump
  • ~/.composer/vendor/naomik/yamldump/
  • ~/.composer/vendor/naomik/yamldump/vendor/symfony/yaml/

毕竟,如果我有依赖于symfony/yaml=2.5.3Package A和需要symfony/yaml=2.6.xPackage B怎么办?

After all, what if I have Package A that depends on symfony/yaml=2.5.3 and Package B that requires symfony/yaml=2.6.x?

如果composer global require将依赖项安装到~/.composer/vendor/*,则每个全局所需的软件包都无法维护其自身对依赖项的版本要求...

If the composer global require installs dependencies to ~/.composer/vendor/*, each globally required package can't maintain it's own version requirement of its dependency...

我知道这是一个复杂的问题,但是我真的不知道如何开始解决它.

I know this is sort of a convoluted problem, but I really don't know how to begin fixing it.

目标

用户应该能够

$ composer global require naomik/yamldump=dev-master
$ yamldump sample.yml

错误

$ yamldump sample.yml
Warning: require_once(vendor/autoload.php): failed to open stream: No such file or directory in /Users/naomik/.composer/vendor/naomik/yamldump/bin/yamldump on line 8

Fatal error: require_once(): Failed opening required 'vendor/autoload.php' (include_path='.:') in /Users/naomik/.composer/vendor/naomik/yamldump/bin/yamldump on line 8

问题

这里是黑色&白色:

Here it is in black & white:

我打算如何编写require "vendor/autoload.php"行并将其用于本地安装的软件包和全局安装的软件包?

How am I intended to write the require "vendor/autoload.php" line and have it work for both locally installed packages and globally installed packages?

推荐答案

定位vendor/autoload.php通常不是一个好主意,并且仅当您从正确的目录运行脚本时才有效.以下内容将为您提供更好的服务:

Targeting vendor/autoload.php is generally not a good idea and only works if you run the script from the correct directory. The following should serve you better:

require_once __DIR__.'/../vendor/autoload.php';

但是,如果您的应用程序是作为依赖项安装的,则这可能仍然是一个问题.在这种情况下,您可能需要一些更重要的东西:

However, this still might be an issue if your application is installed as a dependency. In that case, you might need something more substantial:

if (
    (!$classLoader = includeIfExists(__DIR__.'/../vendor/autoload.php')) &&
    (!$classLoader = includeIfExists(__DIR__.'/../../../autoload.php'))
) {
    echo 'You must set up the project dependencies, run the following commands:'.PHP_EOL.
        'curl -sS https://getcomposer.org/installer | php'.PHP_EOL.
        'php composer.phar install'.PHP_EOL;
    exit(1);
}

这将首先在您直接在应用程序上工作的位置查找自动装带器.如果不存在,则将您的应用程序作为依赖项安装时,它看起来将位于自动加载器的位置.

This first looks for the autoloader in the location you would expect it to be if you are working directly on your application. If that does not exist, it looks where the autoloader would be if your application is installed as a dependency.

这篇关于如何配置Composer软件包以进行全局安装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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