ProfileOption.php 2.0 KB

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