diff --git a/README.md b/README.md index 49cbda3..0589f83 100644 --- a/README.md +++ b/README.md @@ -24,11 +24,12 @@ default: ### Config values -| config | default | Environment | Description | -| ------- | ------------- | -------------- | ----------------------------- | -| verbose | false | none | Enables/disables verbose mode | -| rootDir | /var/www/html | BEHAT_HOST | Specifies http root dir | -| host | localhost | BEHAT_ROOT_DIR | Host domain or IP | +| config | default | Environment | Description | +| ------- | ------------- | -------------- | -------------------------------------------------- | +| verbose | false | none | 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 | You can also use `-v` option to enable verbose mode. Example ```bash @@ -78,4 +79,4 @@ class FeatureContext implements Context } } } -``` \ No newline at end of file +``` diff --git a/psalm.xml b/psalm.xml index 4e9226b..065d4b3 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,9 @@ verbose = $verbose; $this->rootDir = $rootDir; + $this->runAs = $runAs; self::$host = $host; self::$instance = $this; } @@ -79,6 +81,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 +201,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'); diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml new file mode 100644 index 0000000..4e198d3 --- /dev/null +++ b/tests/psalm-baseline.xml @@ -0,0 +1,8 @@ + + + + + scalarNode + + +