1 | <?php |
||
22 | class ServiceProvider |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * Class properties |
||
27 | */ |
||
28 | |||
29 | /** |
||
30 | * The Request instance containing HTTP request data and behaviors |
||
31 | * |
||
32 | * @type Request |
||
33 | */ |
||
34 | protected $request; |
||
35 | |||
36 | /** |
||
37 | * The Response instance containing HTTP response data and behaviors |
||
38 | * |
||
39 | * @type AbstractResponse |
||
40 | */ |
||
41 | protected $response; |
||
42 | |||
43 | /** |
||
44 | * The id of the current PHP session |
||
45 | * |
||
46 | * @type string|boolean |
||
47 | */ |
||
48 | protected $session_id; |
||
49 | |||
50 | /** |
||
51 | * The view layout |
||
52 | * |
||
53 | * @type string |
||
54 | */ |
||
55 | protected $layout; |
||
56 | |||
57 | /** |
||
58 | * The view to render |
||
59 | * |
||
60 | * @type string |
||
61 | */ |
||
62 | protected $view; |
||
63 | |||
64 | /** |
||
65 | * Shared data collection |
||
66 | * |
||
67 | * @type DataCollection |
||
68 | */ |
||
69 | protected $shared_data; |
||
70 | |||
71 | |||
72 | /** |
||
73 | * Methods |
||
74 | */ |
||
75 | |||
76 | /** |
||
77 | * Constructor |
||
78 | * |
||
79 | * @param Request $request Object containing all HTTP request data and behaviors |
||
80 | * @param AbstractResponse $response Object containing all HTTP response data and behaviors |
||
81 | */ |
||
82 | public function __construct(Request $request = null, AbstractResponse $response = null) |
||
90 | |||
91 | /** |
||
92 | * Bind object instances to this service |
||
93 | * |
||
94 | * @param Request $request Object containing all HTTP request data and behaviors |
||
95 | * @param AbstractResponse $response Object containing all HTTP response data and behaviors |
||
96 | * @return ServiceProvider |
||
97 | */ |
||
98 | public function bind(Request $request = null, AbstractResponse $response = null) |
||
106 | |||
107 | /** |
||
108 | * Returns the shared data collection object |
||
109 | * |
||
110 | * @return \Klein\DataCollection\DataCollection |
||
111 | */ |
||
112 | public function sharedData() |
||
116 | |||
117 | /** |
||
118 | * Get the current session's ID |
||
119 | * |
||
120 | * This will start a session if the current session id is null |
||
121 | * |
||
122 | * @return string|false |
||
123 | */ |
||
124 | public function startSession() |
||
135 | |||
136 | /** |
||
137 | * Stores a flash message of $type |
||
138 | * |
||
139 | * @param string $msg The message to flash |
||
140 | * @param string $type The flash message type |
||
141 | * @param array $params Optional params to be parsed by markdown |
||
142 | * @return void |
||
143 | */ |
||
144 | public function flash($msg, $type = 'info', $params = null) |
||
158 | |||
159 | /** |
||
160 | * Returns and clears all flashes of optional $type |
||
161 | * |
||
162 | * @param string $type The name of the flash message type |
||
163 | * @return array |
||
164 | */ |
||
165 | public function flashes($type = null) |
||
186 | |||
187 | /** |
||
188 | * Render a text string as markdown |
||
189 | * |
||
190 | * Supports basic markdown syntax |
||
191 | * |
||
192 | * Also, this method takes in EITHER an array of optional arguments (as the second parameter) |
||
193 | * ... OR this method will simply take a variable number of arguments (after the initial str arg) |
||
194 | * |
||
195 | * @param string $str The text string to parse |
||
196 | * @param array $args Optional arguments to be parsed by markdown |
||
197 | * @return string |
||
198 | */ |
||
199 | public static function markdown($str, $args = null) |
||
227 | |||
228 | /** |
||
229 | * Escapes a string for UTF-8 HTML displaying |
||
230 | * |
||
231 | * This is a quick macro for escaping strings designed |
||
232 | * to be shown in a UTF-8 HTML environment. Its options |
||
233 | * are otherwise limited by design |
||
234 | * |
||
235 | * @param string $str The string to escape |
||
236 | * @param int $flags A bitmask of `htmlentities()` compatible flags |
||
237 | * @return string |
||
238 | */ |
||
239 | public static function escape($str, $flags = ENT_QUOTES) |
||
243 | |||
244 | /** |
||
245 | * Redirects the request to the current URL |
||
246 | * |
||
247 | * @return ServiceProvider |
||
248 | */ |
||
249 | public function refresh() |
||
257 | |||
258 | /** |
||
259 | * Redirects the request back to the referrer |
||
260 | * |
||
261 | * @return ServiceProvider |
||
262 | */ |
||
263 | public function back() |
||
275 | |||
276 | /** |
||
277 | * Get (or set) the view's layout |
||
278 | * |
||
279 | * Simply calling this method without any arguments returns the current layout. |
||
280 | * Calling with an argument, however, sets the layout to what was provided by the argument. |
||
281 | * |
||
282 | * @param string $layout The layout of the view |
||
283 | * @return string|ServiceProvider |
||
284 | */ |
||
285 | public function layout($layout = null) |
||
295 | |||
296 | /** |
||
297 | * Renders the current view |
||
298 | * |
||
299 | * @return void |
||
300 | */ |
||
301 | public function yieldView() |
||
305 | |||
306 | /** |
||
307 | * Renders a view + optional layout |
||
308 | * |
||
309 | * @param string $view The view to render |
||
310 | * @param array $data The data to render in the view |
||
311 | * @return void |
||
312 | */ |
||
313 | public function render($view, array $data = array()) |
||
336 | |||
337 | /** |
||
338 | * Renders a view without a layout |
||
339 | * |
||
340 | * @param string $view The view to render |
||
341 | * @param array $data The data to render in the view |
||
342 | * @return void |
||
343 | */ |
||
344 | public function partial($view, array $data = array()) |
||
351 | |||
352 | /** |
||
353 | * Add a custom validator for our validation method |
||
354 | * |
||
355 | * @param string $method The name of the validator method |
||
356 | * @param callable $callback The callback to perform on validation |
||
357 | * @return void |
||
358 | */ |
||
359 | public function addValidator($method, $callback) |
||
363 | |||
364 | /** |
||
365 | * Start a validator chain for the specified string |
||
366 | * |
||
367 | * @param string $string The string to validate |
||
368 | * @param string $err The custom exception message to throw |
||
369 | * @return Validator |
||
370 | */ |
||
371 | public function validate($string, $err = null) |
||
375 | |||
376 | /** |
||
377 | * Start a validator chain for the specified parameter |
||
378 | * |
||
379 | * @param string $param The name of the parameter to validate |
||
380 | * @param string $err The custom exception message to throw |
||
381 | * @return Validator |
||
382 | */ |
||
383 | public function validateParam($param, $err = null) |
||
387 | |||
388 | |||
389 | /** |
||
390 | * Magic "__isset" method |
||
391 | * |
||
392 | * Allows the ability to arbitrarily check the existence of shared data |
||
393 | * from this instance while treating it as an instance property |
||
394 | * |
||
395 | * @param string $key The name of the shared data |
||
396 | * @return boolean |
||
397 | */ |
||
398 | public function __isset($key) |
||
402 | |||
403 | /** |
||
404 | * Magic "__get" method |
||
405 | * |
||
406 | * Allows the ability to arbitrarily request shared data from this instance |
||
407 | * while treating it as an instance property |
||
408 | * |
||
409 | * @param string $key The name of the shared data |
||
410 | * @return string |
||
411 | */ |
||
412 | public function __get($key) |
||
416 | |||
417 | /** |
||
418 | * Magic "__set" method |
||
419 | * |
||
420 | * Allows the ability to arbitrarily set shared data from this instance |
||
421 | * while treating it as an instance property |
||
422 | * |
||
423 | * @param string $key The name of the shared data |
||
424 | * @param mixed $value The value of the shared data |
||
425 | * @return void |
||
426 | */ |
||
427 | public function __set($key, $value) |
||
431 | |||
432 | /** |
||
433 | * Magic "__unset" method |
||
434 | * |
||
435 | * Allows the ability to arbitrarily remove shared data from this instance |
||
436 | * while treating it as an instance property |
||
437 | * |
||
438 | * @param string $key The name of the shared data |
||
439 | * @return void |
||
440 | */ |
||
441 | public function __unset($key) |
||
445 | } |
||
446 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: