Creating reusable packages with Packagist

Here is a transferable skill. And you’d stand out of the crowd with this knowledge I reckon. Although its very common to work with open source libraries, we rarely ever create our own.

Have you ever had a piece of code that you find so useful, but have never shared it with someone else? Maybe its because you may think it may be not so useful to someone else or some other reason, but you’ll never know unless you try. I for one, encourage you to give back to your community. Not to mention it gives you credibility in the open source world.

Step 1) Version control your code. Here is how to if don’t already know.

Step 2) Introduce a composer.json file at the root of your project.

A composer.json file tells composer of its dependencies, versioning and autoloading strategy. Here is a sample file

{
  "name": "<your project>/<your repository>",
  "description": "<brief description>",
  "authors": [
    {
      "name": "Your name"
    }
  ],
  "require": {
    "<dependency>": "<version>"
  },
  "autoload": {
    "psr-4": {
      "<namespace mapping>": "<folder mapping>"
    }
  }
}

Here is a sample

{
  "name": "genesis/behat-fail-aid",
  "description": "Get more out of your test suite by getting it to work with you when tests fail. Works with Goutte and MinkExtension (Selenium2Driver)",
  "authors": [
    {
      "name": "Abdul Wahhab Qureshi"
    }
  ],
  "require": {
    "behat/mink": "*@stable",
    "behat/mink-extension": "~2.0@stable",
    "behat/mink-selenium2-driver": "^1.3"
  },
  "require-dev": {
    "phpunit/phpunit": "4.*",
    "behat/behat": "^3.5",
    "behat/mink-goutte-driver": "^1.2",
    "ciaranmcnulty/behat-localwebserverextension": "^1.1"
  },
  "autoload": {
    "psr-4": {
      "FailAid\\": "bootstrap/"
    }
  }
}

A more comprehensive description of the composer.json file can be found here https://getcomposer.org/doc/04-schema.md

The versioning followed is usually SemVer. Head over to semver.org for more details. This is extremely important if you want your package to survive in the open source world!

Step 3) Register on packagist.org

Go through the usual hoops around the signup process.

Step 4) Register/Publish your project repository on packagist.org

  1. Ensure that you’re logged in.
  2. Find the Submit button and click it!
  3. Enter your project repository url in the field. This is what appears in your web address bar when you’re looking at the repository in your browser.
  4. Click on Check
  5. Packagist will validate the composer.json file in your project and inform you of its findings.
  6. Confirm and submit the package!

You can now execute something like this to download your package

composer require <your project>/<your repository name>

Or require it in another project’s composer.json file!

Step 5) Tagging your code

Once you’re at a point when you have a version fit for public usage (stable), you can mark it with a tag a point of reference for others to use it in that state forever. You will make changes later on, but this tag will be fixed for people to use. As you go along you will keep on creating more tags adding new and exciting features that people can reuse. To mark your committed change as stable and reusable you should use a versioning strategy. The most common and well known is SemVer. Here is an article detailing it. To create a tag execute:

git tag 1.0.0

This will create a tag named 1.0.0. This is only created on your local machine, push your tag to the remote machine with the command

git push --tags

Versioning your code >>

Publish your code as a package on packagist! >>

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *