Helpers.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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;
  11. use Predis\Network\IConnection;
  12. use Predis\Network\IConnectionCluster;
  13. use Predis\Network\IConnectionReplication;
  14. /**
  15. * Defines a few helper methods.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class Helpers
  20. {
  21. /**
  22. * Checks if the specified connection represents an aggregation of connections.
  23. *
  24. * @param IConnection $connection Connection object.
  25. * @return Boolean
  26. */
  27. public static function isAggregated(IConnection $connection)
  28. {
  29. return $connection instanceof IConnectionCluster || $connection instanceof IConnectionReplication;
  30. }
  31. /**
  32. * Checks if the specified connection represents a cluster.
  33. *
  34. * @param IConnection $connection Connection object.
  35. * @return Boolean
  36. */
  37. public static function isCluster(IConnection $connection)
  38. {
  39. return $connection instanceof IConnectionCluster;
  40. }
  41. /**
  42. * Offers a generic and reusable method to handle exceptions generated by
  43. * a connection object.
  44. *
  45. * @param CommunicationException $exception Exception.
  46. */
  47. public static function onCommunicationException(CommunicationException $exception)
  48. {
  49. if ($exception->shouldResetConnection()) {
  50. $connection = $exception->getConnection();
  51. if ($connection->isConnected()) {
  52. $connection->disconnect();
  53. }
  54. }
  55. throw $exception;
  56. }
  57. /**
  58. * Normalizes the arguments array passed to a Redis command.
  59. *
  60. * @param array $arguments Arguments for a command.
  61. * @return array
  62. */
  63. public static function filterArrayArguments(Array $arguments)
  64. {
  65. if (count($arguments) === 1 && is_array($arguments[0])) {
  66. return $arguments[0];
  67. }
  68. return $arguments;
  69. }
  70. /**
  71. * Normalizes the arguments array passed to a variadic Redis command.
  72. *
  73. * @param array $arguments Arguments for a command.
  74. * @return array
  75. */
  76. public static function filterVariadicValues(Array $arguments)
  77. {
  78. if (count($arguments) === 2 && is_array($arguments[1])) {
  79. return array_merge(array($arguments[0]), $arguments[1]);
  80. }
  81. return $arguments;
  82. }
  83. /**
  84. * Returns only the hashable part of a key (delimited by "{...}"), or the
  85. * whole key if a key tag is not found in the string.
  86. *
  87. * @param string $key A key.
  88. * @return string
  89. */
  90. public static function extractKeyTag($key)
  91. {
  92. $start = strpos($key, '{');
  93. if ($start !== false) {
  94. $end = strpos($key, '}', $start);
  95. if ($end !== false) {
  96. $key = substr($key, ++$start, $end - $start);
  97. }
  98. }
  99. return $key;
  100. }
  101. }