Skip to content

Commit de1c1a3

Browse files
Add missing code snippets for documentation (#125)
* Add new batch of code snippets from documentation * Remove faulty test declaration * Test remove subscribing snippets test file * Fixes for failing tests * More test fixes * Even more test fixes * Cryptor setup snippet fix * Linter fixes * Edit ParseToken snippet
1 parent 5e9faf5 commit de1c1a3

14 files changed

+670
-22
lines changed

examples/AccessManagerV2.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/../vendor/autoload.php');
4+
5+
use PubNub\PubNub;
6+
use PubNub\PNConfiguration;
7+
8+
// snippet.setup
9+
$config = new PNConfiguration();
10+
$config->setSecretKey(getenv('SECRET_KEY') ?: 'demo');
11+
$config->setPublishKey(getenv('PUBLISH_KEY') ?: 'demo');
12+
$config->setSubscribeKey(getenv('SUBSCRIBE_KEY') ?: 'demo');
13+
$config->setUserId('example');
14+
15+
$pubnub = new PubNub($config);
16+
// snippet.end
17+
18+
// snippet.grant_channel_authkey
19+
$channels = ["ch1", "ch2"];
20+
$auth_key = "blah";
21+
$read = true;
22+
$write = false;
23+
$ttl_mins = 15;
24+
try {
25+
$result = $pubnub->grant()
26+
->channels($channels)
27+
->authKeys($auth_key)
28+
->read($read)
29+
->write($write)
30+
->ttl($ttl_mins)
31+
->sync();
32+
print("Grant successful\n");
33+
} catch (\PubNub\Exceptions\PubNubServerException $exception) {
34+
print_r("Message: " . $exception->getMessage() . "\n");
35+
print_r("Status: " . $exception->getStatusCode() . "\n");
36+
echo "Original message: ";
37+
print_r($exception->getBody());
38+
} catch (\PubNub\Exceptions\PubNubException $exception) {
39+
print_r("Message: " . $exception->getMessage());
40+
}
41+
// snippet.end
42+
43+
// snippet.grant_all_channels
44+
$pubnub->grant()
45+
->read(true)
46+
->write(true)
47+
->sync();
48+
// snippet.end
49+
50+
// snippet.grant_specific_channel
51+
$pubnub->grant()
52+
->channels("my_channel")
53+
->read(true)
54+
->write(true)
55+
->sync();
56+
// snippet.end
57+
58+
// snippet.grant_channel_with_authkey
59+
$pubnub->grant()
60+
->channels("my_channel")
61+
->read(true)
62+
->write(false)
63+
->authKeys("my_ro_authkey")
64+
->ttl(5)
65+
->sync();
66+
// snippet.end
67+
68+
// snippet.grant_presence_channel
69+
$pubnub->grant()
70+
->channels("my_channel-pnpres")
71+
->read(true)
72+
->write(true)
73+
->sync();
74+
// snippet.end
75+
76+
// snippet.grant_channel_group
77+
$result = $pubnub->grant()
78+
->channelGroups(["cg1", "cg2", "cg3"])
79+
->authKeys(["key1", "key2", "auth3"])
80+
->read(true)
81+
->write(true)
82+
->manage(true)
83+
->ttl(12237)
84+
->sync();
85+
// snippet.end
86+
87+
// snippet.grant_application_level
88+
try {
89+
$result = $pubnub->grant()
90+
->read(true)
91+
->write(true)
92+
->sync();
93+
94+
print_r($result);
95+
} catch (\PubNub\Exceptions\PubNubServerException $exception) {
96+
print_r($exception->getMessage() . "\n");
97+
print_r($exception->getStatusCode() . "\n");
98+
print_r($exception->getBody());
99+
} catch (\PubNub\Exceptions\PubNubException $exception) {
100+
print_r($exception->getMessage());
101+
}
102+
// snippet.end
103+
104+
// snippet.grant_channel_level
105+
$result = $pubnub->grant()
106+
->channels("my_channel")
107+
->read(true)
108+
->write(true)
109+
->sync();
110+
// snippet.end
111+
112+
// snippet.grant_user_level
113+
$result = $pubnub->grant()
114+
->channels("my_channel")
115+
->authKeys("my_authkey")
116+
->read(true)
117+
->write(true)
118+
->ttl(5)
119+
->sync();
120+
// snippet.end

examples/AppContext.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@
198198
assert($updateChannelMetadataResult->getName() === $sampleChannels[0]['name'] . ' - Updated');
199199
// snippet.end
200200

201+
// snippet.write_updated_metadata_back
202+
// Writing the updated object back to the server
203+
$pubnub->setChannelMetadata()
204+
->channel($sampleChannels[0]['id'])
205+
->meta([
206+
"name" => $updateChannelMetadataResult->getName(),
207+
"description" => $updateChannelMetadataResult->getDescription(),
208+
"custom" => $updatedChannelCustom,
209+
])
210+
->sync();
211+
print("Object has been updated.\n");
212+
// snippet.end
213+
201214
// snippet.remove_channel_metadata
202215
$removeChannelMetadataResult = $pubnub->removeChannelMetadata()
203216
->channel($sampleChannels[1]['id'])

examples/ChannelGroups.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/../vendor/autoload.php');
4+
5+
use PubNub\PNConfiguration;
6+
use PubNub\PubNub;
7+
use PubNub\Exceptions\PubNubServerException;
8+
9+
// snippet.setup
10+
// Create configuration
11+
$pnConfig = new PNConfiguration();
12+
$pnConfig->setSubscribeKey(getenv("SUBSCRIBE_KEY") ?? "demo");
13+
$pnConfig->setPublishKey(getenv("PUBLISH_KEY") ?? "demo");
14+
$pnConfig->setUserId("php-channel-group-demo");
15+
16+
// Initialize PubNub instance
17+
$pubnub = new PubNub($pnConfig);
18+
// snippet.end
19+
20+
// snippet.add_channels
21+
$result = $pubnub->addChannelToChannelGroup()
22+
->channels(["news", "sports"])
23+
->channelGroup("my-group")
24+
->sync();
25+
26+
echo "Channels added to group successfully!" . PHP_EOL;
27+
// snippet.end
28+
29+
// snippet.list_channels
30+
$result = $pubnub->listChannelsInChannelGroup()
31+
->channelGroup("cg1")
32+
->sync();
33+
// snippet.end
34+
35+
// snippet.remove_channels
36+
$pubnub->removeChannelFromChannelGroup()
37+
->channels("son")
38+
->channelGroup("family")
39+
->sync();
40+
// snippet.end
41+
42+
// snippet.delete_channel_group
43+
$pubnub->removeChannelGroup()
44+
->channelGroup("family")
45+
->sync();
46+
// snippet.end

examples/Configuration.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
use PubNub\PNConfiguration;
88
use PubNub\PubNub;
99
use PubNub\CryptoModule;
10+
use PubNub\Crypto\AesCbcCryptor;
11+
use PubNub\Crypto\LegacyCryptor;
1012
use PubNub\Enums\PNStatusCategory;
1113
use PubNub\Callbacks\SubscribeCallback;
14+
use PubNub\PubNubCrypto;
15+
use PubNub\PubNubCryptoLegacy;
1216

1317
// snippet.setup
1418
// Create a new configuration instance
@@ -202,6 +206,103 @@ function presence($pubnub, $presence)
202206
$pubnub = new PubNub($pnconf);
203207
// snippet.end
204208

209+
// snippet.init_non_secure
210+
$pnConfiguration = new PNConfiguration();
211+
212+
$pnConfiguration->setSubscribeKey("my_sub_key");
213+
$pnConfiguration->setPublishKey("my_pub_key");
214+
$pnConfiguration->setSecure(false);
215+
$pnConfiguration->setUserId("myUniqueUserId");
216+
$pubnub = new PubNub($pnConfiguration);
217+
// snippet.end
218+
219+
// snippet.init_read_only
220+
$pnConfiguration = new PNConfiguration();
221+
222+
$pnConfiguration->setUserId("myUniqueUserId");
223+
$pnConfiguration->setSubscribeKey("my_sub_key");
224+
225+
$pubnub = new PubNub($pnConfiguration);
226+
// snippet.end
227+
228+
// snippet.use_custom_uuid
229+
$pnconf = new PNConfiguration();
230+
231+
$pnconf->setSubscribeKey("mySubscribeKey");
232+
$pnconf->setPublishKey("myPublishKey");
233+
$pnconf->setUserId("myUniqueUserId");
234+
235+
$pubnub = new PubNub($pnconf);
236+
// snippet.end
237+
238+
// snippet.remove_listeners
239+
$subscribeCallback = new MySubscribeCallback();
240+
241+
$pubnub->addListener($subscribeCallback);
242+
243+
// some time later
244+
$pubnub->removeListener($subscribeCallback);
245+
// snippet.end
246+
247+
// snippet.set_user_id
248+
$pnconf = new PNConfiguration();
249+
$pnconf->setUserId("myUniqueUserId");
250+
// snippet.end
251+
252+
// snippet.get_user_id
253+
$pubnub->getConfiguration()
254+
->getUserId();
255+
// snippet.end
256+
257+
// snippet.set_auth_key
258+
$pubnub->getConfiguration()
259+
->setAuthKey("my_newauthkey");
260+
// snippet.end
261+
262+
// snippet.get_auth_key
263+
$pubnub->getConfiguration()
264+
->getAuthKey();
265+
// snippet.end
266+
267+
// snippet.get_filter_expression
268+
$pubnub->getConfiguration()->getFilterExpression();
269+
// snippet.end
270+
271+
// snippet.disable_immutable_check
272+
$config = new PNConfiguration();
273+
$config->setPublishKey('demo');
274+
$config->setSubscribeKey('demo');
275+
$config->setUserId('demo');
276+
// not recommended, disables config immutability
277+
$config->disableImmutableCheck();
278+
279+
$pn = new PubNub($config);
280+
// snippet.end
281+
282+
// snippet.crypto_module_setup
283+
// encrypts using 256-bit AES-CBC cipher (recommended)
284+
// decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
285+
$pnConfiguration = new PNConfiguration();
286+
$pnConfiguration->setSubscribeKey('demo');
287+
$pnConfiguration->setPublishKey('demo');
288+
$pnConfiguration->setUserId('crypto-demo');
289+
$cryptoModule = CryptoModule::aesCbcCryptor("enigma", true);
290+
$pnConfiguration->setCryptoModule($cryptoModule);
291+
292+
$pubnub = new PubNub($pnConfiguration);
293+
294+
// encrypts with 128-bit cipher key entropy(legacy)
295+
// decrypts data encrypted with the legacy and the 256-bit AES-CBC ciphers
296+
$pnConfiguration2 = new PNConfiguration();
297+
$pnConfiguration2->setSubscribeKey('demo');
298+
$pnConfiguration2->setPublishKey('demo');
299+
$pnConfiguration2->setUserId('crypto-demo-legacy');
300+
$legacyCryptoModule = CryptoModule::legacyCryptor("enigma", true);
301+
$pnConfiguration2->setCryptoModule($legacyCryptoModule);
302+
303+
$pubnub2 = new PubNub($pnConfiguration2);
304+
// snippet.end
305+
205306
// Verify configuration
206307
assert($pnconf->getSubscribeKey() === (getenv('SUBSCRIBE_KEY') ?: 'demo'));
207308
assert($pnconf->getUserId() === "filter-demo-user");

examples/MessageActions.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,45 @@ function tagForAnalytics($pubnub, $channel, $messageTimetoken, $tag, $value)
491491
}
492492

