PredisConnection.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Illuminate\Redis\Connections;
  3. use Closure;
  4. use Illuminate\Contracts\Redis\Connection as ConnectionContract;
  5. use Predis\Command\Argument\ArrayableArgument;
  6. /**
  7. * @mixin \Predis\Client
  8. */
  9. class PredisConnection extends Connection implements ConnectionContract
  10. {
  11. /**
  12. * The Predis client.
  13. *
  14. * @var \Predis\Client
  15. */
  16. protected $client;
  17. /**
  18. * Create a new Predis connection.
  19. *
  20. * @param \Predis\Client $client
  21. * @return void
  22. */
  23. public function __construct($client)
  24. {
  25. $this->client = $client;
  26. }
  27. /**
  28. * Subscribe to a set of given channels for messages.
  29. *
  30. * @param array|string $channels
  31. * @param \Closure $callback
  32. * @param string $method
  33. * @return void
  34. */
  35. public function createSubscription($channels, Closure $callback, $method = 'subscribe')
  36. {
  37. $loop = $this->pubSubLoop();
  38. $loop->{$method}(...array_values((array) $channels));
  39. foreach ($loop as $message) {
  40. if ($message->kind === 'message' || $message->kind === 'pmessage') {
  41. $callback($message->payload, $message->channel);
  42. }
  43. }
  44. unset($loop);
  45. }
  46. /**
  47. * Parse the command's parameters for event dispatching.
  48. *
  49. * @param array $parameters
  50. * @return array
  51. */
  52. protected function parseParametersForEvent(array $parameters)
  53. {
  54. return collect($parameters)
  55. ->transform(function ($parameter) {
  56. return $parameter instanceof ArrayableArgument
  57. ? $parameter->toArray()
  58. : $parameter;
  59. })->all();
  60. }
  61. }