Factory.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 InvalidArgumentException;
  12. use ReflectionClass;
  13. use Predis\ClientException;
  14. /**
  15. * Factory class for creating profile instances from strings.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. final class Factory
  20. {
  21. private static $profiles = array(
  22. '1.2' => 'Predis\Profile\RedisVersion120',
  23. '2.0' => 'Predis\Profile\RedisVersion200',
  24. '2.2' => 'Predis\Profile\RedisVersion220',
  25. '2.4' => 'Predis\Profile\RedisVersion240',
  26. '2.6' => 'Predis\Profile\RedisVersion260',
  27. '2.8' => 'Predis\Profile\RedisVersion280',
  28. 'default' => 'Predis\Profile\RedisVersion280',
  29. 'dev' => 'Predis\Profile\RedisUnstable',
  30. );
  31. /**
  32. *
  33. */
  34. private function __construct()
  35. {
  36. // NOOP
  37. }
  38. /**
  39. * Returns the default server profile.
  40. *
  41. * @return ProfileInterface
  42. */
  43. public static function getDefault()
  44. {
  45. return self::get('default');
  46. }
  47. /**
  48. * Returns the development server profile.
  49. *
  50. * @return ProfileInterface
  51. */
  52. public static function getDevelopment()
  53. {
  54. return self::get('dev');
  55. }
  56. /**
  57. * Registers a new server profile.
  58. *
  59. * @param string $alias Profile version or alias.
  60. * @param string $profileClass FQN of a class implementing Predis\Profile\ProfileInterface.
  61. */
  62. public static function define($alias, $profileClass)
  63. {
  64. $reflection = new ReflectionClass($profileClass);
  65. if (!$reflection->isSubclassOf('Predis\Profile\ProfileInterface')) {
  66. throw new InvalidArgumentException(
  67. "Cannot register '$profileClass' as it is not a valid profile class"
  68. );
  69. }
  70. self::$profiles[$alias] = $profileClass;
  71. }
  72. /**
  73. * Returns the specified server profile.
  74. *
  75. * @param string $version Profile version or alias.
  76. * @return ProfileInterface
  77. */
  78. public static function get($version)
  79. {
  80. if (!isset(self::$profiles[$version])) {
  81. throw new ClientException("Unknown server profile: $version");
  82. }
  83. $profile = self::$profiles[$version];
  84. return new $profile();
  85. }
  86. }