How to Build and Publish Your Own Composer Package for PHP Projects

Learn step-by-step how to create a PHP Composer package, configure its composer.json, implement code with PSR‑4 autoloading, write PHPUnit tests, manage Git commits, publish to Packagist, and install the package via Composer, complete with practical commands and examples.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Build and Publish Your Own Composer Package for PHP Projects

Composer is a PHP dependency manager that automates downloading, installing, and autoloading libraries defined in a project's composer.json.

How Composer Works

It consists of three parts: the CLI tool, a package repository, and the code repository.

CLI Commands

The composer command provides operations such as create-project, require, remove, and clear-cache.

Repositories

Packagist

: the default public repository (https://packagist.org) that stores package metadata. Repository: the code host, typically GitHub or Gitee.

Autoloading

Composer generates autoload files that follow PSR‑0 and PSR‑4 standards.

Step‑by‑Step Guide

Step 1 – Create a Git Repository

Create a public GitHub repository named hello and clone it locally.

git clone [email protected]:Tinywan/hello.git

Step 2 – Initialise composer.json

Run composer init and answer the prompts to generate a basic composer.json. Example output:

{
    "name": "tinywan/hello",
    "description": "How to build your own Composer package",
    "type": "library",
    "autoload": {
        "psr-4": {
            "Tinywan\\Hello\\": "src/"
        }
    },
    "authors": [
        {
            "name": "Tinywan",
            "email": "[email protected]"
        }
    ],
    "require": {}
}

Step 3 – Implement Package Functionality

Create a simple encryption class in src/Encryption.php that provides static encrypt and decrypt methods using OpenSSL.

<?php
declare(strict_types=1);

namespace tinywan\hello;

class Encryption
{
    public const AES_128_ECB = 'AES-128-ECB';

    public static function encrypt(string $data, string $key, string $algo = self::AES_128_ECB, string $iv = ''): string
    {
        $encryptedBytes = openssl_encrypt($data, $algo, $key, OPENSSL_RAW_DATA);
        return base64_encode($encryptedBytes);
    }

    public static function decrypt(string $data, string $key, string $algo = self::AES_128_ECB, string $iv = ''): string|false
    {
        return openssl_decrypt(base64_decode($data), $algo, $key, OPENSSL_RAW_DATA);
    }
}

Step 4 – Write PHPUnit Tests

Install PHPUnit as a dev dependency and create tests/EncryptionTest.php to verify encryption and decryption.

<?php
declare(strict_types=1);

class EncryptionTest extends \PHPUnit\Framework\TestCase
{
    public function test()
    {
        $data = 'OpenSourceStack';
        $key  = '53vYPpTJIR1aYFiFh0PppZzF';

        $encrypt = \tinywan\hello\Encryption::encrypt($data, $key);
        self::assertIsString($encrypt);

        $decrypt = \tinywan\hello\Encryption::decrypt($encrypt, $key);
        self::assertEquals($decrypt, $data);
    }
}

Run the test with:

$ vendor/phpunit/phpunit/phpunit tests/EncryptionTest.php

Step 5 – Commit and Tag

Add a .gitignore to exclude generated files and IDE directories, commit the code, and create a version tag.

build
vendor
.idea
.vscode
.phpunit*
composer.lock
$ git commit -m "init"
$ git tag v0.1
$ git push origin v0.1

Step 6 – Publish to Packagist

Register the package on packagist.org (or submit via the “Submit Package” page) so that Composer can retrieve its metadata.

Step 7 – Install the Package

Require the package in another project: composer require tinywan/hello dev-main For a stable release, use the tagged version (e.g., v0.1).

Conclusion

By following these steps you can create a reusable Composer library, test it with PHPUnit, publish it to Packagist, and consume it in any PHP project, avoiding duplicate code and leveraging the PHP ecosystem.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPComposerPackagephpunitPackagistPSR-4
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.