Factory.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Profile;
  11. use Predis\ClientException;
  12. /**
  13. * Factory class for creating profile instances from strings.
  14. *
  15. * @author Daniele Alessandri <suppakilla@gmail.com>
  16. */
  17. final class Factory
  18. {
  19. private static $profiles = array(
  20. '2.0' => 'Predis\Profile\RedisVersion200',
  21. '2.2' => 'Predis\Profile\RedisVersion220',
  22. '2.4' => 'Predis\Profile\RedisVersion240',
  23. '2.6' => 'Predis\Profile\RedisVersion260',
  24. '2.8' => 'Predis\Profile\RedisVersion280',
  25. '3.0' => 'Predis\Profile\RedisVersion300',
  26. '3.2' => 'Predis\Profile\RedisVersion320',
  27. 'dev' => 'Predis\Profile\RedisUnstable',
  28. 'default' => 'Predis\Profile\RedisVersion300',
  29. );
  30. /**
  31. *
  32. */
  33. private function __construct()
  34. {
  35. // NOOP
  36. }
  37. /**
  38. * Returns the default server profile.
  39. *
  40. * @return ProfileInterface
  41. */
  42. public static function getDefault()
  43. {
  44. return self::get('default');
  45. }
  46. /**
  47. * Returns the development server profile.
  48. *
  49. * @return ProfileInterface
  50. */
  51. public static function getDevelopment()
  52. {
  53. return self::get('dev');
  54. }
  55. /**
  56. * Registers a new server profile.
  57. *
  58. * @param string $alias Profile version or alias.
  59. * @param string $class FQN of a class implementing Predis\Profile\ProfileInterface.
  60. *
  61. * @throws \InvalidArgumentException
  62. */
  63. public static function define($alias, $class)
  64. {
  65. $reflection = new \ReflectionClass($class);
  66. if (!$reflection->isSubclassOf('Predis\Profile\ProfileInterface')) {
  67. throw new \InvalidArgumentException("The class '$class' is not a valid profile class.");
  68. }
  69. self::$profiles[$alias] = $class;
  70. }
  71. /**
  72. * Returns the specified server profile.
  73. *
  74. * @param string $version Profile version or alias.
  75. *
  76. * @throws ClientException
  77. *
  78. * @return ProfileInterface
  79. */
  80. public static function get($version)
  81. {
  82. if (!isset(self::$profiles[$version])) {
  83. throw new ClientException("Unknown server profile: '$version'.");
  84. }
  85. $profile = self::$profiles[$version];
  86. return new $profile();
  87. }
  88. }