PHPackages                             sanmai/version-info - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. sanmai/version-info

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

sanmai/version-info
===================

Version Info Parser

0.2.3(1y ago)137.0k↓13.6%3MITPHPPHP &gt;=8.2CI passing

Since Sep 30Pushed 1w ago1 watchersCompare

[ Source](https://github.com/sanmai/version-info)[ Packagist](https://packagist.org/packages/sanmai/version-info)[ GitHub Sponsors](https://github.com/sanmai)[ RSS](/packages/sanmai-version-info/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (5)Dependencies (8)Versions (10)Used By (3)

Version info parser
===================

[](#version-info-parser)

[![Latest Stable Version](https://camo.githubusercontent.com/da187d35d5ac81ba41285ae8f5307cb6e7c296dd0f121e7568f86cde75274c1a/68747470733a2f2f706f7365722e707567782e6f72672f73616e6d61692f76657273696f6e2d696e666f2f762f737461626c65)](https://packagist.org/packages/sanmai/version-info)[![Coverage Status](https://camo.githubusercontent.com/d537ab66f19bc42ae38a4c2441476402e51a39b2856875204e9263dabb50f49b/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73616e6d61692f76657273696f6e2d696e666f2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/sanmai/version-info?branch=master)

This small library solves a problem where a package wants to know or report own version.

This is not a new problem, e.g. there's [ocramius/package-versions](https://github.com/Ocramius/PackageVersions), but it depends heavily on Composer, and not without an associated IO penalty.

On the contrary, this package solves the same problem but without any extra IO whatsoever. What you need is to instruct Git to [expand placeholders](https://git-scm.com/docs/gitattributes#_export_subst) (detailed instructions below) when adding a file with a constant to an archive of a tagged release. Then you feed this constant to the library, and there is your version.

Sure, you can't be certain people always install a package from archives. In this case you can use either abovementioned [ocramius/package-versions](https://github.com/Ocramius/PackageVersions), or use [two auxiliary classes](#fallback-readers) this library provides to fetch version strings right from Git or from a [branch alias](https://getcomposer.org/doc/articles/aliases.md#branch-alias) from `composer.json`.

Installation
------------

[](#installation)

```
composer require sanmai/version-info
```

This library needs PHP 7.4 or greater. It was tested to work under PHP 7.x and 8.0.

Usage
-----

[](#usage)

Create a PHP class (or use existing class) with the following constant:

```
const VERSION_INFO = '$Format:%h%d by %an +%ae$'; // or '$Format:%h%d$'
```

Amend `.gitattributes` with a path to the file with the class with `export-subst` attribute:

```
/src/MyVersion.php     export-subst

```

Where you want to know your version, call a version reader:

```
$reader = \VersionInfo\PlaceholderVersionReader(MyVersion::VERSION_INFO);
$version = $versionReader->getVersionString();

if ($version !== null) {
    return $version;
}

// Fallback on other methods, or return a dummy version.
// See src/Example.php for a complete example.
```

That's all!

Testing
-------

[](#testing)

To verify that your version constant is being correctly replaced you can use `git archive` command, pointing it at a tag, or a branch:

```
git archive --format=tar v1.1 | grep --text VERSION_INFO
```

The constant should contain something like:

```
c3ff8f6 (tag: v1.1) by John Doe +john.doe@example.com

```

Fallback readers
----------------

[](#fallback-readers)

Apart from `PlaceholderVersionReader` there are `GitVersionReader` and `ComposerBranchAliasVersionReader`. [See this example for details.](src/Example.php)

Memoization
-----------

[](#memoization)

Any of these classes do not do their own memoization. If you need memoization and lazy loading, try [Later, a deferred object manager](https://github.com/sanmai/later).

```
use function Later\later;

$deferredVersion = later(function () {
    $reader = \VersionInfo\PlaceholderVersionReader(MyVersion::VERSION_INFO);

    yield $version = $versionReader->getVersionString();
});

// And at some later point...
$deferredVersion->get(); // returns memoized version string, computing it on the spot, as needed
```

License
-------

[](#license)

This project is licensed [under the terms of the MIT license](LICENSE).

###  Health Score

49

—

FairBetter than 94% of packages

Maintenance76

Regular maintenance activity

Popularity30

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 57.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~432 days

Total

5

Last Release

374d ago

PHP version history (4 changes)0.1PHP ^7.3 || ^8.0

0.2.1PHP &gt;=7.3

0.2.2PHP &gt;=7.4

0.2.3PHP &gt;=8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/edcb8dde95c71b1c97c3c91e57d3548795fa2014c657744fb878e2be3b5949fc?d=identicon)[sanmai](/maintainers/sanmai)

---

Top Contributors

[![sanmai](https://avatars.githubusercontent.com/u/139488?v=4)](https://github.com/sanmai "sanmai (36 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (27 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sanmai-version-info/health.svg)

```
[![Health](https://phpackages.com/badges/sanmai-version-info/health.svg)](https://phpackages.com/packages/sanmai-version-info)
```

###  Alternatives

[mck89/peast

Peast is PHP library that generates AST for JavaScript code

19139.2M47](/packages/mck89-peast)[sauladam/shipment-tracker

Parses tracking information for several carriers, like UPS, USPS, DHL and GLS by simply scraping the data. No need for any kind of API access.

9843.5k](/packages/sauladam-shipment-tracker)[jstewmc/rtf

Read and write Rich Text Format (RTF) documents with PHP

45153.1k6](/packages/jstewmc-rtf)[tcds-io/php-jackson

A lightweight, flexible object serializer for PHP, inspired by FasterXML/jackson

113.2k10](/packages/tcds-io-php-jackson)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
