ClientOptions.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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;
  11. use Predis\Options\IOption;
  12. use Predis\Options\ClientPrefix;
  13. use Predis\Options\ClientProfile;
  14. use Predis\Options\ClientCluster;
  15. use Predis\Options\ClientConnectionFactory;
  16. /**
  17. * Class that manages validation and conversion of client options.
  18. *
  19. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. class ClientOptions
  22. {
  23. private static $sharedOptions;
  24. private $handlers;
  25. private $defined;
  26. private $options = array();
  27. /**
  28. * @param array $options Array of client options.
  29. */
  30. public function __construct(Array $options = array())
  31. {
  32. $this->handlers = $this->initialize($options);
  33. $this->defined = array_keys($options);
  34. }
  35. /**
  36. * Ensures that the default options are initialized.
  37. *
  38. * @return array
  39. */
  40. private static function getSharedOptions()
  41. {
  42. if (isset(self::$sharedOptions)) {
  43. return self::$sharedOptions;
  44. }
  45. self::$sharedOptions = array(
  46. 'profile' => new ClientProfile(),
  47. 'connections' => new ClientConnectionFactory(),
  48. 'cluster' => new ClientCluster(),
  49. 'prefix' => new ClientPrefix(),
  50. );
  51. return self::$sharedOptions;
  52. }
  53. /**
  54. * Defines an option handler or overrides an existing one.
  55. *
  56. * @param string $option Name of the option.
  57. * @param IOption $handler Handler for the option.
  58. */
  59. public static function define($option, IOption $handler)
  60. {
  61. self::getSharedOptions();
  62. self::$sharedOptions[$option] = $handler;
  63. }
  64. /**
  65. * Undefines the handler for the specified option.
  66. *
  67. * @param string $option Name of the option.
  68. */
  69. public static function undefine($option)
  70. {
  71. self::getSharedOptions();
  72. unset(self::$sharedOptions[$option]);
  73. }
  74. /**
  75. * Initializes client options handlers.
  76. *
  77. * @param array $options List of client options values.
  78. * @return array
  79. */
  80. private function initialize($options)
  81. {
  82. $handlers = self::getSharedOptions();
  83. foreach ($options as $option => $value) {
  84. if (isset($handlers[$option])) {
  85. $handler = $handlers[$option];
  86. $handlers[$option] = function() use($handler, $value) {
  87. return $handler->validate($value);
  88. };
  89. }
  90. }
  91. return $handlers;
  92. }
  93. /**
  94. * Checks if the specified option is set.
  95. *
  96. * @param string $option Name of the option.
  97. * @return Boolean
  98. */
  99. public function __isset($option)
  100. {
  101. return in_array($option, $this->defined);
  102. }
  103. /**
  104. * Returns the value of the specified option.
  105. *
  106. * @param string $option Name of the option.
  107. * @return mixed
  108. */
  109. public function __get($option)
  110. {
  111. if (isset($this->options[$option])) {
  112. return $this->options[$option];
  113. }
  114. if (isset($this->handlers[$option])) {
  115. $handler = $this->handlers[$option];
  116. $value = $handler instanceof IOption ? $handler->getDefault() : $handler();
  117. $this->options[$option] = $value;
  118. return $value;
  119. }
  120. }
  121. }