Skip to content

Commit 7f115c5

Browse files
kmazariegosKarla Mazariegos
andauthored
PHP Example App: Add DSync Pagination Example (#27)
Co-authored-by: Karla Mazariegos <[email protected]>
1 parent 63e5a0c commit 7f115c5

File tree

14 files changed

+601
-340
lines changed

14 files changed

+601
-340
lines changed

php-directory-sync-example/router.php

Lines changed: 101 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require __DIR__ . "/vendor/autoload.php";
3+
require __DIR__ . "/vendor/autoload.php";
44
error_reporting(E_ALL ^ E_WARNING);
55
use Twig\Environment;
66
use Twig\Loader\FilesystemLoader;
@@ -30,6 +30,28 @@ function httpNotFound()
3030
return true;
3131
}
3232

33+
// Convenient function to transform an object to an associative array
34+
function objectToArray($d)
35+
{
36+
if (is_object($d)) {
37+
// Gets the properties of the given object
38+
// with get_object_vars function
39+
$d = get_object_vars($d);
40+
}
41+
42+
if (is_array($d)) {
43+
/*
44+
* Return array converted to object
45+
* Using __FUNCTION__ (Magic constant)
46+
* for recursive call
47+
*/
48+
return array_map(__FUNCTION__, $d);
49+
} else {
50+
// Return array
51+
return $d;
52+
}
53+
}
54+
3355
// Routing
3456
switch (strtok($_SERVER["REQUEST_URI"], "?")) {
3557
case (preg_match("/\.css$/", $_SERVER["REQUEST_URI"]) ? true : false):
@@ -50,37 +72,95 @@ function httpNotFound()
5072
}
5173
return httpNotFound();
5274

