for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php declare(strict_types=1);
namespace Cocotte\Shell;
use Assert\Assertion;
final class DefaultEnv implements Env
{
public function put(string $name, string $value)
global $_SERVER;
global
Instead of relying on global state, we recommend one of these alternatives:
function myFunction($a, $b) { // Do something }
class MyClass { private $a; private $b; public function __construct($a, $b) { $this->a = $a; $this->b = $b; } public function myFunction() { // Do something } }
Assertion::true(putenv($name.'='.$value), "Could not put env with name '$name' and value '$value'.");
Assertion::same(
$value,
$retrieved = $this->get($name),
"Failed asserting that value '$value' for env with name '$name' has been preserved when putting it. ".
"Retrieved value was '$retrieved'."
);
/**
* @see \Symfony\Component\Process\Process::getDefaultEnv
*/
$_SERVER[$name] = $value;
}
public function get(string $name, $default = null): ?string
$value = getenv($name);
if (false !== $value) {
return $value;
return $default;
public function unset(string $name)
Assertion::true(putenv($name), "Could not unset env with name '$name'.");
null,
"Failed asserting that env with name '$name' has been unset. ".
unset($_SERVER[$name]);
Assertion::keyNotExists(
$_SERVER,
$name,
"Failed asserting that env with name '$name' has been unset in global _SERVER."
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state