-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.php
162 lines (111 loc) · 3.74 KB
/
routes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
* /routes/routes.php
*
* This file is responsible for defining the routes and their action. Each method
* defines an endpoint. Only the method with the same name as the request's
* endpoint is called. If any value is returned, it is sent to the client as
* JSON. Methods should choose the appropriated controller action based ont he
* request's parameters.
*
*/
declare(strict_types=1);
require_once __DIR__ . "/router.php";
require_once __DIR__ . "/../controllers/session_controller.php";
require_once __DIR__ . "/../controllers/broadcasts_controller.php";
require_once __DIR__ . "/../controllers/users_controller.php";
require_once __DIR__ . "/../controllers/messages_controller.php";
class Routes extends Router {
protected $reqParams;
function __construct($request) {
parent::__construct($request);
$this->reqParams = [
"method" => $this->method,
"endpoint" => $this->endpoint,
"verb" => $this->verb,
"args" => $this->args,
"request" => $this->request
];
}
protected function root() {
require_once __DIR__ . "/../public/index.html";
}
protected function session() {
$session_controller = new SessionController();
switch ($this->verb) {
case "new":
if ($this->method === "GET") {
$session_controller->newSession($this->reqParams);
return;
}
break;
case "delete":
if ($this->method === "GET") {
$session_controller->destroy($this->reqParams);
return;
}
break;
case "check":
if ($this->method === "GET") {
return $session_controller->check();
}
break;
}
return ["error" => "invalid request"];
}
protected function broadcasts() {
$broadcasts_controller = new BroadcastsController();
switch ($this->verb) {
case "streams":
if ($this->method === "GET") {
return $broadcasts_controller->streams();
}
break;
case "show":
if ($this->method === "GET") {
return $broadcasts_controller->show($this->reqParams);
}
break;
case "messages":
switch ($this->method) {
case "GET":
return $broadcasts_controller->messages($this->reqParams);
case "POST":
return $broadcasts_controller->newMessage($this->reqParams);
}
break;
default:
switch ($this->method) {
case "GET":
return $broadcasts_controller->index();
case "POST":
return $broadcasts_controller->create($this->reqParams);
}
break;
}
return ["error" => "invalid request"];
}
protected function users() {
$user_controller = new UserController();
switch ($this->verb) {
case "messages":
if ($this->method === "GET") {
return $user_controller->messages($this->reqParams);
}
break;
case "all_with_messages":
if ($this->method === "GET") {
return $user_controller->allWithMessages($this->reqParams);
}
break;
}
return ["error" => "invalid request"];
}
protected function messages() {
$messages_controller = new MessagesController();
if ($this->verb === "report" && $this->method === "GET") {
return $messages_controller->report($this->reqParams);
}
return ["error" => "invalid request"];
}
}