Options.php 3.1 KB

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