Create first Composer Package

Summary

By now most of the PHP developers with some experience know what Composer is why everyone is using it. If not, Composer is a dependency package manager of the project. In simple terms I can say, Composer:

  1. Easily installs 3rd party libraries
  2. Autoload Library Classes in the project
  3. Easily upgrade 3rd party libraries without breaking the code
  4. Reduces the project size(Good for distributing projects)

Here we will see how to create a package and include this new package into one project.

Creating Package

I will create a simple SayHello Class with one method get(), which will return ‘Hello World!’ . I will keep that class in helloworld/src/HelloWorld/SayHello.php

<?php
namespace HelloWorld;
class SayHello
{
   public static function get()
   {
       return 'Hello World!';
   }
}

Setting Up Composer

Note: If you still don’t have composer installed then you can do so from here.

Create and Update composer.json

> composer init

Follow the interactive steps

Once the composer.json is generated, modify the composer.json to look like this

{
    "name""jigarakatidus/hello-world",
    "description""This is test package",
    "type""library",
    "license""MIT",
    "authors": [
        {
            "name""Jigar Dhulla",
            "email""jigar.tidus@gmail.com"
        }
    ],
    "minimum-stability""dev",
    "require": {
        "php"">=7.1.0"
    },
    "autoload": {
        "psr-0": {
            "HelloWorld""src/"
        }
    }
}

“autoload” will require all the files under src/ according to PSR-0

Testing the Package

To test, we will be using this same project. Composer files are required so we will run

> composer install

This will generate the autload.php and related composer files. Also if your package as any dependencies that you may have mentioned in composer.json then that will appear in vendors directory.

Create a test.php in your projects root directory and paste following content

<?php
require_once __DIR__ . '/vendor/autoload.php'// Autoload files using Composer autoload
use HelloWorld\SayHello;
echo SayHello::get();

Finally, run the following command in the terminal to test

> php test.php

Output should be

> php test.php
Hello World!

Submitting to packagist

Let’s push our code to Github.

Note: Do not commit and push vendors directory.
git init
git add .
git commit -m "First commit"
git remote add origin git@github.com:username/hello-world.git
git push origin master

Go to Packagist website and Register yourself. Then submit your project by providing your github link

git@github.com:username/hello-world.git

My listing can be found here: https://packagist.org/packages/jigarakatidus/hello-world

More to come (hopefully)…

Next, I will try to write about how to setup a Github Hooks to automatically update the packages on Packagist.

References

https://packagist.org/

http://getcomposer.org/

https://blog.jgrossi.com/2013/creating-your-first-composer-packagist-package/

Leave a comment