I needed to link a GitHub hosted library into my project with composer. It was my own fork of a library that was abandoned. I didn’t want to register it with packagist.com because it already is infested with weeds of this kind. Conveniently, the library already had composer.json so I just needed to edit my project’s composer.json and link to the library. Easy I thought.
-
{
-
…
-
"repositories": [
-
{
-
"type": "package",
-
"package": {
-
"name": "xpavp03/nette-csv-response",
-
"version": "dev-patch-1",
-
"source": {
-
"type": "git",
-
"url": "https://github.com/xpavp03/nette-csv-response.git",
-
"reference": "origin/patch-1"
-
}
-
}
-
}
-
],
-
"require": {
-
…
-
"xpavp03/nette-csv-response": "dev-patch-1"
-
}
-
…
-
}
-
Well, it did download the files but it also created a Git repository in my vendors structure. I could work around it by excluding it during deployment but I wanted to see if I can fetch the library without it.
-
"repositories": [
-
{
-
"type": "package",
-
"package": {
-
"name": "xpavp03/nette-csv-response",
-
"version": "dev-patch-1",
-
"dist": {
-
"type": "zip",
-
"url": "https://github.com/xpavp03/nette-csv-response/archive/patch-1.zip"
-
}
-
}
-
}
-
],
-
Worked great but, composer didn’t include this library in its autoloading files. Mhm…
It turned out that if you link a library as a package composer won’t look for its composer.json. It didn’t matter that it existed and had instructions for autoloading – composer didn’t expect it there and didn’t try looking. So the solution was to include autoloading instructions into my project’s composer.json:
-
"repositories": [
-
{
-
"type": "package",
-
"package": {
-
"name": "xpavp03/nette-csv-response",
-
"version": "dev-patch-1",
-
"dist": {
-
"type": "zip",
-
"url": "https://github.com/xpavp03/nette-csv-response/archive/patch-1.zip"
-
}
-
"autoload": {
-
"classmap": ["src/"]
-
}
-
}
-
}
-
],
-
I wish this was mentioned in the documentation.
Thank you for this. I was searching for a while to figure out why my package wasn’t registering in the autoloader. Setting the autoload in the respository did it. Strange that it doesn’t just read the package’s composer.json.