Options.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\Configuration;
  11. /**
  12. * Manages Predis options with filtering, conversion and lazy initialization of
  13. * values using a mini-DI container approach.
  14. *
  15. * @property-read mixed aggregate Custom connection aggregator.
  16. * @property-read mixed cluster Aggregate connection for clustering.
  17. * @property-read mixed connections Connection factory.
  18. * @property-read mixed exceptions Toggles exceptions in client for -ERR responses.
  19. * @property-read mixed prefix Key prefixing strategy using the given prefix.
  20. * @property-read mixed profile Server profile.
  21. * @property-read mixed replication Aggregate connection for replication.
  22. *
  23. * @author Daniele Alessandri <suppakilla@gmail.com>
  24. */
  25. class Options implements OptionsInterface
  26. {
  27. protected $input;
  28. protected $options;
  29. protected $handlers;
  30. /**
  31. * @param array $options Array of options with their values
  32. */
  33. public function __construct(array $options = array())
  34. {
  35. $this->input = $options;
  36. $this->options = array();
  37. $this->handlers = $this->getHandlers();
  38. }
  39. /**
  40. * Ensures that the default options are initialized.
  41. *
  42. * @return array
  43. */
  44. protected function getHandlers()
  45. {
  46. return array(
  47. 'cluster' => 'Predis\Configuration\ClusterOption',
  48. 'connections' => 'Predis\Configuration\ConnectionFactoryOption',
  49. 'exceptions' => 'Predis\Configuration\ExceptionsOption',
  50. 'prefix' => 'Predis\Configuration\PrefixOption',
  51. 'profile' => 'Predis\Configuration\ProfileOption',
  52. 'replication' => 'Predis\Configuration\ReplicationOption',
  53. );
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getDefault($option)
  59. {
  60. if (isset($this->handlers[$option])) {
  61. $handler = $this->handlers[$option];
  62. $handler = new $handler();
  63. return $handler->getDefault($this);
  64. }
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function defined($option)
  70. {
  71. return (
  72. array_key_exists($option, $this->options) ||
  73. array_key_exists($option, $this->input)
  74. );
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function __isset($option)
  80. {
  81. return (
  82. array_key_exists($option, $this->options) ||
  83. array_key_exists($option, $this->input)
  84. ) && $this->__get($option) !== null;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function __get($option)
  90. {
  91. if (isset($this->options[$option]) || array_key_exists($option, $this->options)) {
  92. return $this->options[$option];
  93. }
  94. if (isset($this->input[$option]) || array_key_exists($option, $this->input)) {
  95. $value = $this->input[$option];
  96. unset($this->input[$option]);
  97. if (method_exists($value, '__invoke')) {
  98. $value = $value($this, $option);
  99. }
  100. if (isset($this->handlers[$option])) {
  101. $handler = $this->handlers[$option];
  102. $handler = new $handler();
  103. $value = $handler->filter($this, $value);
  104. }
  105. return $this->options[$option] = $value;
  106. }
  107. if (isset($this->handlers[$option])) {
  108. return $this->options[$option] = $this->getDefault($option);
  109. }
  110. }
  111. }