MeuMySQL Blog is now at http://blog.mdnsolutions.com/.

Friday, January 4, 2013

Creating your own Composer Package

Hi folks! I'd like to talk about to create your own dependency for Composer in this article. It's been one of the best invented tools for PHP ever in my humble opinion. Basically, you can manager your application's dependencies in a really easy way. If you haven't heard about it please check out the Compose website.

In this article I shall you show to create your own dependency using Github, deploy and maintain using Packagist.



Github

To start with, you need a account in any version system, such as Github, SVN or any other. I've used for this article Github, if you don't have a account there or don't know how to create a repository I recommend to you read about it here.

In my account at Github I've created a new repository called "mdn-sample" to be my I first package for Composer.



Now let's create 2 files into our new repository. First one is gonna be called packages.json and this one is very important to provider informations about our package. Into this one you are going to write something like that:

{
    "packages": {
        "name": "medinadato/mdn-sample",
        "version": "1.0.0",
        "dist": {
          "url": "https://github.com/medinadato/mdn-sample",
          "type": "zip"
},

"source": {
            "url": "https://github.com/medinadato/mdn-sample",
            "type": "git",
            "reference": ""

        }
    }
}

As you can see, it has information about version, source, name of the package and other informations.

The second file is the composer.json, this one gives information about other vendors your package  depends to. In our case here I'm justing adding one package "doctrine/common" as an example.

 {
    "name": "medinadato/mdn-sample",
    "description": "MDN sample",
    "require": {
        "doctrine/common": "*"
    },
    "minimum-stability": "dev"
}



Packagist

That is it. Your package is ready to go. All you need to do is register it in a Composer Package Repository such as Packagist. To do so, you have to create a account over there and Submit a new Package as in the image below:



Once you click on the button Submit Package you are going to see one screen as below. Type then the Github path for your new repository and the Packagist will add it into your package list.



Running the new package

At this point you should have your package available to be imported by the composer in your machine. To check it out, create in your computer a folder and into that a file called composer.json with the following content:

{
    "require": {
        "medinadato/mdn-sample": "dev-master"
    },
    "minimum-stability": "dev"
}


Go into the folder you've created and run de code bellow:

$ cd /path/your_folder
$ composer.phar install

Now the composer is look for your package and all its dependencies and then installing them.

$ composer.phar install
Loading composer repositories with package information
Installing dependencies
  - Installing doctrine/common (dev-master a890fe1)
    Cloning a890fe1b689ed65c52bf4a7e64333a27b3c400d6

  - Installing medinadato/mdn-sample (dev-master 4bc96b3)
    Cloning 4bc96b3ba8bb7957b3e9d0aca2316ef28650489e

Writing lock file
Generating autoload files

And in your directory you will have something like this:



That is it. I hope it is handy for you as it's been for my projects. See ya!




References:

http://getcomposer.org/
https://packagist.org/
https://github.com/medinadato/mdn-sample

No comments:

Post a Comment