How to configure composer package for global installation?

Update:

I really posted this package so you can see the problem yourself.

Look here naomik / yamldump


I am trying to make a small CLI tool and pack it with the composer.

The following is an extremely simplified version of the program, but this is enough to demonstrate the problem I am facing.

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 

Files are installed on

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

This is a problem because I was not going to install symfony/yaml and symfony/yaml . My vendor/autoload.php package can no longer find the Yaml package in the right place.

I do not mind that symfony/yaml was installed globally, but it would be reasonable for me that composer global require install the package as follows:

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

In the end, what if I have Package A , which depends on symfony/yaml=2.5.3 and Package B , which requires symfony/yaml=2.6.x ?

If composer global require sets ~/.composer/vendor/* dependencies, each globally required package cannot support its own version of its dependency requirements ...

I know this is a kind of confusing problem, but I really don't know how to start fixing it.


goal

User must be able to

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

Error

 $ 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 

Question

Here it is black and white:

How do I plan to write the string require "vendor/autoload.php" and does it work with both locally installed packages and globally installed packages?

+6
source share
1 answer

Targeting vendor/autoload.php usually not a good idea and only works when running a script from the correct directory. The following should serve you better:

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

However, this can be a problem if your application is installed as a dependency. In this case, you may 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); } 

First of all, search for the autoloader in the place that you expect if you are working directly with your application. If this does not exist, it will look for where the autoloader will be if your application is installed as a dependency.

+4
source

Source: https://habr.com/ru/post/974191/


All Articles