|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +declare(strict_types=1); |
| 23 | + |
| 24 | +use Iggy\Client; |
| 25 | +use Iggy\PollingStrategy; |
| 26 | +use Iggy\SendMessage; |
| 27 | + |
| 28 | +function iggy_connection_string(): string |
| 29 | +{ |
| 30 | + $configured = getenv('IGGY_CONNECTION_STRING'); |
| 31 | + if ($configured !== false && $configured !== '') { |
| 32 | + return $configured; |
| 33 | + } |
| 34 | + |
| 35 | + $host = getenv('IGGY_HOST') ?: '127.0.0.1'; |
| 36 | + $port = getenv('IGGY_PORT') ?: '8090'; |
| 37 | + $username = rawurlencode(getenv('IGGY_USERNAME') ?: 'iggy'); |
| 38 | + $password = rawurlencode(getenv('IGGY_PASSWORD') ?: 'iggy'); |
| 39 | + |
| 40 | + return "iggy+tcp://{$username}:{$password}@{$host}:{$port}"; |
| 41 | +} |
| 42 | + |
| 43 | +function iggy_client(): Client |
| 44 | +{ |
| 45 | + $client = Client::fromConnectionString(iggy_connection_string()); |
| 46 | + $client->connect(); |
| 47 | + |
| 48 | + return $client; |
| 49 | +} |
| 50 | + |
| 51 | +function ensure_stream_and_topic(Client $client, string $stream, string $topic): void |
| 52 | +{ |
| 53 | + if ($client->getStream($stream) === null) { |
| 54 | + $client->createStream($stream); |
| 55 | + } |
| 56 | + |
| 57 | + if ($client->getTopic($stream, $topic) === null) { |
| 58 | + $client->createTopic($stream, $topic, 1); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function send_payloads(Client $client, string $stream, string $topic, array $payloads): void |
| 63 | +{ |
| 64 | + $messages = array_map( |
| 65 | + static fn (string $payload): SendMessage => new SendMessage($payload), |
| 66 | + $payloads, |
| 67 | + ); |
| 68 | + |
| 69 | + $client->sendMessages($stream, $topic, 0, $messages); |
| 70 | +} |
| 71 | + |
| 72 | +function print_polled_messages(Client $client, string $stream, string $topic, int $count): void |
| 73 | +{ |
| 74 | + $messages = $client->pollMessages($stream, $topic, 0, PollingStrategy::first(), $count, true); |
| 75 | + |
| 76 | + if (count($messages) < $count) { |
| 77 | + throw new RuntimeException("Expected {$count} messages, received " . count($messages)); |
| 78 | + } |
| 79 | + |
| 80 | + foreach ($messages as $message) { |
| 81 | + echo "Received message at offset {$message->offset()}: {$message->payload()}", PHP_EOL; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +function example_payloads(string $prefix, int $count): array |
| 86 | +{ |
| 87 | + return array_map( |
| 88 | + static fn (int $index): string => "{$prefix}-message-{$index}", |
| 89 | + range(1, $count), |
| 90 | + ); |
| 91 | +} |
0 commit comments