Factory.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 $class FQN of a class implementing Predis\Profile\ProfileInterface.
  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. * @return ProfileInterface
  75. */
  76. public static function get($version)
  77. {
  78. if (!isset(self::$profiles[$version])) {
  79. throw new ClientException("Unknown server profile: '$version'.");
  80. }
  81. $profile = self::$profiles[$version];
  82. return new $profile();
  83. }
  84. }