Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Storage | |
100.00% |
19 / 19 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| env | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
| register | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\System\File |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\System\File; |
| 16 | |
| 17 | use phpOMS\Autoloader; |
| 18 | |
| 19 | /** |
| 20 | * Filesystem class. |
| 21 | * |
| 22 | * Performing operations on the file system |
| 23 | * |
| 24 | * @package phpOMS\System\File |
| 25 | * @license OMS License 2.0 |
| 26 | * @link https://jingga.app |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | final class Storage |
| 30 | { |
| 31 | /** |
| 32 | * Registered storage. |
| 33 | * |
| 34 | * @var array<string, StorageAbstract|string|ContainerInterface> |
| 35 | * @since 1.0.0 |
| 36 | */ |
| 37 | private static array $registered = []; |
| 38 | |
| 39 | /** |
| 40 | * Constructor. |
| 41 | * |
| 42 | * @since 1.0.0 |
| 43 | * @codeCoverageIgnore |
| 44 | */ |
| 45 | private function __construct() |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get registred env instance. |
| 51 | * |
| 52 | * @param string $env Environment name |
| 53 | * |
| 54 | * @return StorageAbstract |
| 55 | * |
| 56 | * @throws \Exception Throws exception in case of invalid storage |
| 57 | * |
| 58 | * @since 1.0.0 |
| 59 | */ |
| 60 | public static function env(string $env = 'local') : StorageAbstract |
| 61 | { |
| 62 | if (isset(self::$registered[$env])) { |
| 63 | if (\is_string(self::$registered[$env])) { |
| 64 | /** @var StorageAbstract $instance */ |
| 65 | $instance = new self::$registered[$env](); |
| 66 | self::$registered[$env] = $instance; |
| 67 | } elseif (self::$registered[$env] instanceof StorageAbstract |
| 68 | || self::$registered[$env] instanceof ContainerInterface |
| 69 | ) { |
| 70 | /** @var StorageAbstract $instance */ |
| 71 | $instance = self::$registered[$env]; |
| 72 | } else { |
| 73 | throw new \Exception('Invalid type'); |
| 74 | } |
| 75 | } else { |
| 76 | $stg = \ucfirst(\strtolower($env)); |
| 77 | $stg = __NAMESPACE__ . '\\' . $stg . '\\' . $stg . 'Storage'; |
| 78 | |
| 79 | if (!Autoloader::exists($stg)) { |
| 80 | throw new \Exception('Invalid type'); |
| 81 | } |
| 82 | |
| 83 | /** @var StorageAbstract $stg */ |
| 84 | /** @var StorageAbstract $instance */ |
| 85 | $instance = new $stg(); |
| 86 | |
| 87 | self::$registered[$env] = $instance; |
| 88 | } |
| 89 | |
| 90 | return $instance; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Register storage environment. |
| 95 | * |
| 96 | * @param string $name Name of the environment |
| 97 | * @param ContainerInterface|StorageAbstract|string $class Class to register. This can be either a namespace path, a anonymous class or storage implementation. |
| 98 | * |
| 99 | * @return bool |
| 100 | * |
| 101 | * @since 1.0.0 |
| 102 | */ |
| 103 | public static function register(string $name, $class) : bool |
| 104 | { |
| 105 | if (isset(self::$registered[$name])) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | self::$registered[$name] = $class; |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | } |