How can you make your own Composer application and publish

May 30, 2024
Illustration representing a developer working with PHP code and creating a Composer package.

-sidebar-toc>

Even though Composer was built to manage dependency it can be used for other tasks as well. Composer for other tasks, such as:

  • Automated loading Automatically load functions and classes from external libraries so that they are easily accessible in your code. This will make it easier for the process of integrating code from external sources in your code.
  • Code generation: Generate boilerplate code to your application, such as configuration files, or any other frequently used code snippets to accelerate the developing process and assure uniformity.
  • scripting: A built-in scripting software can automate the most common activities, like creating documentation or running tests from your code base. This can help simplify your development process and decrease manual effort.

This step-by-step guide will walk users through the process of creating a Composer package, then posting it on Packagist, a repository for PHP packages that developers across the globe are able to use for their own applications.

What is the process behind Composer function?

First, let's make sure we understand Composer. In simple terms, Composer is a program that works with a composer.json file that includes the definitions of the dependencies in the PHP project. It looks up those packages from a centralized repository and then automatically downloads and installs those dependencies using the repo for packages.

If you use Composer installed within your PHP working environment This is what the dependency download and installation procedure will look like:

  1. Define the required dependencies for your project in the composer.json file in the project's root directory. This file provides information about the required libraries, their versions and any other options for configuration or dependencies on the libraries.
  2. Find dependencies within Composer by using commands such as installing to install the necessary dependencies; upgrade to upgrade dependencies that are already in use as well as request to add additional dependencies in the composer.json file. When you use the command Composer reads the composer.json file to find the required dependencies, and then searches the package repo for the latest version of each dependency appropriate to your PHP environment and checks for any conflicts or version constraints.
  3. Composer downloads and installs required dependencies (including libraries) to the vendors directory of your project. Composer then creates the composer.lock file recording exactly the versions of the dependencies installed.
  4. Composer creates an autoloader to load classes and functions from the dependencies that are installed instantly. This allows you to include the library installed for your projects without the need to manually add every file.

The process of creating and publishing a Composer package

This guide teaches you to create a simple PHP library known as tempconv which converts Celsius temperature to Fahrenheit and reverse, and then wraps it up with the publication of it in a Composer package.

The prerequisites

There are a few items in place prior to you start:

  • PHP and Composer installed properly on your system at the time of writing the most recent Composer version of Composer is v2.6.6, but these instructions can be used with any version 2 variant.
  • The  GitHub account to set up the repository to host your code.
  • An account on Packagist to make your library available for publication.

Create a repository of projects

Make your personal GitHub repository to follow this guide, and you'll be able to upload your completed library's source code as well as files.

Screenshot of the page seen while creating a repository on GitHub.
Creating the project repository in GitHub.

This tutorial uses the project name tempconv. In the description field, include a few sentences about the program. Select the option to create the README file. Choose the Composer template as the Add .gitignore option, and select a licence of your choice. This tutorial employs MIT License. Finally, click Create repository.

Create a copy of the repository

Create a copy of the repository that you have made to the local machine. Be sure to replace the URL with your own both here and throughout this tutorial:

$ git clone https://github.com/rexfordnyrk/tempconv.git

This creates a directory called tempconv within your working directory. In the moment, it only contains README.md, the LICENSE as well as .gitignore files, however, you'll also create your packages here also.

Making your PHP library

In your project directory, add a file named TemperatureConverter.php with the following code:

This class has only one method, convert which takes temperature as well as the unit arguments and return the temperature converted. It will throw an exception in the event that the unit is invalid.

That's enough for now. In the real world, you would likely develop unit tests in order to make sure your program works in the way you expected after modifications or updates.

The process of creating your composer packages.

With your library code put in place, you're now ready to transform it into an Composer package. It will create an composer.json file for your library using a step-bystep guide, and then we'll go over some of the best practices for organizing your program as a package before pushing it into your repository.

Making the composer.json package file

Although you can manually create the content of a composer.json file in the root directory of your project, it's better to generate it by using the next Composer command:

$ composer init

The command will guide through a simple process-by-step guide. The answers you provide to prompts like the package name the description, the author's information as well as the type of license will generate the package's composer.json file.

Composer's documentation outlines the expected values, as well as alternatives you could use to create your composer.json. composer.json.

Screenshot of Composer's config generator in action.
Utilizing composer init to create the configuration.

The wizard will provide already-defined responses to questions where applicable, such as deriving your email address and name from Git. It is your choice to modify the answers if you want to.

This library isn't tied to any other packages for functionality Therefore, you're able to say No to any questions regarding dependencies.

The wizard shows you a preview of the generated file content and asks you to confirm it to complete the wizard.

Organizing package files

