* @since 1.0.0 */ private static array $registered = []; /** * Constructor. * * @since 1.0.0 * @codeCoverageIgnore */ private function __construct() { } /** * Get registred env instance. * * @param string $env Environment name * * @return StorageAbstract * * @throws \Exception Throws exception in case of invalid storage * * @since 1.0.0 */ public static function env(string $env = 'local') : StorageAbstract { if (isset(self::$registered[$env])) { if (\is_string(self::$registered[$env])) { /** @var StorageAbstract $instance */ $instance = new self::$registered[$env](); self::$registered[$env] = $instance; } elseif (self::$registered[$env] instanceof StorageAbstract || self::$registered[$env] instanceof ContainerInterface ) { /** @var StorageAbstract $instance */ $instance = self::$registered[$env]; } else { throw new \Exception('Invalid type'); } } else { $stg = \ucfirst(\strtolower($env)); $stg = __NAMESPACE__ . '\\' . $stg . '\\' . $stg . 'Storage'; if (!Autoloader::exists($stg)) { throw new \Exception('Invalid type'); } /** @var StorageAbstract $stg */ /** @var StorageAbstract $instance */ $instance = new $stg(); self::$registered[$env] = $instance; } return $instance; } /** * Register storage environment. * * @param string $name Name of the environment * @param ContainerInterface|StorageAbstract|string $class Class to register. This can be either a namespace path, a anonymous class or storage implementation. * * @return bool * * @since 1.0.0 */ public static function register(string $name, $class) : bool { if (isset(self::$registered[$name])) { return false; } self::$registered[$name] = $class; return true; } }