Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to develop and include a Composer package?

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.

like image 710
greggilbert Avatar asked Jan 12 '13 16:01

greggilbert


People also ask

What is a composer package?

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.

How do I add composer to my project?

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.

Where does composer get packages from?

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.


1 Answers

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.

Read more

  • PSR [petermoulding.com] (via archive.org)
  • PSR-4 autoloading support in Composer [seld.be]
  • Battle of the Autoloaders: PSR-0 vs. PSR-4 [Sitepoint]
  • Why use a PSR-0 or PSR-4 autoload in composer if classmap is actually faster? [SO]

You can Google it if you need more info on PSR-0, PSR-4 and Classmap.

Example

"autoload": {    "psr-4": { "Core\\": "src/Core" }  ## "standard": { "namespace" : "path/to/dir"} } 

Or (edit)

If you actually want to add a local package:

  1. 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.

  2. Zip the package, with the composer.json file as the root file in the archive.zip, and place it where desired.

  3. 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?":

  1. 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.

  2. Upload it to Github and tag the version. Use Semantic versioning (make sure you exclude/ignore the vendor directory when uploading to Github).

  3. 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.

like image 75
Shalom Sam Avatar answered Sep 19 '22 08:09

Shalom Sam