Saturday, February 27, 2021
Home Pc and Mobiles Learn How to Distribute Your PHP Packages With Packagist

Learn How to Distribute Your PHP Packages With Packagist

You’ve developed out a beautiful PHP package that you’ve put your heart and soul into, and are proud of. Now it’s time for distribution, and one key part is ensuring you provide others the easiest possible route to download and utilize your package, but how?

Thankfully, For this there is Packagist a free de facto repository for all PHP package dependencies. Learn how to get your PHP package listed on Packagist, allowing others to download and install it with one command.

Structure Your Package Properly

Before distribution, your package needs to be structured properly and must utilize namespaces. Decide on a root namespace for your package. This can be anything you wish, but generally it’s advised to use AuthorPackageName.

For example, the namespace JSmithBlogPoster would inform others the developer of the package goes by the name Jsmith and this is their package named BlogPoster.

With root namespace decided, create a /src/ directory in your project, and move all PHP files to it. Every file must have the proper declaration at the top, so all PHP files within the /src/ directory must commence:

namespace JSmithBlogPoster;

The namespace declaration is relative to the directory structure. For example, if you have a file at /src/Images/Uploader.php, at the top of the file you would have:

namespace JSmithBlogPosterImages;
class Uploader

Last, ensure the class declaration of all PHP files is the same as the filename, as the above example shows. The Uploader.php file has the class name Uploader.

Register a Packagist Account

If you don’t already have one, create a Packagist account, it’s quick and free. Although not required, it’s standard practice to register with the same username as in the root namespace you decided upon in the above section. If that username is not available, then choose something similar.

You don’t need to do anything with the Packagist account just yet, but it is useful to have it ready.

Create composer.json File

Open a text editor, and within the root directory of your project create a composer.json file, with the following contents used as an example:

"name": "jsmith/blogposter",
"description": "An excellent blog posting package...",
"type": "package",
"homepage": "",
"license": "MIT",
"require": {
"php": ">=8.0.0"
"autoload": {
"psr-4": {
"JSmith\BlogPoster\": "src/"

There are only two important aspects of the above file to take note of.

First is the name element at the top. This  which must be two strings separated by a forward slash, the first being your Packagist username, and the second being the desired name of the package. Using the above example, the Packagist username is jsmith, and the package name is blogposter.

Second, at the bottom within the autload section you will notice the line:

"JSmith\BlogPoster\": "src/"

Modify this line so the root namespace you previously decided upon points to the /src/ directory. When your package is installed by someone else, Composer will automatically map your root namespace to the sub-directory your package was installed in.

Upload Your Package to Github

You need to upload your PHP package to a Github repository, as that’s the main integration method Packagist uses. If you aren’t already familiar with creating and publishing to a Github repository, please check out How to Create Your First Repository on GitHub for an excellent step-by-step guide.

Ensure to upload your entire PHP package including all files within /src/ and the composer.json file. If you wish to simply upload the entire project directory to GitHub, open a terminal inside your project directory and run:

git add ./*
git commit -m "Initial commit"
git push -u origin master

You also must add a tag to the GitHub repository, as tags are how Packagist keeps track of releases and version numbers of your packages. To mark the current repository as v0.1 run the commands:

git tag 0.1
git push --tags

Add a PHP Listing on Packagist

Everything is now in place, so with a smile you can go ahead and submit your PHP package for instant listing on Packagist. Visit the Submit Package page, and enter the URL to your GitHub repository, which will be something like:

Hit the Check submit button, and Packagist will download the composer.json file from your repository, plus any available tags / releases and other information.

The next page will confirm you wish to publish the package; once confirmed you will be able to view your new Packagist listing at a URL such as:

Install Your Package With Composer

Try it out, and install your PHP package with Composer. If you don’t already have Composer installed, you may install it with the following command:

sudo curl -sS | sudo php -- --install-dir=/usr/local/bin --filename=composer

Create a new directory, and within it run the command:

composer require jsmith/blogposter

Composer will work its magic, and you will notice a new /vendor/ sub-directory will appear, which if you look inside of, you will see your PHP package!

Give it a test, and load your PHP package. Using the above example, if there was a file a /src/Blogger.php in the package, a quick test script to load it would be:

use JSmithBlogPosterBlogger;
// Load composer dependences
// Get blogger
$client = new Blogger();
echo "Got a " . $client::class . "

Go Forward, and Share With the World

You’ve now learned how easy it is to structure your PHP package appropriately, and list it on Packagist allowing anyone in the world to instantly download and include your package within their project with one single command.

Congratulations, you’ve now opened your PHP package up to the main distribution channel within the PHP eco-system. Go forward, and share your creativity and hard work with the world.

software planning
6 Software Development Steps All Programmers Should Know

Preparing to code your first program? Be sure to follow these key software development steps.

About The Author

Source link

Leave a Reply

Most Popular

Recent Comments

%d bloggers like this: