robertwesner/simple-mvc-php-spawner-bundle

Run background tasks that do not produce synchronous output or require monitoring.

v1.0.1 2025-06-08 14:34 UTC

This package is auto-updated.

Last update: 2025-06-08 14:38:18 UTC


README

Simple MVC Spawner Bundle

License: MIT

Run background tasks that do not produce synchronous output or require monitoring.

Fire and forget.

Created for my YouTube playlist viewer with pagination.

Installation

composer require robertwesner/simple-mvc-php-spawner-bundle

Make sure to load the bundle in your $PROJECT_ROOT$/configurations/bundles.php.

use RobertWesner\SimpleMvcPhp\Configuration;
use RobertWesner\SimpleMvcPhpSpawnerBundle\SpawnerBundle;
use RobertWesner\SimpleMvcPhpSpawnerBundle\SpawnerBundleConfiguration;

Configuration::BUNDLES
    ::load(
        SpawnerBundle::class,
        // optional, can be omitted
        new SpawnerBundleConfiguration(
            preExecuteScriptPath: __BASE_DIR__ . '/pre-command.php',
        ),
    );

Usage

Create configuration

final readonly class MySpawnConfiguration implements SpawnConfigurationInterface
{
    public function __construct(
        private string $foo,
        private string $bar,
    ) {}

    public function __serialize(): array
    {
        return [
            'foo' => $this->foo,
            'bar' => $this->bar,
        ];
    }

    public function __unserialize(array $data): void
    {
        $this->foo = $data['foo'];
        $this->bar = $data['bar'];
    }

    public function getFoo(): string
    {
        return $this->foo;
    }

    public function getBar(): string
    {
        return $this->bar;
    }
}

Create background task

class MySpawn implements SpawnInterface
{
    public function run(SpawnConfigurationInterface|MySpawnConfiguration $configuration): void
    {
        // asynchronous task with $configuration->getFoo(), etc...
    }
}

Execute

// spawner accessed through dependency injection
$this->spawner->spawn(
    EchoSpawn::class,
    new EchoSpawnConfiguration(
        'Some value',
        'Another value',
    ),
);