Skip to content

Commit

Permalink
Merge pull request #67 from LibreSign/feature/add-support-to-workers
Browse files Browse the repository at this point in the history
Add support to workers
  • Loading branch information
vitormattos authored Dec 9, 2023
2 parents c5d5b97 + 134fda3 commit 2b399be
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/RunServerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
22 changes: 21 additions & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;
}
Expand All @@ -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');
Expand All @@ -94,11 +104,12 @@ public function load(ContainerBuilder $container, array $config): void
$output->writeln('<info>Verbose: ' . $verbose . '</info>');
$output->writeln('<info>Host: ' . $host . '</info>');
$output->writeln('<info>RunAs: ' . $runAs . '</info>');
$output->writeln('<info>Workers: ' . $workers . '</info>');
}
}
$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);
Expand All @@ -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');
Expand Down

0 comments on commit 2b399be

Please sign in to comment.