| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation App Framework — standalone Env adapter. |
| 4 |
* |
| 5 |
* Answers from PHP itself: real `define()`d constants, a content |
| 6 |
* directory the host passes in, the PHP version as the platform. |
| 7 |
* |
| 8 |
* @package OpenStation |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace OpenStation\App\Standalone; |
| 12 |
|
| 13 |
use OpenStation\App\Contracts\Env as EnvContract; |
| 14 |
|
| 15 |
// Direct access, unless a standalone host is booting on bare PHP. |
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
defined( 'OPENSTATION_STANDALONE' ) || exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Plain-PHP environment facts. |
| 22 |
*/ |
| 23 |
final class Env implements EnvContract { |
| 24 |
|
| 25 |
/** |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $content_dir; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
private $environment_type; |
| 34 |
|
| 35 |
/** |
| 36 |
* @param string $content_dir Writable content directory. Defaults to the system temp dir. |
| 37 |
* @param string $environment_type `production` | `staging` | `development` | `local`. |
| 38 |
*/ |
| 39 |
public function __construct( $content_dir = '', $environment_type = 'production' ) { |
| 40 |
$this->content_dir = '' !== $content_dir ? rtrim( (string) $content_dir, '/\\' ) : sys_get_temp_dir(); |
| 41 |
$this->environment_type = (string) $environment_type; |
| 42 |
} |
| 43 |
|
| 44 |
/** {@inheritDoc} */ |
| 45 |
public function constant( $name, $fallback = null ) { |
| 46 |
return defined( $name ) ? constant( $name ) : $fallback; |
| 47 |
} |
| 48 |
|
| 49 |
/** {@inheritDoc} */ |
| 50 |
public function content_dir() { |
| 51 |
return $this->content_dir; |
| 52 |
} |
| 53 |
|
| 54 |
/** {@inheritDoc} */ |
| 55 |
public function platform() { |
| 56 |
return array( |
| 57 |
'name' => 'PHP', |
| 58 |
'version' => PHP_VERSION, |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** {@inheritDoc} */ |
| 63 |
public function environment_type() { |
| 64 |
return $this->environment_type; |
| 65 |
} |
| 66 |
|
| 67 |
/** {@inheritDoc} */ |
| 68 |
public function is_network() { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
/** {@inheritDoc} */ |
| 73 |
public function format_datetime( $timestamp, $format = 'Y-m-d H:i:s' ) { |
| 74 |
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date -- The standalone host has no site timezone; PHP's own is the contract here. |
| 75 |
return date( (string) $format, (int) $timestamp ); |
| 76 |
} |
| 77 |
} |
| 78 |
|