From 8bda821749071f569d41ddc5052f63a2036156fb Mon Sep 17 00:00:00 2001 From: James Jervis Date: Thu, 29 Sep 2016 20:02:35 -0500 Subject: [PATCH] Update to use instance --- config/config.php | 11 ++ src/Entity/Environment.php | 155 ++++++++++++++++++++++++++++ src/Environment.php | 180 ++++++++++++++++++++++----------- src/Factory/Environment.php | 35 +++++++ src/Middleware/Environment.php | 76 ++++++++++++++ 5 files changed, 399 insertions(+), 58 deletions(-) create mode 100644 config/config.php create mode 100644 src/Entity/Environment.php create mode 100644 src/Factory/Environment.php create mode 100644 src/Middleware/Environment.php diff --git a/config/config.php b/config/config.php new file mode 100644 index 0000000..9f8b3e9 --- /dev/null +++ b/config/config.php @@ -0,0 +1,11 @@ + [ + 'factories' => [ + \Reliv\Server\Entity\Environment::class => \Reliv\Server\Factory\Environment::class + ], + ], +]; diff --git a/src/Entity/Environment.php b/src/Entity/Environment.php new file mode 100644 index 0000000..d46e31c --- /dev/null +++ b/src/Entity/Environment.php @@ -0,0 +1,155 @@ + + * @copyright 2016 Reliv International + * @license License.txt + * @link https://github.com/reliv + */ +class Environment +{ + /** + * @var string + */ + protected $configPath; + + /** + * @var string + */ + protected $name; + + /** + * @var array + */ + protected $options + = [ + 'isProduction' => true, + 'initSet' => [ + ], + ]; + + /** + * Environment constructor. + * + * @param string $envName + * @param array $options + * @param string $configPath + */ + public function __construct( + $envName, + array $options = [], + $configPath = null + ) { + $this->setName($envName); + $this->setOptions($options); + $this->configPath = $configPath; + } + + /** + * setOptions + * + * @param $options + * + * @return void + */ + protected function setOptions($options) + { + if (empty($options)) { + return; + } + + array_merge( + $this->options, + $options + ); + } + + /** + * getOptions + * + * @return array + */ + public function getOptions() + { + return $this->options; + } + + /** + * setName + * + * @param string $envName + * + * @return void + */ + protected function setName($envName) + { + $this->name = (string)$envName; + } + + /** + * getName + * + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * getConfigPath + * + * @return string + */ + public function getConfigPath() + { + return $this->configPath; + } + + /** + * get + * + * @param string $key + * @param null $default + * + * @return mixed|null + */ + public function get($key, $default = null) + { + if (array_key_exists($key, $this->options)) { + return $this->options[$key]; + } + + return $default; + } + + /** + * isEnvironment + * + * @param $name + * + * @return bool + */ + public function isEnvironment($name) + { + $serverName = $this->getName(); + + return ($name === $serverName); + } + + /** + * isProduction + * + * @return bool + */ + public function isProduction() + { + $isProduction = (bool)$this->get('isProduction', true); + + return $isProduction; + } +} diff --git a/src/Environment.php b/src/Environment.php index 532cfa8..a7add72 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -12,105 +12,158 @@ */ class Environment { + /** + * @var string + */ + protected static $configKey = 'RelivServerEnvironment'; + /** * @var string */ protected static $localConfigPath = ''; /** - * @var bool + * @var null|\Reliv\Server\Entity\Environment */ - protected static $environmentSet = false; + protected static $instance = null; /** - * @var array + * getInstance + * + * @return null|\Reliv\Server\Entity\Environment */ - protected static $environment - = [ - 'name' => 'production', - 'isProduction' => true, - 'initSet' => [ - ], - ]; + public static function getInstance() + { + return self::$instance; + } /** - * setLocalConfigPath + * buildInstance + * + * @param $localConfigPath + * @param null $envName + * @param null $configKey * - * @param $localConfigPath - * @param null $env + * @return null|Entity\Environment * @throws \Exception */ - public static function setLocalEnvironment($localConfigPath, $env = null) - { - if ($env === null) { - $env = getenv('ENV'); + public static function buildInstance( + $localConfigPath, + $envName = null, + $configKey = null + ) { + if (self::$instance) { + return self::$instance; } - if (empty($env)) { - throw new \Exception('ENV is empty'); + if ($envName === null) { + $envName = getenv('ENV'); } - if (self::$environmentSet) { - return; + if (empty($envName)) { + throw new \Exception('ENV is empty'); } - self::$localConfigPath = $localConfigPath . '/' . $env . '.php'; - self::setEnvironment(); - } - /** - * setEnvironment - * - * @return void - */ - public static function setEnvironment() - { - if (self::$environmentSet) { - return; + if (!empty($configKey)) { + self::$configKey = $configKey; } + $configKey = self::$configKey; + + $configPath = $localConfigPath . '/' . $envName . '.php'; + // Assume default if config file not found - if (!file_exists(self::$localConfigPath)) { - self::$environmentSet = true; + if (!file_exists($configPath)) { trigger_error( 'localConfigPath not set', E_USER_WARNING ); - return; + self::$instance = self::build( + $envName, + [], + $configPath + ); + + return self::$instance; } - $config = include(self::$localConfigPath); + $config = include($configPath); // Assume default if config not found - if (!$config['RelivServerEnvironment']) { - self::$environmentSet = true; + if (!$config[$configKey]) { trigger_error( - "Config value 'RelivServerEnvironment' not set", + "Config value {$configKey} not set", E_USER_WARNING ); - return; + self::$instance = self::build( + $envName, + [], + $configPath + ); + + return self::$instance; } - self::$environment = array_merge( - self::$environment, - $config['RelivServerEnvironment'] + self::$instance = self::build( + $envName, + $config[$configKey], + $configPath ); - foreach (self::$environment['initSet'] as $key => $value) { + return self::$instance; + } + + /** + * build + * + * @param string $envName + * @param array $config + * @param string $configPath + * + * @return Entity\Environment + */ + protected static function build( + $envName, + array $config, + $configPath = null + ) { + $instance = new \Reliv\Server\Entity\Environment( + $envName, + $config, + $configPath + ); + + $initSet = $instance->get('initSet', []); + + foreach ($initSet as $key => $value) { ini_set($key, $value); } - self::$environmentSet = true; + return $instance; } /** - * getEnvironment + * @deprecated Use self::buildInstance() + * setLocalEnvironment + * + * @param string $localConfigPath + * @param null $envName + * @param null $configKey * - * @return array + * @return \Reliv\Server\Entity\Environment + * @throws \Exception */ - public static function getEnvironment() - { - return self::$environment; + public static function setLocalEnvironment( + $localConfigPath, + $envName = null, + $configKey = null + ) { + return self::buildInstance( + $localConfigPath, + $envName, + $configKey + ); } /** @@ -122,33 +175,44 @@ public static function getEnvironment() */ public static function isEnvironment($name) { - return ($name == self::$environment['name']); + return self::$instance->isEnvironment($name); } /** * isProduction * - * @return mixed + * @return bool */ public static function isProduction() { - return (self::$environment['isProduction']); + return self::$instance->isProduction(); + } + + /** + * getName + * + * @return string + */ + public static function getName() + { + return self::$instance->getName(); } /** * get * - * @param string $key + * @param string $key * @param null|mixed $default * * @return mixed|null */ public static function get($key, $default = null) { - if (array_key_exists($key, self::$environment)) { - return self::$environment[$key]; + // @BC + if ($key === 'name') { + return self::getName(); } - return $default; + return self::$instance->get($key, $default); } } diff --git a/src/Factory/Environment.php b/src/Factory/Environment.php new file mode 100644 index 0000000..9e3944f --- /dev/null +++ b/src/Factory/Environment.php @@ -0,0 +1,35 @@ + + * @copyright 2016 Reliv International + * @license License.txt + * @link https://github.com/reliv + */ +class Environment +{ + /** + * __invoke + * + * @param ContainerInterface $container + * + * @return null|\Reliv\Server\Entity\Environment + * @throws \Exception + */ + public function __invoke(ContainerInterface $container) + { + $instance = \Reliv\Server\Environment::getInstance(); + + if(empty($instance)) { + throw new \Exception('Reliv\Server\Environment MUST be build before configuration. No instance returned'); + } + + return $instance; + } +} diff --git a/src/Middleware/Environment.php b/src/Middleware/Environment.php new file mode 100644 index 0000000..a474a8a --- /dev/null +++ b/src/Middleware/Environment.php @@ -0,0 +1,76 @@ + + * @copyright 2016 Reliv International + * @license License.txt + * @link https://github.com/reliv + */ +class Environment implements MiddlewareInterface +{ + /** + * @var ContainerInterface + */ + protected $container; + + /** + * @var string + */ + protected $localConfigPath; + + /** + * @var null|string + */ + protected $envName = null; + + /** + * @var null|string + */ + protected $configKey = null; + + /** + * Environment constructor. + * + * @param string $localConfigPath + * @param string|null $envName + * @param string|null $configKey + */ + public function __construct( + $localConfigPath, + $envName = null, + $configKey = null + ) { + $this->localConfigPath = $localConfigPath; + $this->envName = $envName; + $this->configKey = $configKey; + } + + /** + * __invoke + * + * @param Request $request + * @param Response $response + * @param callable|Response $out + * + * @return mixed + */ + public function __invoke(Request $request, Response $response, callable $out = null) + { + $instance = \Reliv\Server\Environment::buildInstance( + $this->localConfigPath, + $this->envName, + $this->configKey + ); + + return $out($request, $response); + } +}