Composer is the great tool for developers to manage the PHP library that used in our PHP project.

Its just working like apt-get in ubuntu, brew in mac, yum in centos, npm in node and so on.

So we can easily to include or exclude any PHP library we need, by just including one file called autoload.php that generated by composer it self, we can use our library as well. We don’t to bother thinking about what version of libraries we used that compatible or not, do upgrading package to latest version when available, and soon which quite troublesome if we do it manually.

So composer helping to transform complexity into something simple.

Ok, after knowing summarise of composer, now lets make our local repository for composer.

Imagine you will start your first open source project, but still in development stage which is mean still in your local computer.

So you must make it composer-based first, when this project is ready to launch you can send it to packagist.org to be installable by everyone.

Lets Make It!

I supposed you have composer installed, if no just check this out to install it on your computer:

https://getcomposer.org/download/

Create directory for your project

$ mkdir ~/banana-project

Go to banana-project

$ cd ~/banana-project

For example I have this directory structure.

src

/Hello.php

Here is content of src/Hello.php :

<?php // Hello.php

namespace BananaProject; class Hello { public function say() {

echo "Hai I'm Learning Composer!!!!";

} }

Then do composer init . Composer will give you some question related to the project you are creating. And at the end it will create you composer.json file, and here is sample of my composer.json :

{

"name": "didinahmadi/hello",

"description": "first step before making oss project",

"authors": [

{

"name": "Didin Ahmadi",

"email": "didinonpqcms@gmail.com"

}

],

"minimum-stability": "dev",

"require": {}

}

Modify composer.json above to be like this below:

{

"name": "didinahmadi/hello",

"description": "first step before making oss project",

"authors": [

{

"name": "Didin Ahmadi",

"email": "didinonpqcms@gmail.com"

}

],

+ "autoload": {

+ "psr-4": {

+ "BananaProject\\": "src/"

+ }

+ },

"minimum-stability": "dev",

"require": {}

}

We’re telling to composer that where the path of my banana project should be find.

We are using PSR-4 as autoloading standard, at least this is currently recommended by composer owner today, but you can use other autoload way like PSR-0 , classmap and files as defined here.

Ok, Next step is we should make our project versioned, I prefer using git , so you should give it a try.

Make sure git installed on your computer, or you can find it here.

$ cd ~/banana-project

$ git init

$ git add .

$ git commit -m "Initialize commit"

Then we have done make our project to be composer-able.

Let’s Verify It!

Great, so far you following this article, now lets pretend that we will start a project who will use banana-project inside.

$ mkdir ~/monkey-project

$ cd ~/monkey-project

$ touch composer.json

then fill composer.json like code below:

{

"name": "didinahmadi/monkey-project",

"description": "Sample project who will use banana-project",

+ "require": {

+ "didinahmadi/hello": "dev-master"

+ },

+ "repositories": [

+ {

+ "name": "didinahmadi/hello",

+ "type": "git",

+ "url": "/Users/didinahmadi/banana-project"

+ }

+ ],

"authors": [

{

"name": "Didin Ahmadi",

"email": "didinonpqcms@gmail.com"

}

]

}

Make sure you define on require statement to define correct package of banana-project . And also on repositories part, you should define where banana-path location are correctly. In my case this located at /Users/didinahmadi/banana-project

Then you can run command

composer install

Now, take a cup of coffee because we are almost done and just need to wait until composer doing magic for us :)

Final Test

All right, now lets do final test by including banana-project inside monkey-project .

cd ~/monkey-project

touch index.php

Fill in index.php with this content below:

<?php

include __DIR__ . '/vendor/autoload.php'; use BananaProject\Hello;

$hello = new Hello; echo '<h1 style="text-align:center;">';

$hello->say();

echo '</h1>';

Then run php built-in server to get fast result.

$ cd ~/monkey-project

$ php -S localhost:1111

Finally open up http://localhost:1111 to your browser

Yeyeyeyeyeey I made it, how about you???

Source:

banana project source

monkey project source