|
| 1 | +<?php |
| 2 | + |
| 3 | +set_time_limit(0); |
| 4 | + |
| 5 | +require('../../vendor/autoload.php'); |
| 6 | + |
| 7 | +use PubNub\PNConfiguration; |
| 8 | +use PubNub\PubNub; |
| 9 | + |
| 10 | +$pnconf = new PNConfiguration(); |
| 11 | +$pnconf->setPublishKey("demo"); |
| 12 | +$pnconf->setSubscribeKey("demo"); |
| 13 | +$pnconf->setUuid("example"); |
| 14 | + |
| 15 | +$pubnub = new PubNub($pnconf); |
| 16 | + |
| 17 | +$channel = "demo_example"; |
| 18 | + |
| 19 | +print("We're setting the channel's $channel additional info.\n"); |
| 20 | +print("\tTo exit type '/exit'\n"); |
| 21 | +print("\tTo show the current object type '/show'\n"); |
| 22 | +print("\tTo show this help type '/help'\n"); |
| 23 | + |
| 24 | +print("Enter the channel name: "); |
| 25 | +$name = trim(fgets(STDIN)); |
| 26 | + |
| 27 | +print("Enter the channel description: "); |
| 28 | +$description = trim(fgets(STDIN)); |
| 29 | + |
| 30 | +// Set channel metadata |
| 31 | +$pubnub->setChannelMetadata() |
| 32 | + ->channel($channel) |
| 33 | + ->meta([ |
| 34 | + "name" => $name, |
| 35 | + "description" => $description, |
| 36 | + ]) |
| 37 | + ->sync(); |
| 38 | + |
| 39 | +print("The channel has been created with name and description.\n"); |
| 40 | + |
| 41 | +while (true) { |
| 42 | + // Fetch current channel metadata |
| 43 | + $response = $pubnub->getChannelMetadata() |
| 44 | + ->channel($channel) |
| 45 | + ->sync(); |
| 46 | + |
| 47 | + print("Enter the field name: "); |
| 48 | + $fieldName = trim(fgets(STDIN)); |
| 49 | + |
| 50 | + if ($fieldName === '/exit') { |
| 51 | + exit(); |
| 52 | + } elseif ($fieldName === '/show') { |
| 53 | + print_r($response); |
| 54 | + continue; |
| 55 | + } elseif ($fieldName === '/help') { |
| 56 | + print("\tTo exit type '/exit'\n"); |
| 57 | + print("\tTo show the current object type '/show'\n"); |
| 58 | + print("\tTo show this help type '/help'\n"); |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + print("Enter the field value: "); |
| 63 | + $fieldValue = trim(fgets(STDIN)); |
| 64 | + |
| 65 | + // Prepare custom fields |
| 66 | + $custom = (array)$response->getCustom(); |
| 67 | + |
| 68 | + if (isset($custom[$fieldName])) { |
| 69 | + print("Field $fieldName already has a value. Overwrite? (y/n): "); |
| 70 | + $confirmation = trim(fgets(STDIN)); |
| 71 | + |
| 72 | + if (strtolower($confirmation) !== 'y') { |
| 73 | + print("Object will not be updated.\n"); |
| 74 | + continue; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Update custom field |
| 79 | + $custom[$fieldName] = $fieldValue; |
| 80 | + |
| 81 | + // Writing the updated object back to the server |
| 82 | + $pubnub->setChannelMetadata() |
| 83 | + ->channel($channel) |
| 84 | + ->meta([ |
| 85 | + "name" => $response->getName(), |
| 86 | + "description" => $response->getDescription(), |
| 87 | + "custom" => $custom, |
| 88 | + ]) |
| 89 | + ->sync(); |
| 90 | + print("Object has been updated.\n"); |
| 91 | +} |
0 commit comments