Skip to content

Commit b7f5e3b

Browse files
authored
Merge pull request #56 from LibreSign/feature/add-hability-run-as
Add hability run as
2 parents 38e05aa + 4685828 commit b7f5e3b

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ default:
2424
2525
### Config values
2626
27-
| config | default | Environment | Description |
28-
| ------- | ------------- | -------------- | ----------------------------- |
29-
| verbose | false | none | Enables/disables verbose mode |
30-
| rootDir | /var/www/html | BEHAT_HOST | Specifies http root dir |
31-
| host | localhost | BEHAT_ROOT_DIR | Host domain or IP |
27+
| config | default | Environment | Description |
28+
| ------- | ------------- | -------------- | -------------------------------------------------- |
29+
| verbose | false | none | Enables/disables verbose mode |
30+
| rootDir | /var/www/html | BEHAT_HOST | Specifies http root dir |
31+
| host | localhost | BEHAT_ROOT_DIR | Host domain or IP |
32+
| runAs | | BEHAT_RUN_AS | The username to be used to run the built-in server |
3233
3334
You can also use `-v` option to enable verbose mode. Example
3435
```bash
@@ -78,4 +79,4 @@ class FeatureContext implements Context
7879
}
7980
}
8081
}
81-
```
82+
```

psalm.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?xml version="1.0"?>
22
<psalm
3+
errorBaseline="tests/psalm-baseline.xml"
34
errorLevel="2"
5+
findUnusedBaselineEntry="true"
6+
findUnusedCode="false"
47
resolveFromConfigFile="true"
58
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
69
xmlns="https://getpsalm.org/schema/config"

src/RunServerListener.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ class RunServerListener implements EventSubscriberInterface
3636
private static int $port = 0;
3737
private ?int $verbose = null;
3838
private string $rootDir;
39+
private string $runAs = '';
3940
private static self $instance;
4041

41-
public function __construct(?int $verbose, string $rootDir, string $host)
42+
public function __construct(?int $verbose, string $rootDir, string $host, string $runAs)
4243
{
4344
$this->verbose = $verbose;
4445
$this->rootDir = $rootDir;
46+
$this->runAs = $runAs;
4547
self::$host = $host;
4648
self::$instance = $this;
4749
}
@@ -79,6 +81,10 @@ public function start(): void
7981

8082
$cmd = 'php -S ' . self::$host .':' . self::$port . ' -t ' . $script;
8183

84+
if ($this->runAs) {
85+
$cmd = 'runuser -u ' . $this->runAs . ' -- ' . $cmd;
86+
}
87+
8288
if (is_numeric($this->verbose)) {
8389
$verbose = '';
8490
} else {
@@ -195,8 +201,10 @@ public function getPort()
195201
* Let the OS find an open port for you.
196202
*
197203
* @return int
204+
*
205+
* @psalm-return int<1, max>
198206
*/
199-
private function findOpenPort()
207+
private function findOpenPort(): int
200208
{
201209
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
202210

src/Server.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public function configure(ArrayNodeDefinition $builder): void
7272
->info('Host domain or IP')
7373
->defaultValue('localhost')
7474
->end()
75+
->scalarNode('runAs')
76+
->info('The username to be used to run the built-in server')
77+
->defaultValue('')
78+
->end()
7579
->end()
7680
;
7781
}
@@ -88,9 +92,10 @@ public function load(ContainerBuilder $container, array $config): void
8892
}
8993
}
9094
$host = $this->getHost($config);
95+
$runAs = $this->getRunAs($config);
9196
$definition = (new Definition('PhpBuiltin\RunServerListener'))
9297
->addTag('event_dispatcher.subscriber')
93-
->setArguments([$verbose, $rootDir, $host])
98+
->setArguments([$verbose, $rootDir, $host, $runAs])
9499
;
95100

96101
$container->setDefinition(self::ID . '.listener', $definition);
@@ -105,6 +110,15 @@ private function getHost(array $config): string
105110
return (string) $host;
106111
}
107112

113+
private function getRunAs(array $config): string
114+
{
115+
$runAs = getenv('BEHAT_RUN_AS');
116+
if ($runAs === false) {
117+
$runAs = $config['runAs'];
118+
}
119+
return (string) $runAs;
120+
}
121+
108122
private function getRootDir(array $config): string
109123
{
110124
$rootDir = getenv('BEHAT_ROOT_DIR');

tests/psalm-baseline.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<files psalm-version="5.12.0@f90118cdeacd0088e7215e64c0c99ceca819e176">
3+
<file src="src/Server.php">
4+
<UndefinedInterfaceMethod>
5+
<code>scalarNode</code>
6+
</UndefinedInterfaceMethod>
7+
</file>
8+
</files>

0 commit comments

Comments
 (0)