Creating and Publishing Your Own Package

9.Creating and Publishing Your Own Package

M

In this chapter

We'll learn how to turn shared code into a Composer package — its composer.json, folder layout and namespace — how to use it locally through a path repository, how versions come from Git tags, how to publish it on Packagist or keep it private, and what makes a package well designed.

14–16 min

The Problem in Real Life

Olivia's invoice tool is a second small PHP project. It needs exactly what the booking system has: the Money class and price formatting. So it got a copy, three months ago. Since then the booking system's copy learned to add VAT, the invoice tool's copy fixed a rounding bug, and neither has the other's change.

Marcus wants one Money, used by both projects, with a version number — the same way they now use the phone-number library.

O

If a library from the internet can be shared by thousands of projects, why can't our Money class be shared by two?

Olivia

A Copied Class vs. a Versioned Package

Copies drift apart

Two copies of the same class get different fixes, and nobody knows which one is right.

A package is just a folder

Code, a composer.json with a name and an autoload rule — that's all a package needs.

Versions from Git tags

Tag v1.0.0, and every project can require ^1.0.

Creating and Publishing Your Own Package

From consumer to author: every package you've installed was made the same way you can make one. This chapter covers:

  • A package's composer.json and layout: what makes a folder a package.
  • Path repositories: using a package from a folder while developing.
  • Versions and publishing: Git tags, Packagist, or private.
  • Package architecture: what makes a package pleasant to depend on.

A package's composer.json and layout: a package is a folder with code and a composer.json that has at least a name, bookbay/pricing — lower case, vendor/package — and an autoload rule: "BookBay\\Pricing\\": "src/". Its type is library (the main project's is project). It also lists its own require, a description and a license; composer validate --strict checks that nothing is missing. The usual layout is src/ for code, tests/ for tests, and a README.md, CHANGELOG.md and LICENSE at the top. The namespace starts with your vendor name, so BookBay\Pricing\Money can never clash with anyone else's Money — chapter 1 again.

packages/pricing: the package's composer.json and its class
// ---- packages/pricing/composer.json ----
{
"name": "bookbay/pricing",
"description": "Money and price formatting for BookBay",
"type": "library",
"version": "1.0.0",
"license": "proprietary",
"require": {
"php": ">=8.2"
},
"autoload": {
"psr-4": {
"BookBay\\Pricing\\": "src/"
}
}
}
// ---- packages/pricing/src/Money.php ----
<?php
declare(strict_types=1);
namespace BookBay\Pricing;
final readonly class Money
{
public function __construct(public int $pence) {}
public function format(): string
{
return "£" . number_format($this->pence / 100, 2);
}
}

Path repositories: before publishing anything, the package can live in a folder, packages/pricing/, next to the project. A repository of type path tells Composer to find it there: {"type": "path", "url": "packages/pricing"}. composer require bookbay/pricing then installs it like any package — but instead of copying, Composer links vendor/bookbay/pricing to the folder ("Symlinking from packages/pricing"; on Windows it says "Junctioning"). Edit packages/pricing/src/Money.php, and the project sees the change immediately. Monorepos — one Git repository holding several packages — work exactly this way. The diagram below shows the setup.

One Money, two projects

packages/pricing/

composer.json: bookbay/pricing, "BookBay\\Pricing\\": "src/"; src/Money.php

used by

BookBay booking system

repositories: path → packages/pricing; require bookbay/pricing ^1.0

Invoice tool

repositories: vcs → github.com/bookbay/pricing; require bookbay/pricing ^1.0

installed as

vendor/bookbay/pricing → link to the folder

edits show up immediately

vendor/bookbay/pricing → downloaded v1.0.0

updates when a new tag is pushed

The project uses it through a path repository (real output, Linux or macOS)
// ---- composer.json (part) ----
{
"repositories": [
{ "type": "path", "url": "packages/pricing" }
]
}
$ composer require bookbay/pricing:^1.0
...
Lock file operations: 1 install, 0 updates, 0 removals
- Locking bookbay/pricing (1.0.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing bookbay/pricing (1.0.0): Symlinking from packages/pricing
Generating autoload files

Versions come from Git tags: once the package has its own Git repository, Composer reads its versions from tags: git tag v1.0.0 and git push --tags publish version 1.0.0, and any branch becomes a dev version (dev-main, chapter 5). A path repository has no tags, so the example adds "version": "1.0.0" to the package's composer.json; projects can then require ^1.0. Follow semver strictly from the first release: a renamed method is a new major version, however small it feels.

Releasing a version from Git
$ git tag v1.1.0 # Money::withVat() added: a compatible feature → MINOR
$ git push origin v1.1.0
# in each project that wants it:
$ composer update bookbay/pricing

Publishing: for a public package, push the repository to GitHub (or GitLab), sign in at packagist.org and submit the repository's URL. Packagist reads composer.json and the tags, and from then on anyone can composer require it; a webhook makes new tags appear automatically. For a private package like bookbay/pricing, skip Packagist and use a vcs repository with auth.json (chapter 7), or a private package server. Either way, the invoice tool and the booking system now both require bookbay/pricing:^1.0 — one Money, two projects.

Package architecture: a good package does one thing and has a small public surface: final classes and interfaces (Act 11) for what users touch, everything else @internal or private, because every public method is a promise you keep until the next major version. Require as little as possible, and as widely as possible: "php": ">=8.2" and ^ ranges, so your package doesn't cause chapter 8's conflicts in other people's projects. Libraries don't need to commit composer.lock — the projects that install them have their own. Add a .gitattributes with export-ignore for tests/ and other development files so they're left out of the downloaded archive, and keep a CHANGELOG.md so users can see what each version changed. The table below lists the composer.json fields a library needs.

Table — composer.json fields for a library
FieldExampleWhy
namebookbay/pricingRequired; lower case vendor/package
descriptionMoney and price formattingRequired to publish
typelibraryThe default for packages
licenseMIT or proprietaryTells users what they may do
require"php": ">=8.2"As little and as wide as possible
autoloadpsr-4 BookBay\Pricing\ → src/So projects can load your classes
autoload-devpsr-4 BookBay\Pricing\Tests\ → tests/Your tests only
versionUsually omittedGit tags provide versions

Key Takeaway

A package is a folder with code and a composer.json naming it (vendor/package, lower case, type library) with a PSR-4 autoload rule under a vendor namespace. A path repository installs it from a local folder by linking, so edits show up immediately. Versions come from Git tags (v1.0.0) and must follow semver. Public packages are published by submitting the repository to Packagist; private ones use vcs repositories with auth.json. Good packages do one thing, expose little, require little with wide ranges, don't need a committed lock file, and keep a changelog.

Why This Matters

Teams that run more than one PHP application always end up with shared code. Packages are how it stays in one place, with versions, instead of drifting copies.

Laravel itself is a few dozen packages built exactly this way, and so is every Laravel add-on you'll install. Knowing how packages are made makes their composer.json files, and their version numbers, readable.

bookbay/pricing 1.0.0 is one package used by two projects, and Olivia's rounding fix and the VAT method are finally in the same Money. The project now has namespaces, PSR-4 autoloading, a composer.lock, scripts, and composer audit runs clean. Time to put it all together — on your own computer — in the Composer Checkpoint.