| @@ 20-72 (lines=53) @@ | ||
| 17 | /** |
|
| 18 | * Storage Factory. |
|
| 19 | */ |
|
| 20 | class CacheFactory |
|
| 21 | { |
|
| 22 | /** |
|
| 23 | * @var string One of supported drivers |
|
| 24 | */ |
|
| 25 | private $driver; |
|
| 26 | ||
| 27 | /** |
|
| 28 | * @var array Factory supported driver |
|
| 29 | */ |
|
| 30 | private $supportedDriver = [ |
|
| 31 | 'disk' => DiskCache::class, |
|
| 32 | 'memcached' => MemcachedCache::class, |
|
| 33 | ]; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @var array Options for the driver |
|
| 37 | */ |
|
| 38 | private $options; |
|
| 39 | ||
| 40 | /** |
|
| 41 | * Constructor. |
|
| 42 | * |
|
| 43 | * @param string $driver |
|
| 44 | * @param array $options |
|
| 45 | */ |
|
| 46 | public function __construct(string $driver, array $options) |
|
| 47 | { |
|
| 48 | $this->driver = $driver; |
|
| 49 | $this->options = $options; |
|
| 50 | } |
|
| 51 | ||
| 52 | /** |
|
| 53 | * Return Cache Resource. |
|
| 54 | * |
|
| 55 | * @throws InvalidArgumentException If required driver is not supported |
|
| 56 | * |
|
| 57 | * @return CacheInterface |
|
| 58 | */ |
|
| 59 | public function get() : CacheInterface |
|
| 60 | { |
|
| 61 | $driver = $this->driver; |
|
| 62 | $options = $this->options; |
|
| 63 | ||
| 64 | if (isset($this->supportedDriver[$driver])) { |
|
| 65 | $cache = $this->supportedDriver[$driver]; |
|
| 66 | ||
| 67 | return new $cache($options); |
|
| 68 | } |
|
| 69 | ||
| 70 | throw new InvalidArgumentException("[$driver] not supported."); |
|
| 71 | } |
|
| 72 | } |
|
| 73 | ||
| @@ 19-72 (lines=54) @@ | ||
| 16 | /** |
|
| 17 | * Storage Factory. |
|
| 18 | */ |
|
| 19 | class StorageFactory |
|
| 20 | { |
|
| 21 | /** |
|
| 22 | * @var string One of supported drivers |
|
| 23 | */ |
|
| 24 | private $driver; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * @var array Factory supported driver |
|
| 28 | */ |
|
| 29 | private $supportedDriver = [ |
|
| 30 | 'pdo' => PdoStorage::class, |
|
| 31 | 'mysqli' => MysqliStorage::class, |
|
| 32 | 'mongodb' => MongoDbStorage::class, |
|
| 33 | ]; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @var array Options for the driver |
|
| 37 | */ |
|
| 38 | private $options; |
|
| 39 | ||
| 40 | /** |
|
| 41 | * Constructor. |
|
| 42 | * |
|
| 43 | * @param string $driver |
|
| 44 | * @param array $options |
|
| 45 | */ |
|
| 46 | public function __construct(string $driver, array $options) |
|
| 47 | { |
|
| 48 | $this->driver = $driver; |
|
| 49 | $this->options = $options; |
|
| 50 | } |
|
| 51 | ||
| 52 | /** |
|
| 53 | * Create Database Connection. |
|
| 54 | * |
|
| 55 | * @throws InvalidArgumentException If required driver is not supported |
|
| 56 | * |
|
| 57 | * @return StorageInterface |
|
| 58 | */ |
|
| 59 | public function getConnection() : StorageInterface |
|
| 60 | { |
|
| 61 | $driver = $this->driver; |
|
| 62 | $options = $this->options; |
|
| 63 | ||
| 64 | if (isset($this->supportedDriver[$driver])) { |
|
| 65 | $storageClass = $this->supportedDriver[$driver]; |
|
| 66 | ||
| 67 | return new $storageClass($options); |
|
| 68 | } |
|
| 69 | ||
| 70 | throw new InvalidArgumentException("[$driver] not supported."); |
|
| 71 | } |
|
| 72 | } |
|
| 73 | ||