Skip to main content

Symfony Integration

This bundle integrates PHPStreamServer with the Symfony Runtime component.

Installation

$ composer require phpstreamserver/symfony

Runtime Configuration

Enable the Bundle

config/bundles.php
<?php

return [
// ...
PHPStreamServer\Symfony\PHPStreamServerBundle::class => ['all' => true],
];

Set PHPStreamServerRuntime as the Application Runtime

Set the APP_RUNTIME environment variable to PHPStreamServer\Symfony\PHPStreamServerRuntime, or specify the class through extra.runtime.class in composer.json:

composer.json
{
"require": {
"...": "..."
},
"extra": {
"runtime": {
"class": "PHPStreamServer\\Symfony\\PHPStreamServerRuntime"
}
}
}

Create config/phpss.config.php

config/phpss.config.php
<?php

use PHPStreamServer\Core\ReloadStrategy\ExceptionReloadStrategy;
use PHPStreamServer\Core\Server;
use PHPStreamServer\Symfony\Worker\SymfonyHttpServerWorker;

return static function (Server $server): void {
$server->addWorker(new SymfonyHttpServerWorker(
listen: '0.0.0.0:80',
count: 1,
reloadStrategies: [
new ExceptionReloadStrategy(),
],
));
};

The closure returned from the config/phpss.config.php may have zero or more arguments.
The following arguments are supported:

  • Server $server: server instance used to register plugins and workers
  • array $context: this is the same as $_SERVER + $_ENV
  • string $projectDir: project root directory
  • string $env: current environment
  • bool $debug: whether debug mode is enabled

Create bin/phpss

bin/phpss
#!/usr/bin/env php
<?php

use App\Kernel;
use PHPStreamServer\Symfony\ServerApplication;

require_once \dirname(__DIR__) . '/vendor/autoload_runtime.php';

return new ServerApplication(static function (array $context): Kernel {
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
});

Start the Server

$ bin/phpss start

This bundle adds the Symfony-specific --env and --no-debug options to the start command. For details, see the command's help output.

Worker Configuration

⚙️ SymfonyHttpServerWorker

Worker class: SymfonyHttpServerWorker
This worker type is designed to run the Symfony application web server.

ParameterTypeDefaultDescription
listenstring|Listen|string[]|Listen[]requiredAddress or addresses on which to listen.
countint|nullnullOptional. Number of worker processes. Must be between 1 and 1024. Defaults to the detected CPU count.
reloadablebooltrueOptional. Whether the worker can be reloaded.
userstring|nullnullOptional. Unix user name or user ID (UID).
groupstring|nullnullOptional. Unix group name or group ID (GID).
middlewareMiddleware[][]Optional. Additional HTTP middleware.
reloadStrategiesReloadStrategy[][]Optional. Strategies for triggering automatic worker reloads.
accessLogbooltrueOptional. Whether to log incoming HTTP requests.
gzipboolfalseOptional. Enables response compression. Requires ext-zlib.
connectionLimitint|nullnullOptional. Maximum number of simultaneous connections per worker. Disabled by default.
connectionLimitPerIpint|nullnullOptional. Maximum number of simultaneous connections per IPv4 address or IPv6 /56 block. Loopback addresses are exempt.
concurrencyLimitint|null1*Optional. Maximum number of concurrent requests per worker. Defaults to 1; set to null to disable.

* Symfony applications generally assume that each PHP process handles one request at a time. Shared services and request-specific state may not be safe when multiple requests run concurrently in the same worker, so concurrencyLimit defaults to 1.

⚙️ SymfonyScheduledCommandWorker

Worker class: SymfonyScheduledCommandWorker
This worker type is designed to execute Symfony console commands periodically. The schedule parameter accepts:

  • A numeric string representing seconds (for example, '60')
  • An ISO8601 datetime format (for example, 2026-01-01T00:00:00Z)
  • An ISO8601 duration format (for example, PT1M)
  • A relative date format (for example, 1 minute)
  • A cron expression (for example, */1 * * * *)
ParameterTypeDefaultDescription
commandstringrequiredSymfony console command name with optional arguments and options.
namestring|nullnullOptional. Worker name. Defaults to command name.
schedulestring1 minuteOptional. Schedule in one of the formats described above.
jitterint0Optional. Maximum random delay, in seconds, added to the scheduled time. Set to 0 to disable.
userstring|nullnullOptional. Unix user name or user ID (UID).
groupstring|nullnullOptional. Unix group name or group ID (GID).

SymfonyScheduledCommandWorker additionally requires the Scheduler plugin.

⚙️ SymfonySupervisedCommandWorker

Worker class: SymfonySupervisedCommandWorker
This worker type is designed to run long-running Symfony console commands.

ParameterTypeDefaultDescription
commandstringrequiredSymfony console command name with optional arguments and options.
namestring|nullnullOptional. Worker name. Defaults to command name.
countint1Optional. Number of worker processes. Must be between 1 and 1024.
reloadablebooltrueOptional. Whether the worker can be reloaded.
userstring|nullnullOptional. Unix user name or user ID (UID).
groupstring|nullnullOptional. Unix group name or group ID (GID).
reloadStrategiesReloadStrategy[][]Optional. Strategies for triggering automatic worker reloads.

Integration with Monolog

If you use Monolog as the main logging system in Symfony, you can route all logs to the PHPStreamServer logger. This bundle provides a Monolog handler for seamless integration, which can be configured in the monolog.yaml file.

Install the Logger plugin and Monolog bundle:

$ composer require phpstreamserver/logger symfony/monolog-bundle

Register LoggerPlugin with the desired PHPStreamServer handlers in config/phpss.config.php before registering workers:

use PHPStreamServer\Plugin\Logger\Handler\ConsoleHandler;
use PHPStreamServer\Plugin\Logger\LoggerPlugin;

$server->addPlugin(
new LoggerPlugin(
new ConsoleHandler(),
),
);
config/packages/monolog.yaml
when@dev:
monolog:
handlers:
main:
type: service
id: phpss.monolog_handler
channels: ["!event", "!doctrine"]
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine", "!console"]

when@prod:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_http_codes: [404, 405]
buffer_size: 50 # How many messages should be saved? Prevent memory leaks
nested:
type: service
id: phpss.monolog_handler
channels: ["!event", "!doctrine"]
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine"]

Symfony Events

Symfony HTTP and command workers dispatch events during their normal lifecycle.

⏺️ WorkerStartEvent

Event Class: WorkerStartEvent
Triggered when a worker process starts.

⏺️ WorkerStopEvent

Event Class: WorkerStopEvent
Triggered when a worker process stops.

⏺️ WorkerReloadEvent

Event Class: WorkerReloadEvent
Triggered when a worker process is reloaded.