493493
echo "\n=== MESSAGE ACTIONS DEMO COMPLETE ===\n";
494+
495+
// snippet.fetch_messages_with_paging
496+
function getMessageActionsWithPaging($pubnub, $channel, $start = null)
497+
{
498+
$allActions = [];
499+
$currentStart = $start;
500+
501+
do {
502+
$builder = $pubnub->getMessageActions()
503+
->channel($channel);
504+
505+
if ($currentStart !== null) {
506+
$builder->setStart($currentStart);
507+
}
508+
509+
$result = $builder->sync();
510+
$actions = $result->actions;
511+
512+
if (!empty($actions)) {
513+
$allActions = array_merge($allActions, $actions);
514+
// Get the timetoken of the last action for pagination
515+
$lastAction = end($actions);
516+
$currentStart = $lastAction->actionTimetoken;
517+
} else {
518+
break;
519+
}
520+
} while (!empty($actions) && count($allActions) < 20); // Limit to 20 for demo
521+
522+
return $allActions;
523+
}
524+
525+
// Usage example
526+
$actions = getMessageActionsWithPaging($pubnub, 'my_channel');
527+
foreach ($actions as $action) {
528+
echo "Type: " . $action->getType() . "\n";
529+
echo "Value: " . $action->getValue() . "\n";
530+
echo "UUID: " . $action->getUuid() . "\n";
531+
echo "Message Timetoken: " . $action->getMessageTimetoken() . "\n";
532+
echo "Action Timetoken: " . $action->getActionTimetoken() . "\n";
533+
echo "---\n";
534+
}
535+
// snippet.end

