PHPackages                             sroze/chain-of-responsibility - 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. sroze/chain-of-responsibility

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

sroze/chain-of-responsibility
=============================

A light library that simplify the implementation of a chain of responsibility

1.2.1(9y ago)2410.7k↑326.7%31MITPHP

Since Apr 4Pushed 9y ago1 watchersCompare

[ Source](https://github.com/sroze/ChainOfResponsibility)[ Packagist](https://packagist.org/packages/sroze/chain-of-responsibility)[ Docs](https://github.com/sroze/ChainOfResponsibility)[ RSS](/packages/sroze-chain-of-responsibility/feed)WikiDiscussions master Synced today

READMEChangelog (4)Dependencies (3)Versions (6)Used By (1)

Chain Of Responsibility
=======================

[](#chain-of-responsibility)

[![Build Status](https://camo.githubusercontent.com/2bae771e5d5ffb4b2074dd725e40b0a9e093074316900d68fadd158d514e34b8/68747470733a2f2f7472617669732d63692e6f72672f73726f7a652f436861696e4f66526573706f6e736962696c6974792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/sroze/ChainOfResponsibility)[![SensioLabsInsight](https://camo.githubusercontent.com/98fb1f88184ca0653755cf03ca65ab31c913298df156f3c4bae9247bdcda5e3a/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f37636130663037322d346231622d343764612d623638632d3530393038353336366361662f6d696e692e706e67)](https://insight.sensiolabs.com/projects/7ca0f072-4b1b-47da-b68c-509085366caf)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/6d421c92189f3ba349b828ad65f86af2e8c7b76259cdbfa51f65db48648cf757/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73726f7a652f436861696e4f66526573706f6e736962696c6974792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/sroze/ChainOfResponsibility/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/5c5fd0b2753dfcdc5bc93e4930f5c82b750ecba538de1d6d61d74a2184be582c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f73726f7a652f436861696e4f66526573706f6e736962696c6974792f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/sroze/ChainOfResponsibility/?branch=master)

This light library helps to implement quickly a chain of responsibility. This pattern is especially useful when you need a clear process that involve multiple steps.

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

[](#installation)

The suggested installation method is via composer:

```
composer require sroze/chain-of-responsibility

```

Usage
-----

[](#usage)

There's two components:

- [The Runner](#runner) that will take a brunch of processes, decorates them to create the chain-of-responsibility and execute the head.
- [The builder](#builder) adds a little bit of like to processes because they can declare a list of other processes that needs to be run before.

### Runner

[](#runner)

Create your processes classes that implements the `ChainProcessInterface`. This interface only has one `execute` method that take a `ChainContext` object as parameter. This parameter is usable like an array and will provides your processes a common way to exchange information between them.

```
use SRIO\ChainOfResponsibility\ChainContext;
use SRIO\ChainOfResponsibility\ChainProcessInterface;

class LambdaProcess implements ChainProcessInterface
{
    /**
     * {@inheritdoc}
     */
    public function execute(ChainContext $context)
    {
        // Do whatever you want in this small process, such as
        // sending a mail, manipulating files, ...
    }
}
```

Create the chain runner with a list of given processes. The list order is quite important since it'll be the order of execution of processes.

```
$runner = new ChainRunner([
    new FirstProcess(),
    new SecondProcess(),
    new ThirdProcess()
]);
```

Then, use the `run` method to run each process, with an **optional** `ChainContext` argument.

```
$runner->run(new ArrayChainContext([
    'foo' => 'bar'
]));
```

Builder
-------

[](#builder)

The builder is able to construct your chain of responsibility based on dependencies between processes. To declare dependencies, your process have to implement the `DependentChainProcessInterface` interface.

```
use SRIO\ChainOfResponsibility\ChainContext;
use SRIO\ChainOfResponsibility\DependentChainProcessInterface;

class FooProcess implements DependentChainProcessInterface
{
    /**
     * {@inheritdoc}
     */
    public function execute(ChainContext $context)
    {
        // Do whatever you want...
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'foo';
    }

    /**
     * {@inheritdoc}
     */
    public function dependsOn()
    {
        return ['bar'];
    }
}
```

Then, create another `BarProcess` which `getName` method will return `bar` and its `dependsOn` will return an empty array.

Create a `ChainBuilder` by creating an instance of it and pass an array of processes that implement at least the `ChainProcessInterface`. As the `ChainRunner`, you also can call the `add` method to add one or more processes.

```
$builder = new ChainBuilder([
    new FooProcess(),
    new BarProcess(),
]);
```

You now can retrieve the chain runner by calling the `getRunner` method.

```
$runner = $builder->getRunner();
$runner->run();
```

We the given previous example, the `bar` process will be executed before the `foo` one.

Advanced
--------

[](#advanced)

### Use a custom process decorator

[](#use-a-custom-process-decorator)

To create the chain, the `ChainBuilder` decorate your processes with the `ChainProcessDecorator` class. If you want to use your own decorator to add additional logic such as error recovering or logging, you can easily override the decorator by providing an object that implements `DecoratorFactoryInterface`.

```
$decoratorFactory = new DecoratorFactory();
$runner = new ChainRunner([], $decoratorFactory);
```

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

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

Total

4

Last Release

3411d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/58b8aaf662150e054108b611b620d1cf8c1cb91e00f76d3dfcd2d7a69a10991c?d=identicon)[sroze](/maintainers/sroze)

---

Top Contributors

[![sroze](https://avatars.githubusercontent.com/u/804625?v=4)](https://github.com/sroze "sroze (15 commits)")[![mareg](https://avatars.githubusercontent.com/u/533217?v=4)](https://github.com/mareg "mareg (1 commits)")

---

Tags

dependencyChainresponsibilitychain of responsibility

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sroze-chain-of-responsibility/health.svg)

```
[![Health](https://phpackages.com/badges/sroze-chain-of-responsibility/health.svg)](https://phpackages.com/packages/sroze-chain-of-responsibility)
```

###  Alternatives

[composer/composer

Composer helps you declare, manage and install dependencies of PHP projects. It ensures you have the right stack everywhere.

29.5k196.2M3.1k](/packages/composer-composer)[bamarni/composer-bin-plugin

No conflicts for your bin dependencies

53024.4M1.1k](/packages/bamarni-composer-bin-plugin)[php-di/invoker

Generic and extensible callable invoker

26764.7M69](/packages/php-di-invoker)[dephpend/dephpend

Dependency analysis for PHP

533102.9k2](/packages/dephpend-dephpend)[contributte/di

Extra contrib to nette/di

475.9M18](/packages/contributte-di)[digital-creative/conditional-container

Provides an easy way to conditionally show and hide fields in your Nova resources.

114606.7k4](/packages/digital-creative-conditional-container)

PHPackages © 2026

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