Options.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * {@inheritdoc}
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class Options implements OptionsInterface
  20. {
  21. protected $input;
  22. protected $options;
  23. protected $handlers;
  24. /**
  25. * @param array $options Array of options with their values
  26. */
  27. public function __construct(array $options = array())
  28. {
  29. $this->input = $options;
  30. $this->options = array();
  31. $this->handlers = $this->getHandlers();
  32. }
  33. /**
  34. * Ensures that the default options are initialized.
  35. *
  36. * @return array
  37. */
  38. protected function getHandlers()
  39. {
  40. return array(
  41. 'cluster' => 'Predis\Configuration\ClusterOption',
  42. 'connections' => 'Predis\Configuration\ConnectionFactoryOption',
  43. 'exceptions' => 'Predis\Configuration\ExceptionsOption',
  44. 'prefix' => 'Predis\Configuration\PrefixOption',
  45. 'profile' => 'Predis\Configuration\ProfileOption',
  46. 'replication' => 'Predis\Configuration\ReplicationOption',
  47. );
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getDefault($option)
  53. {
  54. if (isset($this->handlers[$option])) {
  55. $handler = $this->handlers[$option];
  56. $handler = new $handler();
  57. return $handler->getDefault($this);
  58. }
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function defined($option)
  64. {
  65. return (
  66. array_key_exists($option, $this->options) ||
  67. array_key_exists($option, $this->input)
  68. );
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function __isset($option)
  74. {
  75. return (
  76. array_key_exists($option, $this->options) ||
  77. array_key_exists($option, $this->input)
  78. ) && $this->__get($option) !== null;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function __get($option)
  84. {
  85. if (isset($this->options[$option]) || array_key_exists($option, $this->options)) {
  86. return $this->options[$option];
  87. }
  88. if (isset($this->input[$option]) || array_key_exists($option, $this->input)) {
  89. $value = $this->input[$option];
  90. unset($this->input[$option]);
  91. if (is_object($value) && method_exists($value, '__invoke')) {
  92. $value = $value($this, $option);
  93. }
  94. if (isset($this->handlers[$option])) {
  95. $handler = $this->handlers[$option];
  96. $handler = new $handler();
  97. $value = $handler->filter($this, $value);
  98. }
  99. return $this->options[$option] = $value;
  100. }
  101. if (isset($this->handlers[$option])) {
  102. return $this->options[$option] = $this->getDefault($option);
  103. }
  104. return null;
  105. }
  106. }