Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Created August 17, 2017 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplay-dk/f630b10daed0d8d0d8ef820284399817 to your computer and use it in GitHub Desktop.
Save mindplay-dk/f630b10daed0d8d0d8ef820284399817 to your computer and use it in GitHub Desktop.
Environment abstractions for PHP

Draft for a simple PSR providing a trivial abstraction of the local system environment.

Why? So we can stop using hacks to work around dependencies on system state - so that these dependencies can be made visible as (constructor) dependencies, and so we can mock the implementations under test.

<?php
namespace Psr\Environment;
/**
* This abstraction represents the local system's internal hardware clock.
*/
interface SystemClock
{
/**
* Get the current time measured in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
*
* @return int time in seconds
*/
public function getTime(): int;
/**
* Get the current time measured in seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
* with as many fractions of precision as possible under the current
*
* @return float time in seconds (with fractions)
*/
public function getExactTime(): float;
}
/**
* This abstraction provides access to local environment variables (set by the operating system or `putenv`)
*/
interface SystemVars
{
/**
* Get the value of a specified local environment variable.
*
* @return string environment variable value
*
* @throws OutOfBoundsException if the specified variable has not been set
*/
public function getVar(string $name): string;
/**
* Check if a specified local environment variable is set.
*
* @return bool TRUE, if the specified variable is set
*/
public function hasVar(string $name): bool;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment