I am a Composer beginner and I am trying to make one project dependent of another one, while both project only exist on my local machine.
The composer.json in my library project (ProjectA) is:
{ "name" : "project/util", "type" : "library" }
I initialized git in the base folder of this project.
My composer.json in the project depending on the first one (ProjectB):
{ "repositories": [ { "name" : "util", "type" : "git", "url" : "/d/workspaces/util" } ], "require": { "project/util" : "*" }, }
When I run composer install
from ProjectB, I get the following error:
[RuntimeException] Failed to clone , could not read packages from it fatal: repository '' does not exist
I asume something is wrong with the url of the repository, but I am not sure what else to write there.
You can add more repositories to your project by declaring them in composer. json . Repositories are only available to the root package and the repositories defined in your dependencies will not be loaded.
Composer downloads directly from the source, e.g. Packagist only knows those source and tells your composer instance where to go. It does this by downloading a bunch of json files from Packagist.org that have all the infos.
If we were to run composer install in the context of this file, composer would look for a project at the defined URL, and if that URL represents a Git repo that contains a composer. json file that defines its name and type, composer will download that package to our project and place it in the appropriate location.
A repository is a VCS term which describes when VCS is tracking a filesystem. In the scope of individual source code files, a VCS will track additions, deletions, modifications of the lines of text within that file. Popular software industry VCS options include Git, Mercurial, SVN and preforce.
Autoload local package using composer (without going to packagist every time you change).
There are many ways to do so, I will be covering 2 of them:
In all cases we have 2 main parties:
- the local package (the code that we do not want to publish on packagist to be able to autoload it in our project composer).
- the main project (the code base that needs to use the local package code, can be another package and or any project).
Method 1: (direct namespace)
Open the main project composer.json
file and autoload the local package namespaces using any method (PSR-4, PSR-0, ...).
example:
if in the composer.json of the local package we have:
"autoload": { "psr-4": { “Local\\Pack\\": "library" } }, "autoload-dev": { "psr-4": { "Local\\Pack\\Tests\\": "tests" } },
then in the composer.json of the main project we should have:
"autoload": { "psr-4": { "Mahmoudz\\Project\\": "src", "Local\\Pack\\": "../path/to/local/pack/library” << referencing the other local package } }, "autoload-dev": { "psr-4": { "Mahmoudz\\Project\\Tests\\": "tests" } },
Advantages:
- you don’t touche the vendor directory (running composer update by mistake will not override your local changes)
- you don’t need your package to be on packagist to use it
- you work in one place (the local package) and the changes are automatically loaded in the main project
Disadvantages:
- you cannot publish the composer.json on production (needs editing before publishing to require the real package)
Method 2: (local repository)
Download the local package from a local repository.
local package:
1. initialize git in the package (even if you don’t want to use it - no need to commit anything)
2. add composer.json file. In the file make sure you have the following:
"name": "vendor-name/package-name", "autoload": { … // use whichever method you prefer, but make sure it’s being loaded correctly "minimum-stability": "dev"
composer dump-autoload
main project:
1. edit your composer.json to contain the following:
"repositories": [ { "type": "vcs", "url": "/full/path/to/the/local/package/package-name" } ], "require": { "vendor-name/package-name": "dev-master" },
NOTE: whenever you make change in the local package (not the vendor) you need to git commit then you can composer update the main project, it will get the latest copy of the repo to the main project vendor directory.
Advantage:
- you don’t touch the vendor directory (running composer update by mistake will not override your local changes) - you don’t need your package to be on packagist to use it
Disadvantage:
- you have to keep committing your changes (in the local package) and then running composer update in the main project
- you cannot publish the composer.json on production (needs editing before publishing to require the real package)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With