Matok's PHP Blog

Use own library using composer

article image

In previous article we built our reusable library, so let's adopt it.

Read related article:
Building reusable library with Composer
How to use composer to build own library? Squeeze maximum from code reusability.

Project preparation

Just to demonstrate basic principles: I will clone Symnofy Standard Edition project

$ git clone https://github.com/symfony/symfony-standard.git
Cloning into 'symfony-standard'...
remote: Counting objects: 6941, done.
Receiving objects:  99% (6872/6941), 1.56 Mremote: Total 6941 (delta 0), reused 0 (delta 0), pack-reused 6940
Receiving objects: 100% (6941/6941), 1.64 MiB | 754.00 KiB/s, done.
Resolving deltas: 100% (4005/4005), done.

Require library

Requiring our library is exactly the same as requiring any other library. If I type in project directory composer require matok/bbcode-parser @dev1 it will probably2 end up with error. The reason of error is composer don't find anything named like matok/bbcode-parser. We need a push composer a little bit.

I'm using bitbucket and all I need is provide url to my repository in composer.json.

"repositories": [
    {
        "type": "vcs",
        "url":  "https://Matok2@bitbucket.org/Matok2/bbcode-parser.git"
    }
],

Now composer require matok/bbcode-parser @dev do the job. Common mistake is requiring url name. It doesn't matter what is the url - important is name in composer.json in library project. That's everything. If you go to vendor directory library will be there.

Next time - how to tune up this a little bit.

Removing / Uninstalling library

If you want play with composer maybe you need to remove previously installed library. To remove (uninstall) library use command composer remove matok/bbcode-parser


Footnotes:

1: I don't have stable version therefore I must declare @dev
2: sooner or later exactly this library will be free to use and you don't end up with error


If you like this article then mark it as helpful to let others know it's worth to read. Otherwise leave me a feedback/comment and we can talk about it.

I'm foreigner. Where I live my friends call me Maťok.


Comments - Coming Soon