Skip to content

Commit b87f41b

Browse files
authored
Add support for passing env vars to PHP (#914)
* Add support for passing env vars to PHP * Added docs
1 parent e087dfd commit b87f41b

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

docs/configuration.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,17 @@ Default: ``NULL``
240240

241241
Types: ``["string","null"]``
242242

243+
.. _configuration_runner_php_env:
244+
245+
runner.php_env
246+
~~~~~~~~~~~~~~
247+
248+
Key-value set of environment variables to pass to the PHP process
249+
250+
Default: ``NULL``
251+
252+
Types: ``["array","null"]``
253+
243254
.. _configuration_runner_progress:
244255

245256
runner.progress

lib/Extension/RunnerExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class RunnerExtension implements ExtensionInterface
8282
public const PARAM_PHP_CONFIG = 'runner.php_config';
8383
public const PARAM_PHP_DISABLE_INI = 'runner.php_disable_ini';
8484
public const PARAM_PHP_WRAPPER = 'runner.php_wrapper';
85+
public const PARAM_PHP_ENV = 'runner.php_env';
8586
public const PARAM_PROGRESS = 'runner.progress';
8687
public const PARAM_PROGRESS_SUMMARY_BASELINE_FORMAT = 'runner.progress_summary_baseline_format';
8788
public const PARAM_PROGRESS_SUMMARY_FORMAT = 'runner.progress_summary_variant_format';
@@ -131,6 +132,7 @@ public function configure(OptionsResolver $resolver): void
131132
self::PARAM_PHP_CONFIG => [],
132133
self::PARAM_PHP_DISABLE_INI => false,
133134
self::PARAM_PHP_WRAPPER => null,
135+
self::PARAM_PHP_ENV => null,
134136
self::PARAM_PROGRESS => 'verbose',
135137
self::PARAM_PROGRESS_SUMMARY_FORMAT => VariantSummaryFormatter::DEFAULT_FORMAT,
136138
self::PARAM_PROGRESS_SUMMARY_BASELINE_FORMAT => VariantSummaryFormatter::BASELINE_FORMAT,
@@ -163,6 +165,7 @@ public function configure(OptionsResolver $resolver): void
163165
$resolver->setAllowedTypes(self::PARAM_PHP_CONFIG, ['array']);
164166
$resolver->setAllowedTypes(self::PARAM_PHP_DISABLE_INI, ['bool']);
165167
$resolver->setAllowedTypes(self::PARAM_PHP_WRAPPER, ['string', 'null']);
168+
$resolver->setAllowedTypes(self::PARAM_PHP_ENV, ['array', 'null']);
166169
$resolver->setAllowedTypes(self::PARAM_PROGRESS, ['string']);
167170
$resolver->setAllowedTypes(self::PARAM_PROGRESS_SUMMARY_BASELINE_FORMAT, ['string']);
168171
$resolver->setAllowedTypes(self::PARAM_PROGRESS_SUMMARY_FORMAT, ['string']);
@@ -195,6 +198,7 @@ public function configure(OptionsResolver $resolver): void
195198
self::PARAM_PHP_CONFIG => 'Map of PHP ini settings to use when executing out-of-band benchmarks',
196199
self::PARAM_PHP_DISABLE_INI => 'Disable reading the default PHP configuration',
197200
self::PARAM_PHP_WRAPPER => 'Wrap the PHP binary with this command (e.g. ``blackfire run``)',
201+
self::PARAM_PHP_ENV => 'Key-value set of environment variables to pass to the PHP process',
198202
self::PARAM_PROGRESS => 'Default progress logger to use',
199203
self::PARAM_PROGRESS_SUMMARY_FORMAT => 'Expression used to render the summary text default progress loggers',
200204
self::PARAM_PROGRESS_SUMMARY_BASELINE_FORMAT => 'When the a comparison benchmark is referenced, alternative expression used to render the summary text default progress loggers',
@@ -370,7 +374,10 @@ private function registerBenchmark(Container $container): void
370374
});
371375

372376
$container->register(ProcessFactory::class, function (Container $container) {
373-
return new ProcessFactory($container->get(LoggerInterface::class));
377+
return new ProcessFactory(
378+
$container->get(LoggerInterface::class),
379+
$container->getParameter(self::PARAM_PHP_ENV)
380+
);
374381
});
375382

376383
$container->register(Launcher::class, function (Container $container) {

lib/Remote/ProcessFactory.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,28 @@ final class ProcessFactory implements ProcessFactoryInterface
1313
*/
1414
private $logger;
1515

16-
public function __construct(?LoggerInterface $logger = null)
16+
/**
17+
* @var array|null
18+
*/
19+
private $env;
20+
21+
public function __construct(?LoggerInterface $logger = null, ?array $env = null)
1722
{
1823
$this->logger = $logger ?: new NullLogger();
24+
$this->env = $env;
1925
}
2026

2127
public function create(string $commandLine, ?float $timeout = null): Process
2228
{
2329
$this->logger->debug(sprintf('Spawning process: %s', $commandLine));
2430

25-
return Process::fromShellCommandline($commandLine)
31+
$process = Process::fromShellCommandline($commandLine)
2632
->setTimeout($timeout);
33+
34+
if (null !== $this->env) {
35+
$process->setEnv($this->env);
36+
}
37+
38+
return $process;
2739
}
2840
}

phpbench.schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@
138138
"null"
139139
]
140140
},
141+
"runner.php_env": {
142+
"description": "Key-value set of environment variables to pass to the PHP process",
143+
"type": [
144+
"object",
145+
"null"
146+
]
147+
},
141148
"runner.progress": {
142149
"description": "Default progress logger to use",
143150
"type": [
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace PhpBench\Tests\Unit\Remote;
4+
5+
use PhpBench\Remote\ProcessFactory;
6+
use PHPUnit\Framework\TestCase;
7+
use Psr\Log\NullLogger;
8+
9+
class ProcessFactoryTest extends TestCase
10+
{
11+
public function testCreatesProcessWithEnv(): void
12+
{
13+
$process = (new ProcessFactory(new NullLogger(), [
14+
'FOO' => 'hello',
15+
]))->create('echo $FOO');
16+
$process->mustRun();
17+
self::assertStringContainsString('hello', $process->getOutput());
18+
}
19+
20+
public function testCreatesProcessInheritsEnv(): void
21+
{
22+
$_ENV['FOO'] = 'bar';
23+
$process = (new ProcessFactory(new NullLogger(), ['BAZ' => 'boo']))->create('echo $FOO$BAZ');
24+
$process->mustRun();
25+
self::assertStringContainsString('barboo', $process->getOutput());
26+
}
27+
}

0 commit comments

Comments
 (0)