ClientOptions.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Predis;
  3. class ClientOptions {
  4. private $_handlers, $_options;
  5. private static $_sharedOptions;
  6. public function __construct(Array $options = array()) {
  7. $this->initialize($options);
  8. }
  9. private static function getSharedOptions() {
  10. if (isset(self::$_sharedOptions)) {
  11. return self::$_sharedOptions;
  12. }
  13. self::$_sharedOptions = array(
  14. 'profile' => new Options\ClientProfile(),
  15. 'key_distribution' => new Options\ClientKeyDistribution(),
  16. 'on_connection_initialized' => new Options\CustomOption(array(
  17. 'validate' => function($value) {
  18. if (is_callable($value)) {
  19. return $value;
  20. }
  21. },
  22. )),
  23. );
  24. return self::$_sharedOptions;
  25. }
  26. public static function define($option, Options\IOption $handler) {
  27. self::getSharedOptions();
  28. self::$_sharedOptions[$option] = $handler;
  29. }
  30. public static function undefine($option) {
  31. self::getSharedOptions();
  32. unset(self::$_sharedOptions[$option]);
  33. }
  34. private function initialize($options) {
  35. $this->_handlers = $this->getOptions();
  36. foreach ($options as $option => $value) {
  37. if (isset($this->_handlers[$option])) {
  38. $handler = $this->_handlers[$option];
  39. $this->_options[$option] = $handler($value);
  40. }
  41. }
  42. }
  43. private function getOptions() {
  44. return self::getSharedOptions();
  45. }
  46. public function __get($option) {
  47. if (!isset($this->_options[$option])) {
  48. if (!isset($this->_handlers[$option])) {
  49. return null;
  50. }
  51. $handler = $this->_handlers[$option];
  52. $this->_options[$option] = $handler->getDefault();
  53. }
  54. return $this->_options[$option];
  55. }
  56. public function __isset($option) {
  57. return isset(self::$_sharedOptions[$option]);
  58. }
  59. }