53-
//Users endpoint for listUsers function, simply prints first 10 users to the page
54-
case ("/users"):
75+
// home and /login will display the login page
76+
case ("/"):
77+
session_start();
78+
$before = $_GET['before'] ?? "";
79+
$after = $_GET['after'] ?? "";
80+
$directoriesList = new WorkOS\DirectorySync();
81+
[$before, $after, $currentPage] = $directoriesList->listDirectories(
82+
limit: 5,
83+
before: $before,
84+
after: $after,
85+
order: null
86+
);
87+
$parsedDirectories = $currentPage;
88+
echo $twig->render("login.html.twig", ['directories' => $parsedDirectories, 'after' => $after, 'before' => $before]);
89+
return true;
90+
91+
//Directory endpoint
92+
case ("/directory"):
93+
session_start();
5594
$directoryId = htmlspecialchars($_GET["id"]);
56-
$usersList = (new \WorkOS\DirectorySync())
57-
->listUsers(
95+
$directory = (new \WorkOS\DirectorySync())
96+
->getDirectory(
5897
$directoryId
5998
);
60-
$users = json_encode($usersList, JSON_PRETTY_PRINT);
61-
echo $twig->render('users.html.twig', ['users' => $users]);
99+
$parsed_directory = json_encode($directory, JSON_PRETTY_PRINT);
100+
$directoryPayloadArray = objectToArray($directory);
101+
$directoryPayloadArrayRawData = $directoryPayloadArray['raw'];
102+
$directoryName = $directoryPayloadArrayRawData["name"] ?? "";
103+
$directoryType = $directoryPayloadArrayRawData["type"] ?? "";
104+
$directoryDomain = $directoryPayloadArrayRawData["domain"] ?? "";
105+
$directoryCreated = $directoryPayloadArrayRawData["created_at"] ?? "";
106+
$_SESSION['id'] = $directoryId;
107+
echo $twig->render('directory.html.twig', ['directory' => $parsed_directory, 'id' => $_SESSION['id'], 'name'=> $directoryName, 'type'=>$directoryType, 'domain'=>$directoryDomain, 'created_at'=>$directoryCreated]);
62108
return true;
63109

64-
//Groups endpoint for listGroups function, simply prints groups to the page
65-
case ("/groups"):
66-
$directoryId = htmlspecialchars($_GET["id"]);
67-
$groupsList = (new \WorkOS\DirectorySync())
110+
//Groups & Users endpoint for listGroups & listUsers function
111+
case ("/usersgroups"):
112+
session_start();
113+
$directoryId = $_GET["id"];
114+
$directory = (new \WorkOS\DirectorySync())
115+
->getDirectory(
116+
$directoryId
117+
);
118+
$parsed_directory = json_encode($directory, JSON_PRETTY_PRINT);
119+
$directoryPayloadArray = objectToArray($directory);
120+
$directoryPayloadArrayRawData = $directoryPayloadArray['raw'];
121+
$directoryName = $directoryPayloadArrayRawData["name"] ?? "";
122+
$_SESSION['directoryName'] = $directoryName;
123+
[$before, $after, $groups] = (new \WorkOS\DirectorySync())
68124
->listGroups(
69125
$directoryId
70126
);
71-
$groups = json_encode($groupsList, JSON_PRETTY_PRINT);
72-
echo $twig->render('groups.html.twig', ['groups' => $groups]);
127+
[$before, $after, $users] = (new \WorkOS\DirectorySync())
128+
->listUsers(
129+
$directoryId
130+
);
131+
echo $twig->render('groups.html.twig', ['groups' => $groups, 'users' => $users, 'name' => $_SESSION['directoryName'], 'directory' => $directoryId]);
73132
return true;
74133

75-
//Directory endpoint
76-
case ("/directory"):
77-
$directoryId = htmlspecialchars($_GET["id"]);
78-
$directory = (new \WorkOS\DirectorySync())
79-
->getDirectory(
80-
$directoryId
134+
135+
//User endpoint
136+
case ("/user"):
137+
session_start();
138+
$userId = $_GET["id"];
139+
$user = (new \WorkOS\DirectorySync())
140+
->getUser(
141+
$userId
81142
);
82-
$parsed_directory = json_encode($directory, JSON_PRETTY_PRINT);
83-
echo $twig->render('directory.html.twig', ['directory' => $parsed_directory, 'id' => $directoryId]);
143+
$userPayload = json_encode($user, JSON_PRETTY_PRINT);
144+
$userArray = objectToArray($user);
145+
$userRaw = $userArray['raw'];
146+
$userName = $userRaw["first_name"] ?? "";
147+
echo $twig->render('user.html.twig', ['user' => $userPayload, 'firstName' => $userName, 'directoryName' => $_SESSION['directoryName']]);
148+
return true;
149+
150+
//Group endpoint
151+
case ("/group"):
152+
session_start();
153+
$groupId = $_GET["id"];
154+
$group = (new \WorkOS\DirectorySync())
155+
->getGroup(
156+
$groupId
157+
);
158+
$groupPayload = json_encode($group, JSON_PRETTY_PRINT);
159+
$groupArray = objectToArray($group);
160+
$groupRaw = $groupArray['raw'];
161+
$groupName = $groupRaw["name"] ?? "";
162+
$_SESSION['directoryName'];
163+
echo $twig->render('group.html.twig', ['group' => $groupPayload, 'name' => $groupName, 'directoryName' => $_SESSION['directoryName']]);
84164
return true;
85165

86166

@@ -106,15 +186,6 @@ function httpNotFound()
106186
echo $twig->render('webhooks.html.twig');
107187
return true;
108188

109-
// home and /login will display the login page
110-
case ("/"):
111-
case ("/login"):
112-
$directoriesList = (new \WorkOS\DirectorySync())
113-
->listDirectories();
114-
$parsedDirectories = $directoriesList[2];
115-
echo $twig->render("login.html.twig", ['directories' => $parsedDirectories]);
116-
return true;
117-
118189
// Any other endpoint returns a 404
119190
default:
120191
return httpNotFound();

0 commit comments

Comments
 (0)