🎉 initial commit
This commit is contained in:
77
classes/Application/AppEnvironment.php
Normal file
77
classes/Application/AppEnvironment.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace Application;
|
||||
|
||||
use Application\Helper\ArrayPath;
|
||||
use Dotenv\Dotenv;
|
||||
use Exception;
|
||||
|
||||
class AppEnvironment
|
||||
{
|
||||
private static ?AppEnvironment $instance = null;
|
||||
|
||||
private array $environment = [];
|
||||
|
||||
/**
|
||||
* The Singleton's constructor should always be private to prevent direct
|
||||
* construction calls with the `new` operator.
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
$this->loadEnvironment();
|
||||
}
|
||||
|
||||
/**
|
||||
* Singletons should not be cloneable.
|
||||
*/
|
||||
protected function __clone() {}
|
||||
|
||||
/**
|
||||
* Singletons should not be restorable from strings.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __wakeup()
|
||||
{
|
||||
throw new Exception("Cannot unserialize a singleton.");
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the static method that controls the access to the singleton
|
||||
* instance. On the first run, it creates a singleton object and places it
|
||||
* into the static field. On subsequent runs, it returns the client existing
|
||||
* object stored in the static field.
|
||||
*
|
||||
* This implementation lets you subclass the Singleton class while keeping
|
||||
* just one instance of each subclass around.
|
||||
*/
|
||||
public static function getInstance(): AppEnvironment
|
||||
{
|
||||
if (!isset(self::$instance)) {
|
||||
self::$instance = new static();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finally, any singleton should define some business logic, which can be
|
||||
* executed on its instance.
|
||||
*/
|
||||
private function loadEnvironment(): void
|
||||
{
|
||||
if (!empty($this->environment)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->environment = Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT'] . '/../')->load();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getVariable(string|array $key)
|
||||
{
|
||||
return ArrayPath::GetDataFromArray($this->environment, $key);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user