PHPackages                             mike4git/chain-bundle - 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. mike4git/chain-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

mike4git/chain-bundle
=====================

This bundle offers a generic way of creating a chain of (potentially responsible) handlers which delegate or finish the task.

1.0.0(11mo ago)179[1 PRs](https://github.com/mike4git/chain-bundle/pulls)MITPHPPHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0CI passing

Since Jun 6Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/mike4git/chain-bundle)[ Packagist](https://packagist.org/packages/mike4git/chain-bundle)[ RSS](/packages/mike4git-chain-bundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (20)Versions (4)Used By (0)

chain-bundle
============

[](#chain-bundle)

This bundle offers a generic way of creating a chain of (potentially responsible) handlers which delegate or finish the task.

May be you have already heard about the [Chain of Responsability Pattern](https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern)?!

If you think you can handle that with Symfony Events, do so. If you think you can use Messenger-Middleware-Stack instead, of course - feel free.

But if you are searching for a generic way to put services to a chain, where each of the chain links can decides what is going on, use this bundle.

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

[](#installation)

### Require the bundle

[](#require-the-bundle)

```
composer require mike4git/chain-bundle
```

That's it.

Usage
-----

[](#usage)

Imagine you have a processing chain. And each link in the chain may need to perform a specific operation on a workpiece until the entire workpiece is complete.

Or you have a series of areas of responsibility, and a request passes through this [Chain of Responsabilities](https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern) until it reaches the one that can handle and respond to it.

In both cases, the chain-bundle helps you create a simple, structured solution based on a common design pattern.

All you need is a workpiece or a request represented as a PHP object. The only requirement is that this object must implement the marker interface `Mike4Git\ChainBundle\Handler\Context\ChainHandlerContext`.

Each handler or responsible unit in your chain has two constraints:

1. They must implement the Mike4Git\\ChainBundle\\Handler\\ChainHandlerInterface, which requires two methods:

- `supports()` – to check if the handler can process the given request
- `handle()` – to perform the actual processing

2. Handlers are registered as tagged services, like this:

```
Mike4Git\ChainBundle\Tests\FizzBuzz\FizzHandler:
    tags:
        - { name: 'chain.handler', chain: 'fizzbuzz', priority: 100 }
    public: true
```

To trigger the processing chain, simply use the following code:

```
/** stop handling if fitting handler found - by default it is true */
$stopOnFound = false;

/** @var ChainExecutor $executor */
$executor = $container->get(ChainExecutor::class);
$processedWorkpiece = $executor->execute('Name Of The Processing Chain', $workpiece, $stopOnFound);
```

That's it.

### `AsChainHandler` Attribute

[](#aschainhandler-attribute)

To make declaration of chain handlers easier, you can use the `ChainHandler` attribute in your handler class:

```
#[AsChainHandler(chain: 'sample', priority: 100)]
class MyHandler implements ChainHandlerInterface
{
   ...
}
```

But additionally you have to register `MyHandler` as a Symfony service in your yaml file:

```
App\MyHandler: ~
```

### Example

[](#example)

Let's implement a simple FizzBuzz chain using the chain-bundle. We want different handlers to decide if a number should be replaced with "Fizz", "Buzz", "FizzBuzz", or left as-is.

#### Define the Context

[](#define-the-context)

```
use Mike4Git\ChainBundle\Handler\Context\ChainHandlerContext;

class FizzBuzzContext implements ChainHandlerContext
{
    public function __construct(
        public int $number,
        public string $result = ''
    ) {}
}
```

#### Create Handlers

[](#create-handlers)

One simple example for the `FizzBuzzHandler`:

```
use Mike4Git\ChainBundle\Handler\ChainHandlerInterface;

class FizzBuzzHandler implements ChainHandlerInterface
{
    public function supports(ChainHandlerContext $context): bool
    {
        return $context instanceof FizzBuzzContext
            && $context->number % 15 === 0;
    }

    public function handle(ChainHandlerContext $context): ChainHandlerContext
    {
        $context->result = 'FizzBuzz';
        return $context;
    }
}
```

You can then add FizzHandler and BuzzHandler similarly and don't forget the DefaultNumberHandler.

#### Register Handlers in services.yaml

[](#register-handlers-in-servicesyaml)

```
App\FizzBuzz\FizzBuzzHandler:
    tags:
        - { name: 'chain.handler', chain: 'fizzbuzz', priority: 300 }
```

#### Run the Chain

[](#run-the-chain)

Finally, you can run the chain in your controller or service:

```
/** @var ChainExecutor $executor */
$executor = $container->get(ChainExecutor::class);

$context = new FizzBuzzContext(15);
$resultContext = $executor->execute('fizzbuzz', $context, true);

echo $resultContext->result; // Outputs: FizzBuzz
```

Contribution
------------

[](#contribution)

Feel free to open issues for any bug, feature request, or other ideas.

Please remember to create an issue before creating large pull requests.

### Local Development

[](#local-development)

To develop on local machine, the vendor dependencies are required.

```
bin/composer install
```

We use composer scripts for our main quality tools. They can be executed via the `bin/composer` file as well.

```
bin/composer cs:fix
bin/composer phpstan
bin/composer tests
```

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance54

Moderate activity, may be stable

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

342d ago

### Community

Maintainers

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

---

Top Contributors

[![mike4git](https://avatars.githubusercontent.com/u/5848798?v=4)](https://github.com/mike4git "mike4git (24 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/mike4git-chain-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/mike4git-chain-bundle/health.svg)](https://phpackages.com/packages/mike4git-chain-bundle)
```

###  Alternatives

[symfony/maker-bundle

Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.

3.4k111.1M568](/packages/symfony-maker-bundle)[pentatrion/vite-bundle

Vite integration for your Symfony app

2755.3M13](/packages/pentatrion-vite-bundle)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[scheb/2fa

Two-factor authentication for Symfony applications (please use scheb/2fa-bundle to install)

578630.7k1](/packages/scheb-2fa)[contao/core-bundle

Contao Open Source CMS

1231.6M2.4k](/packages/contao-core-bundle)

PHPackages © 2026

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