Development

How to create a Custom Composer Package from scratch

What is Composer?

Composer is a dependency manager for PHP. It will manage the dependencies you require on a project-by-project basis, this means that Composer will pull in all the required libraries and dependencies to manage all of them in one place.

Table of contents

You are probably asking yourself how this can give you some benefits, well, just imagine that you are working on a project and writing a couple of classes to handle certain actions. Time after, you start a new project where you need to reuse those features, so you use the traditional copy-paste, time that helps you optimize. At this point, you have two versions of the same functionalities, so you copy the new version and replace it in the first project.

Suppose that instead of 2 projects there are 20, it’s crazy to think that this rudimentary process must be performed every time the code is adjusted to work. This is where the idea of ​creating a package that encompasses these functionalities is the best option. It can be versioned to be used according to the characteristics of the project and giving a greater support facility.

Creating a composer package means that only the code in a repository and projects can be updated with a few simple steps, and even more, the entire community could have access to it and use it in thousands of projects.

In this blog, you’ll learn how to create a custom package using composer to consume the GitHub API. In order to create a custom composer package you need to create the folder structure, for example:

|- vendor/name
  |- src
    |- Contracts
    |- Models
    |- Providers
  |- tests
  |- vendor

After creating the base structure, in the terminal type:

git init #initialize git tracking
composer init #this command will guide you through creating your composer.json file

The composer.json file configuration

1. Set the package name vendor/name, in this case: agusrdz/github-api.
2. Add a package description: This is a demo package to use the API of Github.
3. Add an author: Your name your@email.com.
4. Add minimum stability for the package: dev.
5. Define your dependencies (require) for production manually instead interactively.
6. Define your dependencies (require-dev) for development manually instead interactively.
7. Confirm the generation of composer.json file.
8. Confirm if you want the vendor directory added to your .gitignore file.
9. Now you only need to add every required dependency to use the package.
10. After editing the composer.json file run composer install on your terminal.

<script>{  "name": "agusrdz/github-api",    "description": "This is a demo package to use the API of Github.",    "type": "package",    "license": "MIT",    "authors": [      {          "name": "Agustin Espinoza",          "email": "agustinurdz_@hotmail.com"        }    ],    "minimum-stability": "dev",    "require": {      "php": ">=5.6",        "guzzlehttp/guzzle": "^6.1",        "jenssegers/model": "^1.1"    },    "require-dev": {      "phpunit/phpunit": "~5.0",        "phpunit/phpunit-mock-objects": "~3.0",        "illuminate/support": "^5.3"   }}</script>

That’s it!

Well, it is almost all what we need… basically, this is the composer.json required to create a composer package but we need to add some more lines to define the parameters as autoload, unit test and preferred stability; so go to composer.json and add this after required-dev.

"require-dev": {
  "phpunit/phpunit": "~5.0",
    "phpunit/phpunit-mock-objects": "~3.0",
    "illuminate/support": "^5.3"
},
"autoload": {
  "psr-4": {
      "AgusRdzGitHub": "src/"
    }
},
"autoload-dev": {
  "classmap": [
      "tests/TestCase.php"
    ]
}

After this, add the phpunit.xml file on root folder to run the unit test. It is highly recommended to do a unit test when developing this type of packages.

<?xml version="1.0" encoding="UTF-8"?><phpunit backupGlobals="false"         backupStaticAttributes="false"         bootstrap="vendor/autoload.php"         colors="true"         convertErrorsToExceptions="true"         convertNoticesToExceptions="true"         convertWarningsToExceptions="true"         processIsolation="false"         stopOnFailure="false"         syntaxCheck="false">    <testsuites>        <testsuite name="Your package's test suit">            <directory>./tests/</directory>        </testsuite>    </testsuites></phpunit>

This package is not listed on Packagist.org because it was created only for demonstrative purposes. In order to test it on your project, you only need to add this in your composer.json:

"require": {
        ...,
        ...,
        "agusrdz/github-api": "dev-master"
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/AgusRdz/agusrdz-github-api.git"
        }
    ],

Conclusion

As you can see, creating packages with Composer is very easy. In this example some of the functionalities provided by the GitHub API are to consumed. The best of all is that it can be used from any project that manages its dependencies with Composer.

If you want to see results, you can visit this repository to review how the API is consumed, and this one to see if it is completely functional. For this example, Packagist is not required because it’s focused on not shared repositories but if you want to publish your packages for the use of all the developer community you can do it following the same process.

Read our blog Automated Code deployments with Github Webhooks, if you want to have a continuous integration environment in your application or your website, we also have a successful story using Github Webhooks with Amazon instances visit the link for knowing more.

At ClickIT we are dedicated to creating intelligent solutions to ensure our customers the agility of their applications with a minimum effort by using packages like this, whether public or private.

Published by
DevOps Guy

Recent Posts

How to Choose a Nearshore Software Development Company | Video

You may have considered hiring a nearshore software development company or services, but you still have doubts…

5 days ago

End to End Project Management: Complete Guide

End-to-end project management goes as far back as you can remember. Every project in history, even…

1 week ago

What is AWS DevOps? | The Complete Guide

AWS DevOps has recently become a trending topic in IT circles as it offers companies…

2 weeks ago

AI vs Machine Learning | Key Differences

When understanding AI vs Machine Learning, it’s essential to grasp how these innovations shape the…

3 weeks ago

Why .NET for Cloud Native Development? | Video

If you are involved in the IT industry by any means, it is your job…

4 weeks ago

Azure Migration: Single to Flexible Server

A Fintech company was dealing with outdated infrastructure, incurring additional costs due to the deprecation…

1 month ago