-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c460f5
commit 8671d13
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Studiometa\WPToolkit\Helpers; | ||
|
||
class Env { | ||
/** | ||
* Get an environment variable value. | ||
* | ||
* @param string $key The variable name. | ||
* @return string | ||
*/ | ||
public static function get( string $key ): string { | ||
// phpcs:ignore | ||
/** @var array<string, string> 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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Studiometa\WPToolkit\Helpers; | ||
|
||
use Symfony\Component\HttpFoundation\Request as SymfonyRequest; | ||
|
||
class Request { | ||
/** | ||
* Private variable to hold the helpers instance. | ||
* | ||
* @var ?Request | ||
*/ | ||
private static $instance = null; | ||
|
||
/** | ||
* Private variables to hold the current request instance. | ||
* | ||
* @var SymfonyRequest | ||
*/ | ||
private SymfonyRequest $request; | ||
|
||
/** | ||
* Private constructor. | ||
*/ | ||
private function __construct() { | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Helpers | ||
* | ||
* @package Studiometa | ||
*/ | ||
|
||
namespace Studiometa\WPToolkit; | ||
|
||
use Studiometa\WPToolkit\Helpers\Env; | ||
use Studiometa\WPToolkit\Helpers\Request; | ||
use Symfony\Component\HttpFoundation\Request as SymfonyRequest; | ||
|
||
/** | ||
* Get an environment variable value. | ||
* | ||
* @param string $key The variable name. | ||
* @return string | ||
*/ | ||
function env( string $key ): string { | ||
return Env::get( $key ); | ||
} | ||
|
||
/** | ||
* Get a Request instance from the symfony/http-foundation package. | ||
* | ||
* @see https://symfony.com/doc/current/components/http_foundation.html#request | ||
* | ||
* @return SymfonyRequest | ||
*/ | ||
function request(): SymfonyRequest { | ||
return Request::request(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
use Studiometa\WPToolkit\Helpers\Env as EnvClass; | ||
use function Studiometa\WPToolkit\env; | ||
|
||
/** | ||
* EnvTest test case. | ||
*/ | ||
class EnvTest extends WP_UnitTestCase { | ||
|
||
/** | ||
* Test the request() function | ||
* | ||
* @return void | ||
*/ | ||
public function test_type_of_request_function_helper() { | ||
$this->assertTrue(is_string(env('missing'))); | ||
$this->assertTrue(is_string(EnvClass::get('missing'))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
use Studiometa\WPToolkit\Helpers\Request as RequestClass; | ||
use function Studiometa\WPToolkit\request; | ||
use Symfony\Component\HttpFoundation\Request as SymfonyRequest; | ||
|
||
/** | ||
* RequestTest test case. | ||
*/ | ||
class RequestTest extends WP_UnitTestCase { | ||
|
||
/** | ||
* Test the request() function | ||
* | ||
* @return void | ||
*/ | ||
public function test_type_of_request_function_helper() { | ||
$request1 = RequestClass::request(); | ||
$request2 = request(); | ||
$this->assertTrue($request1 instanceof SymfonyRequest); | ||
$this->assertTrue($request2 instanceof SymfonyRequest); | ||
$this->assertTrue($request1 === $request2); | ||
} | ||
} |