examples/MessagePersistance.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,13 @@ public function demoHistoryReverse()
237237
echo "Retrieving oldest 3 messages first...\n";
238238

239239
try {
240+
// snippet.history_reverse
240241
$result = $this->pubnub->history()
241-
->channel($this->demoChannel)
242+
->channel("my_channel")
242243
->count(3)
243244
->reverse(true)
244-
->includeTimetoken(true)
245245
->sync();
246+
// snippet.end
246247

247248
echo " 📊 Reverse History Results:\n";
248249
$messages = $result->getMessages();

examples/MobilePush.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,19 @@ private function demonstrateRemoveAllChannelsFromDevice($device)
271271
->deviceId($device['deviceId'])
272272
->sync();
273273
// snippet.end
274+
275+
// snippet.remove_all_channels_response_check
276+
$response = $this->pubnub->removeAllPushChannelsForDevice()
277+
->pushType(PNPushType::APNS2)
278+
->deviceId("yourDeviceId")
279+
->sync();
280+
281+
if ($response->isSuccessful()) {
282+
echo "Successfully removed all push channels from device.";
283+
} else {
284+
echo "Failed to remove push channels.";
285+
}
286+
// snippet.end
274287
}
275288

276289
echo " ✅ Successfully removed all push channels from device\n\n";

0 commit comments

Comments
 (0)