I'm looking to develop a package in PHP, but I don't want it immediately available on GitHub or somewhere. It's easy enough to include a Packagist file in my composer.json
, but how do I add a local package into my composer.json
? Also, should I be building the package in /vendor/foo/bar
(relative to the root composer.json
), or should I put it somewhere else?
Edit: I guess my question is about how everyone else writes their packages. Does every new package get added to Packagist, and then when you want to test your changes, you commit to GitHub (or wherever), and then pull that back down via Composer? That seems really inefficient.
Composer is a dependency manager. It installs packages locally. A package is essentially a directory containing something. In this case it is PHP code, but in theory it could be anything. And it contains a package description which has a name and a version.
Installation - Windows# This is the easiest way to get Composer set up on your machine. Download and run Composer-Setup.exe. It will install the latest Composer version and set up your PATH so that you can call composer from any directory in your command line. Note: Close your current terminal.
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.
As this question has many different components/standards to be explained, I'll try to explain as much as possible here and you can PM me or just Google it for more specific questions as they come up.
To answer your first question, "How do I add a local package into my composer.json
?":
If by "add local package" you mean autoload your class/package, you can do that by either using PSR-4 or PSR-0 or Classmap options in composer.
You can Google it if you need more info on PSR-0, PSR-4 and Classmap.
"autoload": { "psr-4": { "Core\\": "src/Core" } ## "standard": { "namespace" : "path/to/dir"} }
If you actually want to add a local package:
Create a composer.json
for the local package, like:
{ "name": "localPackage/core", "version": "dev-master" }
You can also specify other properties and/or dependencies as required.
Zip the package, with the composer.json
file as the root file in the archive.zip
, and place it where desired.
In the other project/package where you want to include the local package, add the local package name to the required parameter, like
"localPackage/core": "dev-master"
Add the following under the repositories
parameter:
"repositories" : [ { "type": "artifact", "url": "path/to/localPackage.zip" } ]
Now if you have the local package on git, then there would be no need to archive the package (basically omit step 2) and you just need to replace the URL in the above example to the path/to/localPackage/.git
.
(End of edit)
Now to answer the larger question: "How do I develop and include a Composer package?":
Decide the directory structure. Commonly it is as follows:
/PackageRoot /src/PackageCore composer.json ## this is your library’s composer.json LICENSE
and set up your composer.json
.
An example of one of mine composer.json
files can be found at http://pastebin.com/tyHT01Xg.
Upload it to Github and tag the version. Use Semantic versioning (make sure you exclude/ignore the vendor
directory when uploading to Github).
Register the package with Packagist (after logging in).
If you have tagged your commit as v1.0.0
(or similar), then this will show up in your Packagist dashboard for that package.
Now, if everything is done right, you should be able to use your library as a dependency in other projects by adding it to the composer.json
of that project.
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