Composer option to use composer.json alternative?

I just started using the Composer function, where you tell it to look at local directories for dependencies, so you can develop a library and something that uses this library in parallel, without having to click on git to update all the time, which is awesome . eg.

"repositories": [ { "type": "vcs", "url": "/documents/projects/github/guzzle" } ], "require":{ "guzzle/guzzle": "3.7.*@dev" } 

So, when you do an update for the composer, Composer will pull the version of Guzzle from the local directory, so you can test the code for the library in another application that uses this library, without having to insert a change between each code in the repository.

However, I just almost checked in composer.json for my project with this set, which obviously will not work on any elses machine.

In any case, to tell the composer to use a different file than composer.json, or in another way, so that you can tell the composer to use local directories safely, without the high probability of accidentally executing a broken version of composer.json for your repository?

+6
source share
5 answers

Use the COMPOSER environment variable:

 env COMPOSER=composer-dev.json composer install 

In fact, it has been available since at least 2012 .

+5
source

Instead of fetching from the local repository elsewhere, you can add the --prefer-source option to the composer install/update command and remove the local repository link.

Thus, the composer will cause git to clone the software into the vendor directory, and you will be able to develop your software and commit the vendor software, because it is also a fully working git repo.

Adding links to local repositories is not recommended. It works when used for real local software, but it has overhead: you have to mention this repository in every composer.json file that will load this software, even if it is only an indirect dependency (i.e. you add software software that needs this software as a dependency in your local repo).

Hardcoding the repository URL will also prevent you from changing it as you wish. Despite the fact that you can move the repo and change the URL accordingly, all old versions of your software still have the old URL in the composer.json and composer.lock files and will try to load from there.

+2
source

There seems to be no way to do this in Composer, but it can be hacked.

In your composer.json file, put a comment where you want to hack some data.

 { "name": "base-reality/intahwebz", "//": "LOCALHACK", "require":{ "base-reality/php-to-javascript": ">=0.1.17", "guzzle/danackguzzle": "3.3.*@dev", ... ... } ... } 

Then add a separate composer.local file (not tied to Git) that contains links to local directories:

 "LOCALHACK", "repositories": [ { "type": "vcs", "url": "/documents/projects/github/intahwebz-core" } ], 

Add a tiny PHP script called composerLocal.php to generate a new composer.json file

 <?php $srcFile = file_get_contents("composer.json"); $hackFile = file_get_contents("composer.local"); $finalString = str_replace('"LOCALHACK",', $hackFile, $srcFile); file_put_contents("composer.json", $finalString); ?> 

And a little bash script called localupdate.sh to backup the real composer.json file, generate a hacked composer.json, run Composer and then restore the original composer.json file

 cp -f composer.json composer.json.bak php composerLocal.php composer update cp -f composer.json.bak composer.json 

Running the localupdate.sh script allows you to test commits locally without jeopardizing a change to the actual composer.json file used by the project, so there is a chance of accidentally clicking an invalid .json composer in the repository.

To notice, Composer does not read files from the repository directory, it reads the registered files in Git, so you need to commit the changes made to the library code. The above process simply skips the click step.

0
source

This should also work:

  composer config --file=composer2.json && composer install 

see https://getcomposer.org/doc/03-cli.md#usage

0
source

Easy, just use the artifact.

In the repositories add this:

 { "type": "artifact", "url": "path/to/artifact/files/" }, 

Now you just need to create a directory and secure a copy of your repository in this directory.

Name the zip files as follows:

 [vendorname]-[packagename]-[version].zip 

example:

 querypath-QueryPath-3.0.0.zip 

Now you can change the package locally and pull it out of the zip file instead of the online repo.

In the request, add it like this and specify the version defined in zip:

 "querypath/QueryPath": "3.0.0", 

With this method, you will have the opportunity to edit the supplier files, and the composer will still update any autoloaders regarding the changes, and he will leave your changes alone.

-1
source

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


All Articles