|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of RedisClient. |
| 4 | + * git: https://github.com/cheprasov/php-redis-client |
| 5 | + * |
| 6 | + * (C) Alexander Cheprasov <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | +namespace RedisClient; |
| 12 | + |
| 13 | +use RedisClient\Client\AbstractRedisClient; |
| 14 | +use RedisClient\Client\Version\RedisClient2x6; |
| 15 | +use RedisClient\Client\Version\RedisClient2x8; |
| 16 | +use RedisClient\Client\Version\RedisClient3x0; |
| 17 | +use RedisClient\Client\Version\RedisClient3x2; |
| 18 | + |
| 19 | +class ClientFactory { |
| 20 | + |
| 21 | + protected static $versions = [ |
| 22 | + '2.6' => RedisClient2x6::class, |
| 23 | + '2.8' => RedisClient2x8::class, |
| 24 | + '3.0' => RedisClient3x0::class, |
| 25 | + '3.2' => RedisClient3x2::class, |
| 26 | + ]; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param null|array $config |
| 30 | + * @return RedisClient2x6|RedisClient2x8|RedisClient3x0|RedisClient3x2|RedisClient |
| 31 | + */ |
| 32 | + public static function create($config = null) { |
| 33 | + if (isset($config['version'])) { |
| 34 | + return self::createClientByVersion($config['version'], $config); |
| 35 | + } |
| 36 | + return new RedisClient($config); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param string $version |
| 41 | + * @param null|array $config |
| 42 | + * @return RedisClient2x6|RedisClient2x8|RedisClient3x0|RedisClient3x2 |
| 43 | + */ |
| 44 | + public static function createClientByVersion($version, $config = null) { |
| 45 | + list($major, $minor, $patch) = explode('.', $version.'.0.0'); |
| 46 | + $ver = (int) $major .'.'. (int) $minor; |
| 47 | + |
| 48 | + $versions = array_keys(self::$versions); |
| 49 | + foreach ($versions as $v) { |
| 50 | + if ($v >= $ver) { |
| 51 | + $class = self::$versions[$v]; |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + if (empty($class)) { |
| 56 | + throw new \InvalidArgumentException( |
| 57 | + 'RedisClient does not support Redis version '.$version.'. Please, use version ' .end($versions) |
| 58 | + ); |
| 59 | + } |
| 60 | + return new $class($config); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments