Skip to content

Commit

Permalink
Add env helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Mar 8, 2024
1 parent 7c460f5 commit 8671d13
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Helpers/Env.php
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 );
}
}
50 changes: 50 additions & 0 deletions src/Helpers/Request.php
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;
}
}
33 changes: 33 additions & 0 deletions src/helpers.php
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();
}
20 changes: 20 additions & 0 deletions tests/Helpers/EnvTest.php
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')));
}
}
24 changes: 24 additions & 0 deletions tests/Helpers/RequestTest.php
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);
}
}

0 comments on commit 8671d13

Please sign in to comment.