Completing the wizard creates two directories, in addition to the composer.json file:

  • SRC for the source code
  • vendor to download dependencies

Move the TemperatureConverter.php file into the src directory. If your library has dependencies that require composer install, you can use composer install to create the autoloader. Install the package dependencies.

Uploading the code to GitHub

Make sure to add your new and modified files to the git repository:

$ add git to git with"-A"

Commit changes made in the local repository. Then push it to the remote repository at GitHub to make it easy for you to make the project available in the following section:

$ git commit -am "Initial Release" And the git push

Make a version of your library available for release your library

If you include your code in your repo file, you'll be able to create a release of your library and include an identifier number, so that other developers can keep track of the stable and important updates.

Go to your repo on GitHub to select Releases beneath in the About section. The Releases page is where you're expected to have nothing right now then click Make an entirely new release.

A screenshot of GutHub's Releases page.
GitHub's Releases page.

Complete a couple of details concerning your release. These include the tag version and release title. The tag version should be an unique identifier of this release (example: v1.0.0) as well as the release's title should outline the modifications included in the release (example: Initial release).

You can also provide a short description for the version you are releasing. If you want to upload a document, like a compiled binary or source code archive, simply drag and drop the file in the Attach binaries by dropping it here or by selecting them in the area. However, you don't need to do that to follow this instruction.

Screenshot showing details entered on GitHub's Releases page.
The initial release information must be entered on GitHub.

Click "Publish" to make the release.

Your project is now available in your Releases page in your repository. The users can download the files that you've attached as well as read the notes on the release. In addition, if you've have added a tag to your release, developers may utilize that tag to test for the exact source code that you included within the released.

Your library is now ready for sharing all over the globe. Let's publish it in a bundle on Packagist.

Starting by using Packagist

Packagist is the main repository of packages for PHP. It provides a central location where developers can publish and distribute their PHP packages as well as it allows other developers to access and use those packages in their own development projects. Let's get your package published!

Visit the Packagist web site, click Login then select GitHub as the authentication method. Use your GitHub account to authenticate.

Screenshot showing the Packagist authorization page via GitHub.
Joining Packagist using GitHub.

Click "Authorize" to allow Packagist to gain access to your account.

Screenshot of a user logged into Packagist.
Login successfully on Packagist by using GitHub.

Sending your Composer pack to Packagist

For a package to be published on Packagist you must submit the GitHub repository that contains composer.json. composer.json file that provides the description of your package as well as its dependencies. Select "Submit" on Packagist's site and enter the URL of your repo on the screen that opens then click Check to validate the submission.

If the repository's name is correct, Packagist detects the name of the repository The check button is changed to the Submit button..

Screenshot of Composer package details ready to submit on Packagist.
Composer package Rexfordnyrk/Tempconv validated and ready to submit.

Simply click submit then Packagist is quick to create and upload your project.

Screenshot of Composer package details after publishing.
The Composer software package has been published successfully.

That's it! Your package is now available via Packagist for developers who want to use as a dependency in their own projects.

Utilizing your Composer program in different projects

It is possible to use the packaged package to create other applications using the package as an dependency in your composer.json file. You are able to manually build and edit the file but we recommend using the Composer command in the following manner:

$ composer require rexfordnyrk/tempconv

Composer generates it's composer.json file automatically when it's not already there Then it searches the file, downloads it, and automatically loads the file from Packagist. The output should look something similar to the following:

Screenshot of the terminal after adding a library to a project with Composer.

Create an demo.php file to contain your demo application program to integrate with your library.
  It is possible to make use of this temperature converter class in your program code in the following manner:

convert(20, 'C');
 echo "20degC is equivalent to $fahrenheit degF\n";
 
 $celsius = $converter->convert(68, 'F');
 echo "68degF is equivalent to $celsius degC\n";

This code uses it's TemperatureConverter class to convert temperatures between 20 Celsius and 68 Fahrenheit, and produce the result. The imported autoloader.php file was created using Composer to load the dependencies you require. That takes care of loading the class automatically when necessary.

Last but not least, execute this program in your terminal:

$ php demo.php

It should produce output similar to this:

PHP demo.php
20degC corresponds to 68 degF
68degF equals 20 degC

Summary

The library you've created is a straightforward PHP library to convert temperature in Celsius to Fahrenheit and vice versa using an object-oriented program that can be reused in other programs. You've learned to use Composer to create a package from the class with the Composer Init command, and you've learned the basic organizational best practices to use to use your library. Your library was published on Packagist for you to see if other developers can utilize it as a dependent in other projects.

Rexford Nyarko

Rexford Nyarko is a solution architect who has a background in both network and software systems, cloud environments, server administration, and diverse database technologies. Rexford especially enjoys teaching technical terms to people who are not technical.