fast-forward/fork
is meant for Unix-like CLI environments with pcntl
and posix
support. It is not a feature you should expect to work inside a regular PHP-FPM web request.
Use Cases
This page shows common scenarios for the metapackage. The goal is to help you distinguish between:
- services resolved from the container by
FrameworkServiceProvider - installed libraries that you use directly through Composer autoloading
Bootstrap an application container
use FastForward\Framework\ServiceProvider\FrameworkServiceProvider;
use function FastForward\Container\container;
$container = container(FrameworkServiceProvider::class);
This is the default starting point for web applications and small prototypes.
Build an event-driven application workflow
use FastForward\Config\ArrayConfig;
use FastForward\Container\ContainerInterface;
use FastForward\Framework\ServiceProvider\FrameworkServiceProvider;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\ListenerProviderInterface;
use function FastForward\Container\container;
final readonly class UserRegistered
{
public function __construct(public string $email) {}
}
final class SendWelcomeEmailListener
{
public function __invoke(UserRegistered $event): void
{
// Send the email.
}
}
$config = new ArrayConfig([
ContainerInterface::class => [
FrameworkServiceProvider::class,
],
ListenerProviderInterface::class => [
SendWelcomeEmailListener::class,
],
]);
$container = container($config);
$dispatcher = $container->get(EventDispatcherInterface::class);
$dispatcher->dispatch(new UserRegistered('demo@example.com'));
This is the most common pattern when your application starts mixing HTTP entry points with domain events.
Register your application services next to the framework
use App\ServiceProvider\AppServiceProvider;
use FastForward\Framework\ServiceProvider\FrameworkServiceProvider;
use function FastForward\Container\container;
$container = container(
FrameworkServiceProvider::class,
AppServiceProvider::class,
);
The later provider can add new services or override existing ones when it uses the same service identifiers.
Drive container setup from configuration
use App\ServiceProvider\AppServiceProvider;
use FastForward\Config\ArrayConfig;
use FastForward\Container\ContainerInterface;
use FastForward\Framework\ServiceProvider\FrameworkServiceProvider;
use function FastForward\Container\container;
$config = new ArrayConfig([
ContainerInterface::class => [
FrameworkServiceProvider::class,
AppServiceProvider::class,
],
]);
$container = container($config);
This pattern is helpful when you want environment-specific provider lists or a central config file.
Use the additional installed packages directly
The metapackage also installs useful runtime libraries that are not registered automatically by
FrameworkServiceProvider
.
Configuration example:
use function FastForward\Config\config;
$appConfig = config([
'app' => [
'name' => 'Demo',
'debug' => true,
],
]);
$debug = $appConfig->get('app.debug');
Deferred cleanup example:
use function FastForward\Defer\defer;
$cleanup = defer();
$cleanup(static fn() => print "Closing resources\n");
Iterator utility example:
use FastForward\Iterator\ChunkedIteratorAggregate;
foreach (new ChunkedIteratorAggregate(range(1, 6), 2) as $chunk) {
// [1, 2], [3, 4], [5, 6]
}
Parallel CLI workload example:
use FastForward\Fork\Manager\ForkManager;
use FastForward\Fork\Worker\WorkerInterface;
$manager = new ForkManager();
$workers = $manager->fork(
static fn(WorkerInterface $worker): int => 0,
2,
);
$workers->wait();