How can you make your own Composer application and publish
-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:
- 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.
- 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 asrequest
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. - 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.
- 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.
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.
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.
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.
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.
Click "Authorize" to allow Packagist to gain access to your account.
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..
Simply click submit then Packagist is quick to create and upload your project.
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:
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.