Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F11726353
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/OS/Environment.php b/src/OS/Environment.php
new file mode 100644
index 0000000..ce0fc86
--- /dev/null
+++ b/src/OS/Environment.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Keruald\OmniTools\OS;
+
+use InvalidArgumentException;
+
+class Environment {
+
+ public static function has (string $key) : bool {
+ return array_key_exists($key, $_ENV)
+ || array_key_exists($key, $_SERVER);
+ }
+
+ /**
+ * @throws InvalidArgumentException
+ */
+ public static function get (string $key) : string {
+ if (!self::has($key)) {
+ throw new InvalidArgumentException("Key not found: $key");
+ }
+
+ return $_ENV[$key] ?? $_SERVER[$key];
+ }
+
+ public static function getOr (string $key, string $default) : string {
+ return $_ENV[$key] ?? $_SERVER[$key] ?? $default;
+ }
+
+}
diff --git a/tests/OS/EnvironmentTest.php b/tests/OS/EnvironmentTest.php
new file mode 100644
index 0000000..9c3f5d4
--- /dev/null
+++ b/tests/OS/EnvironmentTest.php
@@ -0,0 +1,70 @@
+<?php
+
+namespace Keruald\OmniTools\Tests\OS;
+
+use InvalidArgumentException;
+use Keruald\OmniTools\OS\Environment;
+use PHPUnit\Framework\TestCase;
+
+class EnvironmentTest extends TestCase {
+
+ protected function setUp () : void {
+ // Keep in sync with provideEnvironment data provider
+
+ $_ENV['foo'] = "bar";
+ $_SERVER['foo'] = "baz";
+
+ $_ENV['bar'] = "lorem";
+ $_SERVER['baz'] = "ipsum";
+
+ // And quux isn't defined.
+ }
+
+ public function provideEnvironment () : iterable {
+ yield ["foo", "bar"];
+ yield ["bar", "lorem"];
+ yield ["baz", "ipsum"];
+ }
+
+ public function provideEnvironmentKeys () : iterable {
+ foreach ($this->provideEnvironment() as $kv) {
+ yield [$kv[0]];
+ }
+ }
+
+ /**
+ * @dataProvider provideEnvironmentKeys
+ */
+ public function testHas (string $key) : void {
+ self::assertTrue(Environment::has($key));
+ }
+
+ /**
+ * @dataProvider provideEnvironment
+ */
+ public function testGet (string $key, string $value) : void {
+ self::assertSame($value, Environment::get($key));
+ }
+
+ /**
+ * @dataProvider provideEnvironment
+ */
+ public function testGetOrWhenKeyExists (string $key, string $value) : void {
+ self::assertSame($value, Environment::getOr($key, "default"));
+ }
+
+ public function testHasWhenKeyDoesNotExist () : void {
+ self::assertFalse(Environment::has("quux"));
+ }
+
+
+ public function testGetWhenKeyDoesNotExist () : void {
+ $this->expectException(InvalidArgumentException::class);
+ Environment::get("quux");
+ }
+
+ public function testGetOrWhenKeyDoesNotExist () : void {
+ self::assertSame("default", Environment::getOr("quux", "default"));
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Sep 19, 03:03 (1 d, 5 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2992082
Default Alt Text
(2 KB)
Attached To
Mode
rKERUALD Keruald libraries development repository
Attached
Detach File
Event Timeline
Log In to Comment