Skip to content

Commit

Permalink
Merge pull request #56 from LibreSign/feature/add-hability-run-as
Browse files Browse the repository at this point in the history
Add hability run as
  • Loading branch information
vitormattos committed May 28, 2023
2 parents 38e05aa + 4685828 commit b7f5e3b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,4 +79,4 @@ class FeatureContext implements Context
}
}
}
```
```
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0"?>
<psalm
errorBaseline="tests/psalm-baseline.xml"
errorLevel="2"
findUnusedBaselineEntry="true"
findUnusedCode="false"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
12 changes: 10 additions & 2 deletions src/RunServerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ 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)
public function __construct(?int $verbose, string $rootDir, string $host, string $runAs)
{
$this->verbose = $verbose;
$this->rootDir = $rootDir;
$this->runAs = $runAs;
self::$host = $host;
self::$instance = $this;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down
16 changes: 15 additions & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;
}
Expand All @@ -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);
Expand All @@ -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');
Expand Down
8 changes: 8 additions & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.12.0@f90118cdeacd0088e7215e64c0c99ceca819e176">
<file src="src/Server.php">
<UndefinedInterfaceMethod>
<code>scalarNode</code>
</UndefinedInterfaceMethod>
</file>
</files>

0 comments on commit b7f5e3b

Please sign in to comment.