Factory.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. '2.0' => 'Predis\Profile\RedisVersion200',
  23. '2.2' => 'Predis\Profile\RedisVersion220',
  24. '2.4' => 'Predis\Profile\RedisVersion240',
  25. '2.6' => 'Predis\Profile\RedisVersion260',
  26. '2.8' => 'Predis\Profile\RedisVersion280',
  27. 'default' => 'Predis\Profile\RedisVersion280',
  28. 'dev' => 'Predis\Profile\RedisUnstable',
  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. public static function define($alias, $class)
  62. {
  63. $reflection = new ReflectionClass($class);
  64. if (!$reflection->isSubclassOf('Predis\Profile\ProfileInterface')) {
  65. throw new InvalidArgumentException("The class '$class' is not a valid profile class.");
  66. }
  67. self::$profiles[$alias] = $class;
  68. }
  69. /**
  70. * Returns the specified server profile.
  71. *
  72. * @param string $version Profile version or alias.
  73. * @return ProfileInterface
  74. */
  75. public static function get($version)
  76. {
  77. if (!isset(self::$profiles[$version])) {
  78. throw new ClientException("Unknown server profile: '$version'.");
  79. }
  80. $profile = self::$profiles[$version];
  81. return new $profile();
  82. }
  83. }