Factory.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. 'default' => 'Predis\Profile\RedisVersion300',
  27. 'dev' => 'Predis\Profile\RedisUnstable',
  28. );
  29. /**
  30. *
  31. */
  32. private function __construct()
  33. {
  34. // NOOP
  35. }
  36. /**
  37. * Returns the default server profile.
  38. *
  39. * @return ProfileInterface
  40. */
  41. public static function getDefault()
  42. {
  43. return self::get('default');
  44. }
  45. /**
  46. * Returns the development server profile.
  47. *
  48. * @return ProfileInterface
  49. */
  50. public static function getDevelopment()
  51. {
  52. return self::get('dev');
  53. }
  54. /**
  55. * Registers a new server profile.
  56. *
  57. * @param string $alias Profile version or alias.
  58. * @param string $class FQN of a class implementing Predis\Profile\ProfileInterface.
  59. *
  60. * @throws \InvalidArgumentException
  61. */
  62. public static function define($alias, $class)
  63. {
  64. $reflection = new \ReflectionClass($class);
  65. if (!$reflection->isSubclassOf('Predis\Profile\ProfileInterface')) {
  66. throw new \InvalidArgumentException("The class '$class' is not a valid profile class.");
  67. }
  68. self::$profiles[$alias] = $class;
  69. }
  70. /**
  71. * Returns the specified server profile.
  72. *
  73. * @param string $version Profile version or alias.
  74. *
  75. * @throws ClientException
  76. * @return ProfileInterface
  77. *
  78. */
  79. public static function get($version)
  80. {
  81. if (!isset(self::$profiles[$version])) {
  82. throw new ClientException("Unknown server profile: '$version'.");
  83. }
  84. $profile = self::$profiles[$version];
  85. return new $profile();
  86. }
  87. }