PHPackages                             shopie/mediator - 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. shopie/mediator

ActiveLibrary

shopie/mediator
===============

Essential mediator implementation

v2.0.2(2y ago)0438MITPHP

Since Nov 12Pushed 2y agoCompare

[ Source](https://github.com/Shopie-App/mediator)[ Packagist](https://packagist.org/packages/shopie/mediator)[ RSS](/packages/shopie-mediator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (5)Used By (0)

Mediator
========

[](#mediator)

Essential mediator implementation.

Use Mediator when you want to handle communication between loosely coupled objects.

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

[](#installation)

```
php composer require shopie/mediator
```

### Send a request and get a response back

[](#send-a-request-and-get-a-response-back)

```
// A message we want to send to a handler
// Should extend Request class
// Define the worker handler with the MediatorHandler attribute
#[MediatorHandler(RequestNotificationHandler::class)]
class RequestNotification extends Request
{
    public function __construct(
        private int $id,
        private string $name,
        private bool $isActive
    ) {
    }

    public function isActive(): bool
    {
        return $this->isActive;
    }
}

// A handler that does something with the message
// our message's handler
class RequestNotificationHandler
{
    public function __construct()
    {
    }

    public function handle(RequestNotification $notification): MediatorResult
    {
        // example error return
        if (!$notification->isActive()) {
            return new MediatorResult('User is not activated');
        }

        // ...do work

        // return result
        // 1st argument: error string, 2nd argument: result object
        // result object can be anything
        return new MediatorResult(null, true);
    }
}

// init Mediator, send message, get result back
$result = (new Mediator())->send(new RequestNotification());

// check $result->status
// failed equals:
MediatorResultStatus::FAILED
// success equals:
MediatorResultStatus::SUCCESS
```

### Publish a message to a queue

[](#publish-a-message-to-a-queue)

```
// A message we want to send to a handler
// Should extend Notification class
// Define the worker handler with the MediatorHandler attribute
#[MediatorHandler(TestMessageHandler::class)]
class TestMessage extends Notification
{
    public function __construct(
        public readonly int $id,
        public readonly string $message
    ) {
    }
}

// A handler that does something with the message
class TestMessageHandler
{
    public function __construct()
    {
    }

    public function handle(TestMessage $notification): void
    {
        // .. pushes to some messaging queue
    }
}

// init Mediator, publish message
 (new Mediator())->publish(new TestMessage(1, 'This is a test notification'));
```

### Injecting dependencies to handlers

[](#injecting-dependencies-to-handlers)

Use [shopie/di-container](https://github.com/Shopie-App/di-container) IoC container if you want to inject dependencies to handlers.

```
// Prototype example container initialization in an App class
class App
{
    /**
     * Services are added to the container.
     */
    private ServiceContainerInterface $container;

    /**
     * Services are requested from provider.
     */
    private ServiceProviderInterface $provider;

    public function __construct()
    {
    }

    public function initContainer()
    {
        $collection = new ServiceCollection();

        $this->container = new ServiceContainer($collection);

        $this->provider = new ServiceProvider($collection);
    }

    public function addServices()
    {
        // add mediator to container
        $this->container->addScoped(MediatorInterface::class, Mediator::class);

        // init mediator
        $mediator = $this->provider->getService(Mediator::class);

        // add service provider to mediator
        $mediator->setServiceProvider($this->provider);
    }
}

// Using the App class
$app = new App();
$app->initContainer();
$app->addServices();
```

Now objects can be injected to the handlers.

```
// A handler with dependencies
class TestMessageHandler
{
    public function __construct(private MyRepository $repository)
    {
    }

    public function handle(TestMessage $notification): void
    {
        // do work
        $this->repository->add($notification);
    }
}
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity45

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

Every ~23 days

Total

4

Last Release

845d ago

Major Versions

v1.0.0 → v2.0.02024-01-04

### Community

Maintainers

![](https://www.gravatar.com/avatar/11f8e4de664e4a59504a983234498dcfbdc4b84a154a9f4957fc8f2f11105ac7?d=identicon)[shopie](/maintainers/shopie)

---

Top Contributors

[![ShopieApp](https://avatars.githubusercontent.com/u/72389353?v=4)](https://github.com/ShopieApp "ShopieApp (8 commits)")

### Embed Badge

![Health badge](/badges/shopie-mediator/health.svg)

```
[![Health](https://phpackages.com/badges/shopie-mediator/health.svg)](https://phpackages.com/packages/shopie-mediator)
```

PHPackages © 2026

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