ProfileOption.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. use InvalidArgumentException;
  12. use Predis\Profile\Factory;
  13. use Predis\Profile\ProfileInterface;
  14. use Predis\Profile\RedisProfile;
  15. /**
  16. * Configures the server profile to be used by the client to create command
  17. * instances depending on the specified version of the Redis server.
  18. *
  19. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. class ProfileOption implements OptionInterface
  22. {
  23. /**
  24. * Sets the commands processors that need to be applied to the profile.
  25. *
  26. * @param OptionsInterface $options Client options.
  27. * @param ProfileInterface $profile Server profile.
  28. */
  29. protected function setProcessors(OptionsInterface $options, ProfileInterface $profile)
  30. {
  31. if (isset($options->prefix) && $profile instanceof RedisProfile) {
  32. // NOTE: directly using __get('prefix') is actually a workaround for
  33. // HHVM 2.3.0. It's correct and respects the options interface, it's
  34. // just ugly. We will remove this hack when HHVM will fix re-entrant
  35. // calls to __get() once and for all.
  36. $profile->setProcessor($options->__get('prefix'));
  37. }
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function filter(OptionsInterface $options, $value)
  43. {
  44. if (is_string($value)) {
  45. $value = Factory::get($value);
  46. $this->setProcessors($options, $value);
  47. } elseif (!$value instanceof ProfileInterface) {
  48. throw new InvalidArgumentException('Invalid value for the profile option.');
  49. }
  50. return $value;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getDefault(OptionsInterface $options)
  56. {
  57. $profile = Factory::getDefault();
  58. $this->setProcessors($options, $profile);
  59. return $profile;
  60. }
  61. }