Skip to content

Commit 220232a

Browse files
Merge pull request #2274 from RudIhor/fix/improve-php-tutorials
docs(php): improve PHP documentation with the PHP7 capabilities
2 parents 91871ca + 89696c8 commit 220232a

File tree

6 files changed

+47
-40
lines changed

6 files changed

+47
-40
lines changed

tutorials/tutorial-five-php.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ $channel = $connection->channel();
138138

139139
$channel->exchange_declare('topic_logs', 'topic', false, false, false);
140140

141-
$routing_key = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'anonymous.info';
141+
$routing_key = $argv[1] ?? 'anonymous.info';
142142
$data = implode(' ', array_slice($argv, 2));
143143
if (empty($data)) {
144144
$data = "Hello World!";
@@ -161,6 +161,7 @@ The code for `receive_logs_topic.php`:
161161

162162
require_once __DIR__ . '/vendor/autoload.php';
163163
use PhpAmqpLib\Connection\AMQPStreamConnection;
164+
use PhpAmqpLib\Message\AMQPMessage;
164165

165166
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
166167
$channel = $connection->channel();
@@ -181,7 +182,7 @@ foreach ($binding_keys as $binding_key) {
181182

182183
echo " [*] Waiting for logs. To exit press CTRL+C\n";
183184

184-
$callback = function ($msg) {
185+
$callback = function (AMQPMessage $msg) {
185186
echo ' [x] ', $msg->getRoutingKey(), ':', $msg->getBody(), "\n";
186187
};
187188

tutorials/tutorial-four-php.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ $channel = $connection->channel();
168168

169169
$channel->exchange_declare('direct_logs', 'direct', false, false, false);
170170

171-
$severity = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'info';
171+
$severity = $argv[1] ?? 'info';
172172

173173
$data = implode(' ', array_slice($argv, 2));
174174
if (empty($data)) {
@@ -193,6 +193,7 @@ The code for `receive_logs_direct.php`:
193193

194194
require_once __DIR__ . '/vendor/autoload.php';
195195
use PhpAmqpLib\Connection\AMQPStreamConnection;
196+
use PhpAmqpLib\Message\AMQPMessage;
196197

197198
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
198199
$channel = $connection->channel();
@@ -213,7 +214,7 @@ foreach ($severities as $severity) {
213214

214215
echo " [*] Waiting for logs. To exit press CTRL+C\n";
215216

216-
$callback = function ($msg) {
217+
$callback = function (AMQPMessage $msg) {
217218
echo ' [x] ', $msg->getRoutingKey(), ':', $msg->getBody(), "\n";
218219
};
219220

tutorials/tutorial-one-php.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ The code (in [`receive.php`](https://github.com/rabbitmq/rabbitmq-tutorials/blob
168168
```php
169169
require_once __DIR__ . '/vendor/autoload.php';
170170
use PhpAmqpLib\Connection\AMQPStreamConnection;
171+
use PhpAmqpLib\Message\AMQPMessage;
171172
```
172173

173174
Setting up is the same as the publisher; we open a connection and a
@@ -193,7 +194,7 @@ that will receive the messages sent by the server. Keep in mind
193194
that messages are sent asynchronously from the server to the clients.
194195

195196
```php
196-
$callback = function ($msg) {
197+
$callback = function (AMQPMessage $msg) {
197198
echo ' [x] Received ', $msg->body, "\n";
198199
};
199200

tutorials/tutorial-six-php.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ list($queue_name, ,) = $channel->queue_declare("", false, false, true, false);
8787

8888
$msg = new AMQPMessage(
8989
$payload,
90-
array('reply_to' => $queue_name)
90+
['reply_to' => $queue_name]
9191
);
9292

9393
$channel->basic_publish($msg, '', 'rpc_queue');
@@ -159,14 +159,15 @@ Putting it all together
159159
The Fibonacci task:
160160

161161
```php
162-
function fib($n)
162+
function fib(int $n)
163163
{
164-
if ($n == 0) {
164+
if ($n === 0) {
165165
return 0;
166166
}
167-
if ($n == 1) {
167+
if ($n === 1) {
168168
return 1;
169169
}
170+
170171
return fib($n-1) + fib($n-2);
171172
}
172173
```
@@ -189,25 +190,26 @@ $channel = $connection->channel();
189190

190191
$channel->queue_declare('rpc_queue', false, false, false, false);
191192

192-
function fib($n)
193+
function fib(int $n)
193194
{
194-
if ($n == 0) {
195+
if ($n === 0) {
195196
return 0;
196197
}
197-
if ($n == 1) {
198+
if ($n === 1) {
198199
return 1;
199200
}
201+
200202
return fib($n-1) + fib($n-2);
201203
}
202204

203205
echo " [x] Awaiting RPC requests\n";
204-
$callback = function ($req) {
206+
$callback = function (AMQPMessage $req) {
205207
$n = intval($req->getBody());
206208
echo ' [.] fib(', $n, ")\n";
207209

208210
$msg = new AMQPMessage(
209211
(string) fib($n),
210-
array('correlation_id' => $req->get('correlation_id'))
212+
['correlation_id' => $req->get('correlation_id')]
211213
);
212214

213215
$req->getChannel()->basic_publish(
@@ -218,7 +220,7 @@ $callback = function ($req) {
218220
$req->ack();
219221
};
220222

221-
$channel->basic_qos(null, 1, false);
223+
$channel->basic_qos(0, 1, false);
222224
$channel->basic_consume('rpc_queue', '', false, false, false, false, $callback);
223225

224226
try {
@@ -250,12 +252,13 @@ The code for our RPC client [rpc_client.php](https://github.com/rabbitmq/rabbitm
250252

251253
require_once __DIR__ . '/vendor/autoload.php';
252254
use PhpAmqpLib\Connection\AMQPStreamConnection;
255+
use PhpAmqpLib\Channel\AMQPChannel;
253256
use PhpAmqpLib\Message\AMQPMessage;
254257

255258
class FibonacciRpcClient
256259
{
257-
private $connection;
258-
private $channel;
260+
private AMQPStreamConnection $connection;
261+
private AMQPChannel $channel;
259262
private $callback_queue;
260263
private $response;
261264
private $corr_id;
@@ -283,36 +286,37 @@ class FibonacciRpcClient
283286
true,
284287
false,
285288
false,
286-
array(
289+
[
287290
$this,
288291
'onResponse'
289-
)
292+
]
290293
);
291294
}
292295

293-
public function onResponse($rep)
296+
public function onResponse(AMQPMessage $rep)
294297
{
295-
if ($rep->get('correlation_id') == $this->corr_id) {
296-
$this->response = $rep->body;
298+
if ($rep->get('correlation_id') === $this->corr_id) {
299+
$this->response = $rep->body();
297300
}
298301
}
299302

300-
public function call($n)
303+
public function call(int $n)
301304
{
302305
$this->response = null;
303306
$this->corr_id = uniqid();
304307

305308
$msg = new AMQPMessage(
306309
(string) $n,
307-
array(
310+
[
308311
'correlation_id' => $this->corr_id,
309-
'reply_to' => $this->callback_queue
310-
)
312+
'reply_to' => $this->callback_queue,
313+
]
311314
);
312315
$this->channel->basic_publish($msg, '', 'rpc_queue');
313316
while (!$this->response) {
314317
$this->channel->wait();
315318
}
319+
316320
return intval($this->response);
317321
}
318322
}

tutorials/tutorial-three-php.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ The code for `receive_logs.php`:
236236
237237
require_once __DIR__ . '/vendor/autoload.php';
238238
use PhpAmqpLib\Connection\AMQPStreamConnection;
239+
use PhpAmqpLib\Message\AMQPMessage;
239240
240241
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
241242
$channel = $connection->channel();
@@ -248,7 +249,7 @@ $channel->queue_bind($queue_name, 'logs');
248249
249250
echo " [*] Waiting for logs. To exit press CTRL+C\n";
250251
251-
$callback = function ($msg) {
252+
$callback = function (AMQPMessage $msg) {
252253
echo ' [x] ', $msg->getBody(), "\n";
253254
};
254255

tutorials/tutorial-two-php.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fake a second of work for every dot in the message body. It will pop
8181
messages from the queue and perform the task, so let's call it `worker.php`:
8282

8383
```php
84-
$callback = function ($msg) {
84+
$callback = function (AMQPMessage $msg) {
8585
echo ' [x] Received ', $msg->getBody(), "\n";
8686
sleep(substr_count($msg->getBody(), '.'));
8787
echo " [x] Done\n";
@@ -92,7 +92,7 @@ $channel->basic_consume('hello', '', false, true, false, false, $callback);
9292

9393
Note that our fake task simulates execution time.
9494

95-
Run them as in tutorial one:
95+
Run them as in the tutorial one:
9696

9797
```bash
9898
# shell 1
@@ -202,7 +202,7 @@ It's time to turn them on by setting the fourth parameter to `basic_consume` to
202202
from the worker, once we're done with a task.
203203

204204
```php
205-
$callback = function ($msg) {
205+
$callback = function (AMQPMessage $msg) {
206206
echo ' [x] Received ', $msg->getBody(), "\n";
207207
sleep(substr_count($msg->getBody(), '.'));
208208
echo " [x] Done\n";
@@ -281,10 +281,9 @@ even if RabbitMQ restarts. Now we need to mark our messages as persistent
281281
as part of the property array.
282282
283283
```php
284-
$msg = new AMQPMessage(
285-
$data,
286-
array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT)
287-
);
284+
$msg = new AMQPMessage($data, [
285+
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT
286+
]);
288287
```
289288
290289
> #### Note on message persistence
@@ -323,7 +322,7 @@ a new message to a worker until it has processed and acknowledged the
323322
previous one. Instead, it will dispatch it to the next worker that is not still busy.
324323
325324
```php
326-
$channel->basic_qos(null, 1, false);
325+
$channel->basic_qos(0, 1, false);
327326
```
328327
329328
> #### Note about queue size
@@ -352,10 +351,9 @@ $data = implode(' ', array_slice($argv, 1));
352351
if (empty($data)) {
353352
$data = "Hello World!";
354353
}
355-
$msg = new AMQPMessage(
356-
$data,
357-
array('delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT)
358-
);
354+
$msg = new AMQPMessage($data, [
355+
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
356+
]);
359357
360358
$channel->basic_publish($msg, '', 'task_queue');
361359
@@ -375,6 +373,7 @@ And our `worker.php`:
375373
376374
require_once __DIR__ . '/vendor/autoload.php';
377375
use PhpAmqpLib\Connection\AMQPStreamConnection;
376+
use PhpAmqpLib\Message\AMQPMessage;
378377
379378
$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
380379
$channel = $connection->channel();
@@ -383,7 +382,7 @@ $channel->queue_declare('task_queue', false, true, false, false);
383382
384383
echo " [*] Waiting for messages. To exit press CTRL+C\n";
385384
386-
$callback = function ($msg) {
385+
$callback = function (AMQPMessage $msg) {
387386
echo ' [x] Received ', $msg->getBody(), "\n";
388387
sleep(substr_count($msg->getBody(), '.'));
389388
echo " [x] Done\n";

0 commit comments

Comments
 (0)