diff --git a/src/RunServerListener.php b/src/RunServerListener.php index f5d6dfa..abf3f5c 100644 --- a/src/RunServerListener.php +++ b/src/RunServerListener.php @@ -36,6 +36,7 @@ class RunServerListener implements EventSubscriberInterface private static int $port = 0; private ?int $verbose = null; private string $rootDir; + private string $runAs = ''; private static self $instance; public function __construct(?int $verbose, string $rootDir, string $host) @@ -79,6 +80,10 @@ public function start(): void $cmd = 'php -S ' . self::$host .':' . self::$port . ' -t ' . $script; + if ($this->runAs) { + $cmd = 'runuser -u ' . $this->runAs . ' -- ' . $cmd; + } + if (is_numeric($this->verbose)) { $verbose = ''; } else { @@ -195,8 +200,10 @@ public function getPort() * Let the OS find an open port for you. * * @return int + * + * @psalm-return int<1, max> */ - private function findOpenPort() + private function findOpenPort(): int { $sock = socket_create(AF_INET, SOCK_STREAM, 0); diff --git a/src/Server.php b/src/Server.php index d977eaa..be19fa1 100644 --- a/src/Server.php +++ b/src/Server.php @@ -72,6 +72,10 @@ public function configure(ArrayNodeDefinition $builder): void ->info('Host domain or IP') ->defaultValue('localhost') ->end() + ->scalarNode('runAs') + ->info('The username to be used to run the built-in server') + ->defaultValue('') + ->end() ->end() ; } @@ -88,9 +92,10 @@ public function load(ContainerBuilder $container, array $config): void } } $host = $this->getHost($config); + $runAs = $this->getRunAs($config); $definition = (new Definition('PhpBuiltin\RunServerListener')) ->addTag('event_dispatcher.subscriber') - ->setArguments([$verbose, $rootDir, $host]) + ->setArguments([$verbose, $rootDir, $host, $runAs]) ; $container->setDefinition(self::ID . '.listener', $definition); @@ -105,6 +110,15 @@ private function getHost(array $config): string return (string) $host; } + private function getRunAs(array $config): string + { + $runAs = getenv('BEHAT_RUN_AS'); + if ($runAs === false) { + $runAs = $config['runAs']; + } + return (string) $runAs; + } + private function getRootDir(array $config): string { $rootDir = getenv('BEHAT_ROOT_DIR');