diff --git a/README.md b/README.md index 715bceb..d5509c6 100644 --- a/README.md +++ b/README.md @@ -24,12 +24,13 @@ default: ### Config values -| config | default | Environment | Description | -| ------- | ------------- | -------------- | -------------------------------------------------- | -| verbose | false | BEHAT_VERBOSE | Enables/disables verbose mode | -| rootDir | /var/www/html | BEHAT_HOST | Specifies http root dir | -| host | localhost | BEHAT_ROOT_DIR | Host domain or IP | -| runAs | | BEHAT_RUN_AS | The username to be used to run the built-in server | +| config | default | Environment | Description | +| ------- | ------------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| verbose | false | BEHAT_VERBOSE | Enables/disables verbose mode | +| rootDir | /var/www/html | BEHAT_HOST | Specifies http root dir | +| host | localhost | BEHAT_ROOT_DIR | Host domain or IP | +| runAs | | BEHAT_RUN_AS | The username to be used to run the built-in server | +| workers | 0 | BEHAT_WORKERS | The quantity of workers to use. More informations [here](https://www.php.net/manual/en/features.commandline.webserver.php) searching by `PHP_CLI_SERVER_WORKERS` | You can also use `-v` option to enable verbose mode. Example ```bash diff --git a/src/RunServerListener.php b/src/RunServerListener.php index aa4f41e..5a3c144 100644 --- a/src/RunServerListener.php +++ b/src/RunServerListener.php @@ -37,13 +37,15 @@ class RunServerListener implements EventSubscriberInterface private ?int $verbose = null; private string $rootDir; private string $runAs = ''; + private int $workers = 0; private static self $instance; - public function __construct(?int $verbose, string $rootDir, string $host, string $runAs) + public function __construct(?int $verbose, string $rootDir, string $host, string $runAs, int $workers) { $this->verbose = $verbose; $this->rootDir = $rootDir; $this->runAs = $runAs; + $this->workers = $workers; self::$host = $host; self::$instance = $this; } @@ -81,6 +83,10 @@ public function start(): void $cmd = 'php -S ' . self::$host .':' . self::$port . ' -t ' . $script; + if ($this->workers > 0) { + $cmd = 'PHP_CLI_SERVER_WORKERS=' . $this->workers . ' ' . $cmd; + } + if ($this->runAs && get_current_user() !== $this->runAs) { $cmd = 'runuser -u ' . $this->runAs . ' -- ' . $cmd; } diff --git a/src/Server.php b/src/Server.php index f8d81b1..b5d0c4a 100644 --- a/src/Server.php +++ b/src/Server.php @@ -76,6 +76,15 @@ public function configure(ArrayNodeDefinition $builder): void ->info('The username to be used to run the built-in server') ->defaultValue('') ->end() + ->scalarNode('workers') + ->info( + 'The quantity of workers to use. ' . + 'More informations here ' . + 'https://www.php.net/manual/en/features.commandline.webserver.php ' . + 'searching by PHP_CLI_SERVER_WORKERS' + ) + ->defaultValue(0) + ->end() ->end() ; } @@ -86,6 +95,7 @@ public function load(ContainerBuilder $container, array $config): void $rootDir = $this->getRootDir($config); $host = $this->getHost($config); $runAs = $this->getRunAs($config); + $workers = $this->getWorkers($config); $verbose = $this->getVerboseLevel($container, $config); if (is_numeric($verbose)) { $output = $container->get('cli.output'); @@ -94,11 +104,12 @@ public function load(ContainerBuilder $container, array $config): void $output->writeln('Verbose: ' . $verbose . ''); $output->writeln('Host: ' . $host . ''); $output->writeln('RunAs: ' . $runAs . ''); + $output->writeln('Workers: ' . $workers . ''); } } $definition = (new Definition('PhpBuiltin\RunServerListener')) ->addTag('event_dispatcher.subscriber') - ->setArguments([$verbose, $rootDir, $host, $runAs]) + ->setArguments([$verbose, $rootDir, $host, $runAs, $workers]) ; $container->setDefinition(self::ID . '.listener', $definition); @@ -122,6 +133,15 @@ private function getRunAs(array $config): string return (string) $runAs; } + private function getWorkers(array $config): string + { + $workers = getenv('BEHAT_WORKERS'); + if ($workers === false) { + $workers = $config['workers']; + } + return (string) $workers; + } + private function getRootDir(array $config): string { $rootDir = getenv('BEHAT_ROOT_DIR');