|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use Slim\Views\Twig; |
| 4 | + |
3 | 5 | $autoloader = __DIR__ . '/../vendor/autoload.php'; |
4 | 6 | $sdkAutoloader = __DIR__ . '/../../../vendor/autoload.php'; |
5 | 7 |
|
|
23 | 25 | use OpenTok\OutputMode; |
24 | 26 |
|
25 | 27 | // PHP CLI webserver compatibility, serving static files |
26 | | -$filename = __DIR__ . preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']); |
| 28 | +$filename = __DIR__ . preg_replace('#(\?.*)$#', '', (string) $_SERVER['REQUEST_URI']); |
27 | 29 | if (php_sapi_name() === 'cli-server' && is_file($filename)) { |
28 | 30 | return false; |
29 | 31 | } |
|
34 | 36 | } |
35 | 37 |
|
36 | 38 | // Initialize Slim application |
37 | | -$app = new Slim(array( |
38 | | - 'templates.path' => __DIR__ . '/../templates', |
39 | | - 'view' => new \Slim\Views\Twig(), |
40 | | -)); |
| 39 | +$app = new Slim(['templates.path' => __DIR__ . '/../templates', 'view' => new Twig()]); |
41 | 40 |
|
42 | 41 | // Intialize a cache, store it in the app container |
43 | | -$app->container->singleton('cache', function () { |
44 | | - return new Cache(); |
45 | | -}); |
| 42 | +$app->container->singleton('cache', fn(): Cache => new Cache()); |
46 | 43 |
|
47 | 44 | // Initialize OpenTok instance, store it in the app contianer |
48 | | -$app->container->singleton('opentok', function () { |
49 | | - return new OpenTok(getenv('API_KEY'), getenv('API_SECRET')); |
50 | | -}); |
| 45 | +$app->container->singleton('opentok', fn(): OpenTok => new OpenTok(getenv('API_KEY'), getenv('API_SECRET'))); |
51 | 46 | // Store the API Key in the app container |
52 | 47 | $app->apiKey = getenv('API_KEY'); |
53 | 48 |
|
54 | 49 | // If a sessionId has already been created, retrieve it from the cache |
55 | | -$sessionId = $app->cache->getOrCreate('sessionId', array(), function () use ($app) { |
| 50 | +$sessionId = $app->cache->getOrCreate('sessionId', [], function () use ($app) { |
56 | 51 | // If the sessionId hasn't been created, create it now and store it |
57 | | - $session = $app->opentok->createSession(array( |
58 | | - 'mediaMode' => MediaMode::ROUTED |
59 | | - )); |
| 52 | + $session = $app->opentok->createSession(['mediaMode' => MediaMode::ROUTED]); |
60 | 53 | return $session->getSessionId(); |
61 | 54 | }); |
62 | 55 |
|
63 | 56 | // Configure routes |
64 | | -$app->get('/', function () use ($app) { |
| 57 | +$app->get('/', function () use ($app): void { |
65 | 58 | $app->render('index.html'); |
66 | 59 | }); |
67 | 60 |
|
68 | | -$app->get('/host', function () use ($app, $sessionId) { |
| 61 | +$app->get('/host', function () use ($app, $sessionId): void { |
69 | 62 |
|
70 | | - $token = $app->opentok->generateToken($sessionId, array( |
71 | | - 'role' => Role::MODERATOR |
72 | | - )); |
| 63 | + $token = $app->opentok->generateToken($sessionId, ['role' => Role::MODERATOR]); |
73 | 64 |
|
74 | | - $app->render('host.html', array( |
75 | | - 'apiKey' => $app->apiKey, |
76 | | - 'sessionId' => $sessionId, |
77 | | - 'token' => $token |
78 | | - )); |
| 65 | + $app->render('host.html', ['apiKey' => $app->apiKey, 'sessionId' => $sessionId, 'token' => $token]); |
79 | 66 | }); |
80 | 67 |
|
81 | | -$app->get('/participant', function () use ($app, $sessionId) { |
| 68 | +$app->get('/participant', function () use ($app, $sessionId): void { |
82 | 69 |
|
83 | | - $token = $app->opentok->generateToken($sessionId, array( |
84 | | - 'role' => Role::MODERATOR |
85 | | - )); |
| 70 | + $token = $app->opentok->generateToken($sessionId, ['role' => Role::MODERATOR]); |
86 | 71 |
|
87 | | - $app->render('participant.html', array( |
88 | | - 'apiKey' => $app->apiKey, |
89 | | - 'sessionId' => $sessionId, |
90 | | - 'token' => $token |
91 | | - )); |
| 72 | + $app->render('participant.html', ['apiKey' => $app->apiKey, 'sessionId' => $sessionId, 'token' => $token]); |
92 | 73 | }); |
93 | 74 |
|
94 | | -$app->get('/history', function () use ($app) { |
| 75 | +$app->get('/history', function () use ($app): void { |
95 | 76 | $page = intval($app->request->get('page')); |
96 | | - if (empty($page)) { |
| 77 | + if ($page === 0) { |
97 | 78 | $page = 1; |
98 | 79 | } |
99 | 80 |
|
100 | 81 | $offset = ($page - 1) * 5; |
101 | 82 |
|
102 | 83 | $archives = $app->opentok->listArchives($offset, 5); |
103 | 84 |
|
104 | | - $toArray = function ($archive) { |
105 | | - return $archive->toArray(); |
106 | | - }; |
| 85 | + $toArray = fn($archive) => $archive->toArray(); |
107 | 86 |
|
108 | | - $app->render('history.html', array( |
109 | | - 'archives' => array_map($toArray, $archives->getItems()), |
110 | | - 'showPrevious' => $page > 1 ? '/history?page=' . ($page - 1) : null, |
111 | | - 'showNext' => $archives->totalCount() > $offset + 5 ? '/history?page=' . ($page + 1) : null |
112 | | - )); |
| 87 | + $app->render('history.html', ['archives' => array_map($toArray, $archives->getItems()), 'showPrevious' => $page > 1 ? '/history?page=' . ($page - 1) : null, 'showNext' => $archives->totalCount() > $offset + 5 ? '/history?page=' . ($page + 1) : null]); |
113 | 88 | }); |
114 | 89 |
|
115 | | -$app->get('/download/:archiveId', function ($archiveId) use ($app) { |
| 90 | +$app->get('/download/:archiveId', function ($archiveId) use ($app): void { |
116 | 91 | $archive = $app->opentok->getArchive($archiveId); |
117 | 92 | $app->redirect($archive->url); |
118 | 93 | }); |
119 | 94 |
|
120 | | -$app->post('/start', function () use ($app, $sessionId) { |
| 95 | +$app->post('/start', function () use ($app, $sessionId): void { |
121 | 96 |
|
122 | | - $archive = $app->opentok->startArchive($sessionId, array( |
123 | | - 'name' => "PHP Archiving Sample App", |
124 | | - 'hasAudio' => ($app->request->post('hasAudio') == 'on'), |
125 | | - 'hasVideo' => ($app->request->post('hasVideo') == 'on'), |
126 | | - 'outputMode' => ($app->request->post('outputMode') == 'composed' ? OutputMode::COMPOSED : OutputMode::INDIVIDUAL) |
127 | | - )); |
| 97 | + $archive = $app->opentok->startArchive($sessionId, ['name' => "PHP Archiving Sample App", 'hasAudio' => ($app->request->post('hasAudio') == 'on'), 'hasVideo' => ($app->request->post('hasVideo') == 'on'), 'outputMode' => ($app->request->post('outputMode') == 'composed' ? OutputMode::COMPOSED : OutputMode::INDIVIDUAL)]); |
128 | 98 |
|
129 | 99 | $app->response->headers->set('Content-Type', 'application/json'); |
130 | 100 | echo $archive->toJson(); |
131 | 101 | }); |
132 | 102 |
|
133 | | -$app->get('/stop/:archiveId', function ($archiveId) use ($app) { |
| 103 | +$app->get('/stop/:archiveId', function ($archiveId) use ($app): void { |
134 | 104 | $archive = $app->opentok->stopArchive($archiveId); |
135 | 105 | $app->response->headers->set('Content-Type', 'application/json'); |
136 | 106 | echo $archive->toJson(); |
137 | 107 | }); |
138 | 108 |
|
139 | | -$app->get('/delete/:archiveId', function ($archiveId) use ($app) { |
| 109 | +$app->get('/delete/:archiveId', function ($archiveId) use ($app): void { |
140 | 110 | $app->opentok->deleteArchive($archiveId); |
141 | 111 | $app->redirect('/history'); |
142 | 112 | }); |
|
0 commit comments