From 8671d1347d31fdb013323e835da60429e551de71 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Fri, 8 Mar 2024 16:01:05 +0100 Subject: [PATCH] Add env helpers --- src/Helpers/Env.php | 20 ++++++++++++++ src/Helpers/Request.php | 50 +++++++++++++++++++++++++++++++++++ src/helpers.php | 33 +++++++++++++++++++++++ tests/Helpers/EnvTest.php | 20 ++++++++++++++ tests/Helpers/RequestTest.php | 24 +++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100644 src/Helpers/Env.php create mode 100644 src/Helpers/Request.php create mode 100644 src/helpers.php create mode 100644 tests/Helpers/EnvTest.php create mode 100644 tests/Helpers/RequestTest.php diff --git a/src/Helpers/Env.php b/src/Helpers/Env.php new file mode 100644 index 0000000..78876b6 --- /dev/null +++ b/src/Helpers/Env.php @@ -0,0 +1,20 @@ + Good type. */ + $env = $_ENV; + // In some environment, values are not available in the `$_ENV` variables, + // so we use `getenv` as a fallback to try and get the value. + return $env[ $key ] ?? (string) getenv( $key ); + } +} diff --git a/src/Helpers/Request.php b/src/Helpers/Request.php new file mode 100644 index 0000000..3dc3eaa --- /dev/null +++ b/src/Helpers/Request.php @@ -0,0 +1,50 @@ +request = SymfonyRequest::createFromGlobals(); + } + + /** + * Get singleton instance. + * + * @return Request + */ + private static function get_instance() { + if ( is_null( self::$instance ) ) { + self::$instance = new self(); + } + + return self::$instance; + } + + /** + * Work with the current HTTP request. + * + * @return SymfonyRequest + */ + public static function request(): SymfonyRequest { + return self::get_instance()->request; + } +} diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..dc3fc66 --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,33 @@ +assertTrue(is_string(env('missing'))); + $this->assertTrue(is_string(EnvClass::get('missing'))); + } +} diff --git a/tests/Helpers/RequestTest.php b/tests/Helpers/RequestTest.php new file mode 100644 index 0000000..0b39486 --- /dev/null +++ b/tests/Helpers/RequestTest.php @@ -0,0 +1,24 @@ +assertTrue($request1 instanceof SymfonyRequest); + $this->assertTrue($request2 instanceof SymfonyRequest); + $this->assertTrue($request1 === $request2); + } +}