@@ 21-219 (lines=199) @@ | ||
18 | * |
|
19 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
20 | */ |
|
21 | class SampleController implements ContainerInjectableInterface |
|
22 | { |
|
23 | use ContainerInjectableTrait; |
|
24 | ||
25 | ||
26 | ||
27 | /** |
|
28 | * @var string $db a sample member variable that gets initialised |
|
29 | */ |
|
30 | private $db = "not active"; |
|
31 | ||
32 | ||
33 | ||
34 | /** |
|
35 | * The initialize method is optional and will always be called before the |
|
36 | * target method/action. This is a convienient method where you could |
|
37 | * setup internal properties that are commonly used by several methods. |
|
38 | * |
|
39 | * @return void |
|
40 | */ |
|
41 | public function initialize() : void |
|
42 | { |
|
43 | // Use to initialise member variables. |
|
44 | $this->db = "active"; |
|
45 | } |
|
46 | ||
47 | ||
48 | ||
49 | /** |
|
50 | * This is the index method action, it handles: |
|
51 | * ANY METHOD mountpoint |
|
52 | * ANY METHOD mountpoint/ |
|
53 | * ANY METHOD mountpoint/index |
|
54 | * |
|
55 | * @return string |
|
56 | */ |
|
57 | public function indexAction() : string |
|
58 | { |
|
59 | // Deal with the action and return a response. |
|
60 | return __METHOD__ . ", \$db is {$this->db}"; |
|
61 | } |
|
62 | ||
63 | ||
64 | ||
65 | /** |
|
66 | * This sample method dumps the content of $di. |
|
67 | * GET mountpoint/dump-app |
|
68 | * |
|
69 | * @return string |
|
70 | */ |
|
71 | public function dumpDiActionGet() : string |
|
72 | { |
|
73 | // Deal with the action and return a response. |
|
74 | $services = implode(", ", $this->di->getServices()); |
|
75 | return __METHOD__ . "<p>\$di contains: $services"; |
|
76 | } |
|
77 | ||
78 | ||
79 | ||
80 | /** |
|
81 | * Add the request method to the method name to limit what request methods |
|
82 | * the handler supports. |
|
83 | * GET mountpoint/info |
|
84 | * |
|
85 | * @return string |
|
86 | */ |
|
87 | public function infoActionGet() : string |
|
88 | { |
|
89 | // Deal with the action and return a response. |
|
90 | return __METHOD__ . ", \$db is {$this->db}"; |
|
91 | } |
|
92 | ||
93 | ||
94 | ||
95 | /** |
|
96 | * This sample method action it the handler for route: |
|
97 | * GET mountpoint/create |
|
98 | * |
|
99 | * @return string |
|
100 | */ |
|
101 | public function createActionGet() : string |
|
102 | { |
|
103 | // Deal with the action and return a response. |
|
104 | return __METHOD__ . ", \$db is {$this->db}"; |
|
105 | } |
|
106 | ||
107 | ||
108 | ||
109 | /** |
|
110 | * This sample method action it the handler for route: |
|
111 | * POST mountpoint/create |
|
112 | * |
|
113 | * @return string |
|
114 | */ |
|
115 | public function createActionPost() : string |
|
116 | { |
|
117 | // Deal with the action and return a response. |
|
118 | return __METHOD__ . ", \$db is {$this->db}"; |
|
119 | } |
|
120 | ||
121 | ||
122 | ||
123 | /** |
|
124 | * This sample method action takes one argument: |
|
125 | * GET mountpoint/argument/<value> |
|
126 | * |
|
127 | * @param mixed $value |
|
128 | * |
|
129 | * @return string |
|
130 | */ |
|
131 | public function argumentActionGet($value) : string |
|
132 | { |
|
133 | // Deal with the action and return a response. |
|
134 | return __METHOD__ . ", \$db is {$this->db}, got argument '$value'"; |
|
135 | } |
|
136 | ||
137 | ||
138 | ||
139 | /** |
|
140 | * This sample method action takes zero or one argument and you can use - as a separator which will then be removed: |
|
141 | * GET mountpoint/defaultargument/ |
|
142 | * GET mountpoint/defaultargument/<value> |
|
143 | * GET mountpoint/default-argument/ |
|
144 | * GET mountpoint/default-argument/<value> |
|
145 | * |
|
146 | * @param mixed $value with a default string. |
|
147 | * |
|
148 | * @return string |
|
149 | */ |
|
150 | public function defaultArgumentActionGet($value = "default") : string |
|
151 | { |
|
152 | // Deal with the action and return a response. |
|
153 | return __METHOD__ . ", \$db is {$this->db}, got argument '$value'"; |
|
154 | } |
|
155 | ||
156 | ||
157 | ||
158 | /** |
|
159 | * This sample method action takes two typed arguments: |
|
160 | * GET mountpoint/typed-argument/<string>/<int> |
|
161 | * |
|
162 | * NOTE. Its recommended to not use int as type since it will still |
|
163 | * accept numbers such as 2hundred givving a PHP NOTICE. So, its better to |
|
164 | * deal with type check within the action method and throuw exceptions |
|
165 | * when the expected type is not met. |
|
166 | * |
|
167 | * @param mixed $value with a default string. |
|
168 | * |
|
169 | * @return string |
|
170 | */ |
|
171 | public function typedArgumentActionGet(string $str, int $int) : string |
|
172 | { |
|
173 | // Deal with the action and return a response. |
|
174 | return __METHOD__ . ", \$db is {$this->db}, got string argument '$str' and int argument '$int'."; |
|
175 | } |
|
176 | ||
177 | ||
178 | ||
179 | /** |
|
180 | * This sample method action takes a variadic list of arguments: |
|
181 | * GET mountpoint/variadic/ |
|
182 | * GET mountpoint/variadic/<value> |
|
183 | * GET mountpoint/variadic/<value>/<value> |
|
184 | * GET mountpoint/variadic/<value>/<value>/<value> |
|
185 | * etc. |
|
186 | * |
|
187 | * @param array $value as a variadic parameter. |
|
188 | * |
|
189 | * @return string |
|
190 | */ |
|
191 | public function variadicActionGet(...$value) : string |
|
192 | { |
|
193 | // Deal with the action and return a response. |
|
194 | return __METHOD__ . ", \$db is {$this->db}, got '" . count($value) . "' arguments: " . implode(", ", $value); |
|
195 | } |
|
196 | ||
197 | ||
198 | ||
199 | /** |
|
200 | * Adding an optional catchAll() method will catch all actions sent to the |
|
201 | * router. You can then reply with an actual response or return void to |
|
202 | * allow for the router to move on to next handler. |
|
203 | * A catchAll() handles the following, if a specific action method is not |
|
204 | * created: |
|
205 | * ANY METHOD mountpoint/** |
|
206 | * |
|
207 | * @param array $args as a variadic parameter. |
|
208 | * |
|
209 | * @return mixed |
|
210 | * |
|
211 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
212 | */ |
|
213 | public function catchAll(...$args) |
|
214 | { |
|
215 | // Deal with the request and send an actual response, or not. |
|
216 | //return __METHOD__ . ", \$db is {$this->db}, got '" . count($args) . "' arguments: " . implode(", ", $args); |
|
217 | return; |
|
218 | } |
|
219 | } |
|
220 |
@@ 21-221 (lines=201) @@ | ||
18 | * |
|
19 | * @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
20 | */ |
|
21 | class SampleAppController implements AppInjectableInterface |
|
22 | { |
|
23 | use AppInjectableTrait; |
|
24 | ||
25 | ||
26 | ||
27 | /** |
|
28 | * @var string $db a sample member variable that gets initialised |
|
29 | */ |
|
30 | private $db = "not active"; |
|
31 | ||
32 | ||
33 | ||
34 | /** |
|
35 | * The initialize method is optional and will always be called before the |
|
36 | * target method/action. This is a convienient method where you could |
|
37 | * setup internal properties that are commonly used by several methods. |
|
38 | * |
|
39 | * @return void |
|
40 | */ |
|
41 | public function initialize() : void |
|
42 | { |
|
43 | // Use to initialise member variables. |
|
44 | $this->db = "active"; |
|
45 | ||
46 | // Use $this->app to access the framework services. |
|
47 | } |
|
48 | ||
49 | ||
50 | ||
51 | /** |
|
52 | * This is the index method action, it handles: |
|
53 | * ANY METHOD mountpoint |
|
54 | * ANY METHOD mountpoint/ |
|
55 | * ANY METHOD mountpoint/index |
|
56 | * |
|
57 | * @return string |
|
58 | */ |
|
59 | public function indexAction() : string |
|
60 | { |
|
61 | // Deal with the action and return a response. |
|
62 | return __METHOD__ . ", \$db is {$this->db}"; |
|
63 | } |
|
64 | ||
65 | ||
66 | ||
67 | /** |
|
68 | * This sample method dumps the content of $app. |
|
69 | * GET mountpoint/dump-app |
|
70 | * |
|
71 | * @return string |
|
72 | */ |
|
73 | public function dumpAppActionGet() : string |
|
74 | { |
|
75 | // Deal with the action and return a response. |
|
76 | $services = implode(", ", $this->app->getServices()); |
|
77 | return __METHOD__ . "<p>\$app contains: $services"; |
|
78 | } |
|
79 | ||
80 | ||
81 | ||
82 | /** |
|
83 | * Add the request method to the method name to limit what request methods |
|
84 | * the handler supports. |
|
85 | * GET mountpoint/info |
|
86 | * |
|
87 | * @return string |
|
88 | */ |
|
89 | public function infoActionGet() : string |
|
90 | { |
|
91 | // Deal with the action and return a response. |
|
92 | return __METHOD__ . ", \$db is {$this->db}"; |
|
93 | } |
|
94 | ||
95 | ||
96 | ||
97 | /** |
|
98 | * This sample method action it the handler for route: |
|
99 | * GET mountpoint/create |
|
100 | * |
|
101 | * @return string |
|
102 | */ |
|
103 | public function createActionGet() : string |
|
104 | { |
|
105 | // Deal with the action and return a response. |
|
106 | return __METHOD__ . ", \$db is {$this->db}"; |
|
107 | } |
|
108 | ||
109 | ||
110 | ||
111 | /** |
|
112 | * This sample method action it the handler for route: |
|
113 | * POST mountpoint/create |
|
114 | * |
|
115 | * @return string |
|
116 | */ |
|
117 | public function createActionPost() : string |
|
118 | { |
|
119 | // Deal with the action and return a response. |
|
120 | return __METHOD__ . ", \$db is {$this->db}"; |
|
121 | } |
|
122 | ||
123 | ||
124 | ||
125 | /** |
|
126 | * This sample method action takes one argument: |
|
127 | * GET mountpoint/argument/<value> |
|
128 | * |
|
129 | * @param mixed $value |
|
130 | * |
|
131 | * @return string |
|
132 | */ |
|
133 | public function argumentActionGet($value) : string |
|
134 | { |
|
135 | // Deal with the action and return a response. |
|
136 | return __METHOD__ . ", \$db is {$this->db}, got argument '$value'"; |
|
137 | } |
|
138 | ||
139 | ||
140 | ||
141 | /** |
|
142 | * This sample method action takes zero or one argument and you can use - as a separator which will then be removed: |
|
143 | * GET mountpoint/defaultargument/ |
|
144 | * GET mountpoint/defaultargument/<value> |
|
145 | * GET mountpoint/default-argument/ |
|
146 | * GET mountpoint/default-argument/<value> |
|
147 | * |
|
148 | * @param mixed $value with a default string. |
|
149 | * |
|
150 | * @return string |
|
151 | */ |
|
152 | public function defaultArgumentActionGet($value = "default") : string |
|
153 | { |
|
154 | // Deal with the action and return a response. |
|
155 | return __METHOD__ . ", \$db is {$this->db}, got argument '$value'"; |
|
156 | } |
|
157 | ||
158 | ||
159 | ||
160 | /** |
|
161 | * This sample method action takes two typed arguments: |
|
162 | * GET mountpoint/typed-argument/<string>/<int> |
|
163 | * |
|
164 | * NOTE. Its recommended to not use int as type since it will still |
|
165 | * accept numbers such as 2hundred givving a PHP NOTICE. So, its better to |
|
166 | * deal with type check within the action method and throuw exceptions |
|
167 | * when the expected type is not met. |
|
168 | * |
|
169 | * @param mixed $value with a default string. |
|
170 | * |
|
171 | * @return string |
|
172 | */ |
|
173 | public function typedArgumentActionGet(string $str, int $int) : string |
|
174 | { |
|
175 | // Deal with the action and return a response. |
|
176 | return __METHOD__ . ", \$db is {$this->db}, got string argument '$str' and int argument '$int'."; |
|
177 | } |
|
178 | ||
179 | ||
180 | ||
181 | /** |
|
182 | * This sample method action takes a variadic list of arguments: |
|
183 | * GET mountpoint/variadic/ |
|
184 | * GET mountpoint/variadic/<value> |
|
185 | * GET mountpoint/variadic/<value>/<value> |
|
186 | * GET mountpoint/variadic/<value>/<value>/<value> |
|
187 | * etc. |
|
188 | * |
|
189 | * @param array $value as a variadic parameter. |
|
190 | * |
|
191 | * @return string |
|
192 | */ |
|
193 | public function variadicActionGet(...$value) : string |
|
194 | { |
|
195 | // Deal with the action and return a response. |
|
196 | return __METHOD__ . ", \$db is {$this->db}, got '" . count($value) . "' arguments: " . implode(", ", $value); |
|
197 | } |
|
198 | ||
199 | ||
200 | ||
201 | /** |
|
202 | * Adding an optional catchAll() method will catch all actions sent to the |
|
203 | * router. You can then reply with an actual response or return void to |
|
204 | * allow for the router to move on to next handler. |
|
205 | * A catchAll() handles the following, if a specific action method is not |
|
206 | * created: |
|
207 | * ANY METHOD mountpoint/** |
|
208 | * |
|
209 | * @param array $args as a variadic parameter. |
|
210 | * |
|
211 | * @return mixed |
|
212 | * |
|
213 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
214 | */ |
|
215 | public function catchAll(...$args) |
|
216 | { |
|
217 | // Deal with the request and send an actual response, or not. |
|
218 | //return __METHOD__ . ", \$db is {$this->db}, got '" . count($args) . "' arguments: " . implode(", ", $args); |
|
219 | return; |
|
220 | } |
|
221 | } |
|
222 |