Skip to main content

Symfony Intergation

This bundle integrates PHPStreamServer Runtime with the Symfony framework.

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

Use the APP_RUNTIME environment variable or by specifying the extra.runtime.class in composer.json to change the Runtime class to PHPStreamServer\Symfony\PHPStreamServerRuntime.

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

Create config/phpss.config.php File

config/phpss.config.php
<?php

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

return static function (Server $server): void {
$server->addWorker(new SymfonyHttpServerProcess(
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 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 Is in debug mode

Create bin/phpss File

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 new Symfony-specific options to the start command: (--env, --no-debug). For more details, refer to the start command help output.

Worker Configuration

⚙️ SymfonyHttpServerProcess

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

OptionTypeDefaultDescription
listenstring|Listen|Listen[]not setThe address at which the server is listening.
countintnot setOptional. The number of processes to start. Defaults to number of CPUs.
reloadablebooltrueOptional. Whether the worker can be reloaded with the reload command.
userstringnot setOptional. Unix user of process. Current user by default.
groupstringnot setOptional. Unix group of process. Current group by default.
middlewareMiddleware[]not setOptional. A list of middlewares for processing HTTP requests.
reloadStrategiesReloadStrategy[]not setOptional. The strategies used to reload the worker.
accessLogbooltrueOptional. Whether to log incoming HTTP requests.
gzipboolfalseOptional. Enables gzip compression.
connectionLimitintnot setOptional. The maximum number of connections per worker.
connectionLimitPerIpintnot setOptional. The maximum number of connections allowed per IP.
concurrencyLimitintnot setOptional. The maximum number of concurrent HTTP requests per worker.

⚙️ SymfonyPeriodicProcess

Worker class: SymfonyPeriodicProcess
This worker type is designed to execute Symfony console commands periodically. The schedule option can be specified in one of the following formats:

  • Number of seconds (e.g., 60)
  • An ISO8601 datetime format (e.g., 2025-01-01 00:00:00)
  • An ISO8601 duration format (e.g., PT1M)
  • A relative date format (e.g., 1 minute)
  • A cron expression (e.g., */1 * * * *)
OptionTypeDefaultDescription
commandstringnot setSymfony console command name.
namestringnot setOptional. The name associated with the worker process.
scheduleint"1 minute"Optional. Schedule in one of the formats described above.
jitterint0Optional. Jitter in seconds that adds a random time offset to the schedule.
userintnot setOptional. Unix user of process. Current user by default.
groupintnot setOptional. Unix group of process. Current group by default.

⚙️ SymfonyWorkerProcess

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

OptionTypeDefaultDescription
commandstringnot setSymfony console command name.
namestringnot setOptional. The name associated with the worker process.
countint1Optional. The number of processes to start.
reloadablebooltrueOptional. Whether the worker can be reloaded with the reload command.
userstringnot setOptional. Unix user of process. Current user by default.
groupstringnot setOptional. Unix group of process. Current group by default.

Intergation 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.

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

During the workers' lifecycle, they dispatch events which you can use to get to know what happens with workers.

⏺️ ProcessStartEvent

Event Class: ProcessStartEvent
Triggered when a worker process starts.

⏺️ ProcessStopEvent

Event Class: ProcessStopEvent
Triggered when a worker process stops.

⏺️ ProcessReloadEvent

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