PHPackages                             aklump/token-engine - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. aklump/token-engine

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

aklump/token-engine
===================

A style-independent engine for string token replacement.

0.1.3(2y ago)0100BSD-3-ClausePHPPHP &gt;=7.4

Since Dec 2Pushed 2y ago1 watchersCompare

[ Source](https://github.com/aklump/token-engine)[ Packagist](https://packagist.org/packages/aklump/token-engine)[ Docs](https://intheloftstudios.com/packages/composer/token-engine)[ RSS](/packages/aklump-token-engine/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (3)Versions (12)Used By (0)

Token Engine
============

[](#token-engine)

A style-independent engine for string token replacement.

Here are three included styles...

Tokenized StringFinal OutputStyleHello {{ first }},Hello Peter,TwigStyleHello @first,Hello Peter,AtStyleHello \_\_first,Hello Peter,DoubleUnderscoreStyleOr make your own by implmenting `\AKlump\TokenEngine\TokenStyleInterface`

Basic Usage
-----------

[](#basic-usage)

```
$tokens = \AKlump\TokenEngine\TokenCollection::createFromKeyValueArray([
  'first' => 'Peter',
]);
$replace_tokens = (new \AKlump\TokenEngine\ReplaceTokens($tokens, new \AKlump\TokenEngine\Styles\AtStyle()));

'Welcome Mary Smith,' === $replace_tokens('Hello @first,');
```

Callable Tokens
---------------

[](#callable-tokens)

```
// Create a new token collection.
$tokens = new \AKlump\TokenEngine\TokenCollection();

// Create a token with a callable value.
$callable_token = new \AKlump\TokenEngine\Token('first|toUpperCase');
$callable_token->setValue(function (array $context) {

  // Oversimplified, but it makes the point of callables and context.
  return strtoupper($context['first']);
});
$tokens->add($callable_token);

// Use that token in a string template.
$template = 'HEY {{ first|toUpperCase }}!';

// Create the styleized replacer.
$replace = (new \AKlump\TokenEngine\ReplaceTokens($tokens, new \AKlump\TokenEngine\Styles\TwigStyle()));

// Replace the token with runtime context.
$runtime_context = ['first' => 'Peter'];
$shout = $replace($template, $runtime_context);

'HEY PETER!' === $shout;
```

Working with Styles
-------------------

[](#working-with-styles)

```
$string = 'lorem';
$stylized = (new \AKlump\TokenEngine\Helpers\StylizeString(new \AKlump\TokenEngine\Styles\TwigStyle()))($string);
'{{ lorem }}' === $stylized;

$array = ['foo' => 'bar'];
$stylized = (new \AKlump\TokenEngine\Helpers\ArrayStylizeKeys(new \AKlump\TokenEngine\Styles\TwigStyle()))($array);
['{{ foo }}' => 'bar'] === $stylized;

$array = ['foo' => 'bar'];
$stylized = (new \AKlump\TokenEngine\Helpers\ArrayStylizeValues(new \AKlump\TokenEngine\Styles\TwigStyle()))($array);
['foo' => '{{ bar }}'] === $stylized;
```

Generate CSV of Tokens for User Documentation
---------------------------------------------

[](#generate-csv-of-tokens-for-user-documentation)

```
$examples = \Foo\Bar::getExampleTokens()->toKeyValueArray();
$examples = (new \AKlump\TokenEngine\Helpers\ArrayStylizeKeys(new TwigStyle()))($examples);
$help_text = sprintf('Available tokens: %s', implode(', ', array_keys($examples)));
```

More Example Code
-----------------

[](#more-example-code)

This comes from a project's documentation generation.

```
// Combine two different token collections.
$collection = GetContextTokens::getExampleTokens();
$collection = $collection->merge(GetBudgetTokens::getExampleTokens());

// Sort them by their tokens.
$collection = $collection->sort('token');

// We want to the documentation to show the styled token.
$styler = new \AKlump\TokenEngine\Helpers\StylizeString(new \AKlump\TokenEngine\Styles\AtStyle());

// Iterate on the combined collection and access token properties.
$table_data = [];
foreach ($collection as $token) {
  $table_data[] = [
    'token' => $styler($token->token()),
    'description' => $token->description(),
    'example' => $token->value(),
  ];
}
$table = (new CreateTable())($table_data);
```

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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 ~7 days

Recently: every ~16 days

Total

11

Last Release

822d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/79476f3b82f9c766433c7eb401cbcfc636cd5d5ccf2b2e6b50ea81d1de6c2730?d=identicon)[aklump](/maintainers/aklump)

---

Top Contributors

[![aklump](https://avatars.githubusercontent.com/u/425737?v=4)](https://github.com/aklump "aklump (48 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/aklump-token-engine/health.svg)

```
[![Health](https://phpackages.com/badges/aklump-token-engine/health.svg)](https://phpackages.com/packages/aklump-token-engine)
```

###  Alternatives

[ramsey/uuid

A PHP library for generating and working with universally unique identifiers (UUIDs).

12.6k700.2M3.3k](/packages/ramsey-uuid)[larswiegers/laravel-maps

A new way to handle maps in your laravel applications.

361119.1k2](/packages/larswiegers-laravel-maps)

PHPackages © 2026

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