/
ServiceProvider.php
445 lines (391 loc) · 11.4 KB
/
ServiceProvider.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
/**
* Klein (klein.php) - A fast & flexible router for PHP
*
* @author Chris O'Hara <cohara87@gmail.com>
* @author Trevor Suarez (Rican7) (contributor and v2 refactorer)
* @copyright (c) Chris O'Hara
* @link https://github.com/klein/klein.php
* @license MIT
*/
namespace Klein;
use Klein\DataCollection\DataCollection;
/**
* ServiceProvider
*
* Service provider class for handling logic extending between
* a request's data and a response's behavior
*/
class ServiceProvider
{
/**
* Class properties
*/
/**
* The Request instance containing HTTP request data and behaviors
*
* @type Request
*/
protected $request;
/**
* The Response instance containing HTTP response data and behaviors
*
* @type AbstractResponse
*/
protected $response;
/**
* The id of the current PHP session
*
* @type string|boolean
*/
protected $session_id;
/**
* The view layout
*
* @type string
*/
protected $layout;
/**
* The view to render
*
* @type string
*/
protected $view;
/**
* Shared data collection
*
* @type DataCollection
*/
protected $shared_data;
/**
* Methods
*/
/**
* Constructor
*
* @param Request $request Object containing all HTTP request data and behaviors
* @param AbstractResponse $response Object containing all HTTP response data and behaviors
*/
public function __construct(Request $request = null, AbstractResponse $response = null)
{
// Bind our objects
$this->bind($request, $response);
// Instantiate our shared data collection
$this->shared_data = new DataCollection();
}
/**
* Bind object instances to this service
*
* @param Request $request Object containing all HTTP request data and behaviors
* @param AbstractResponse $response Object containing all HTTP response data and behaviors
* @return ServiceProvider
*/
public function bind(Request $request = null, AbstractResponse $response = null)
{
// Keep references
$this->request = $request ?: $this->request;
$this->response = $response ?: $this->response;
return $this;
}
/**
* Returns the shared data collection object
*
* @return \Klein\DataCollection\DataCollection
*/
public function sharedData()
{
return $this->shared_data;
}
/**
* Get the current session's ID
*
* This will start a session if the current session id is null
*
* @return string|false
*/
public function startSession()
{
if (session_id() === '') {
// Attempt to start a session
session_start();
$this->session_id = session_id() ?: false;
}
return $this->session_id;
}
/**
* Stores a flash message of $type
*
* @param string $msg The message to flash
* @param string $type The flash message type
* @param array $params Optional params to be parsed by markdown
* @return void
*/
public function flash($msg, $type = 'info', $params = null)
{
$this->startSession();
if (is_array($type)) {
$params = $type;
$type = 'info';
}
if (!isset($_SESSION['__flashes'])) {
$_SESSION['__flashes'] = array($type => array());
} elseif (!isset($_SESSION['__flashes'][$type])) {
$_SESSION['__flashes'][$type] = array();
}
$_SESSION['__flashes'][$type][] = $this->markdown($msg, $params);
}
/**
* Returns and clears all flashes of optional $type
*
* @param string $type The name of the flash message type
* @return array
*/
public function flashes($type = null)
{
$this->startSession();
if (!isset($_SESSION['__flashes'])) {
return array();
}
if (null === $type) {
$flashes = $_SESSION['__flashes'];
unset($_SESSION['__flashes']);
} else {
$flashes = array();
if (isset($_SESSION['__flashes'][$type])) {
$flashes = $_SESSION['__flashes'][$type];
unset($_SESSION['__flashes'][$type]);
}
}
return $flashes;
}
/**
* Render a text string as markdown
*
* Supports basic markdown syntax
*
* Also, this method takes in EITHER an array of optional arguments (as the second parameter)
* ... OR this method will simply take a variable number of arguments (after the initial str arg)
*
* @param string $str The text string to parse
* @param array $args Optional arguments to be parsed by markdown
* @return string
*/
public static function markdown($str, $args = null)
{
// Create our markdown parse/conversion regex's
$md = array(
'/\[([^\]]++)\]\(([^\)]++)\)/' => '<a href="$2">$1</a>',
'/\*\*([^\*]++)\*\*/' => '<strong>$1</strong>',
'/\*([^\*]++)\*/' => '<em>$1</em>'
);
// Let's make our arguments more "magical"
$args = func_get_args(); // Grab all of our passed args
$str = array_shift($args); // Remove the initial arg from the array (and set the $str to it)
if (isset($args[0]) && is_array($args[0])) {
/**
* If our "second" argument (now the first array item is an array)
* just use the array as the arguments and forget the rest
*/
$args = $args[0];
}
// Encode our args so we can insert them into an HTML string
foreach ($args as &$arg) {
$arg = htmlentities($arg, ENT_QUOTES, 'UTF-8');
}
// Actually do our markdown conversion
return vsprintf(preg_replace(array_keys($md), $md, $str), $args);
}
/**
* Escapes a string for UTF-8 HTML displaying
*
* This is a quick macro for escaping strings designed
* to be shown in a UTF-8 HTML environment. Its options
* are otherwise limited by design
*
* @param string $str The string to escape
* @param int $flags A bitmask of `htmlentities()` compatible flags
* @return string
*/
public static function escape($str, $flags = ENT_QUOTES)
{
return htmlentities($str, $flags, 'UTF-8');
}
/**
* Redirects the request to the current URL
*
* @return ServiceProvider
*/
public function refresh()
{
$this->response->redirect(
$this->request->uri()
);
return $this;
}
/**
* Redirects the request back to the referrer
*
* @return ServiceProvider
*/
public function back()
{
$referer = $this->request->server()->get('HTTP_REFERER');
if (null !== $referer) {
$this->response->redirect($referer);
} else {
$this->refresh();
}
return $this;
}
/**
* Get (or set) the view's layout
*
* Simply calling this method without any arguments returns the current layout.
* Calling with an argument, however, sets the layout to what was provided by the argument.
*
* @param string $layout The layout of the view
* @return string|ServiceProvider
*/
public function layout($layout = null)
{
if (null !== $layout) {
$this->layout = $layout;
return $this;
}
return $this->layout;
}
/**
* Renders the current view
*
* @return void
*/
public function yieldView()
{
require $this->view;
}
/**
* Renders a view + optional layout
*
* @param string $view The view to render
* @param array $data The data to render in the view
* @return void
*/
public function render($view, array $data = array())
{
$original_view = $this->view;
if (!empty($data)) {
$this->shared_data->merge($data);
}
$this->view = $view;
if (null === $this->layout) {
$this->yieldView();
} else {
require $this->layout;
}
if (false !== $this->response->chunked) {
$this->response->chunk();
}
// restore state for parent render()
$this->view = $original_view;
}
/**
* Renders a view without a layout
*
* @param string $view The view to render
* @param array $data The data to render in the view
* @return void
*/
public function partial($view, array $data = array())
{
$layout = $this->layout;
$this->layout = null;
$this->render($view, $data);
$this->layout = $layout;
}
/**
* Add a custom validator for our validation method
*
* @param string $method The name of the validator method
* @param callable $callback The callback to perform on validation
* @return void
*/
public function addValidator($method, $callback)
{
Validator::addValidator($method, $callback);
}
/**
* Start a validator chain for the specified string
*
* @param string $string The string to validate
* @param string $err The custom exception message to throw
* @return Validator
*/
public function validate($string, $err = null)
{
return new Validator($string, $err);
}
/**
* Start a validator chain for the specified parameter
*
* @param string $param The name of the parameter to validate
* @param string $err The custom exception message to throw
* @return Validator
*/
public function validateParam($param, $err = null)
{
return $this->validate($this->request->param($param), $err);
}
/**
* Magic "__isset" method
*
* Allows the ability to arbitrarily check the existence of shared data
* from this instance while treating it as an instance property
*
* @param string $key The name of the shared data
* @return boolean
*/
public function __isset($key)
{
return $this->shared_data->exists($key);
}
/**
* Magic "__get" method
*
* Allows the ability to arbitrarily request shared data from this instance
* while treating it as an instance property
*
* @param string $key The name of the shared data
* @return string
*/
public function __get($key)
{
return $this->shared_data->get($key);
}
/**
* Magic "__set" method
*
* Allows the ability to arbitrarily set shared data from this instance
* while treating it as an instance property
*
* @param string $key The name of the shared data
* @param mixed $value The value of the shared data
* @return void
*/
public function __set($key, $value)
{
$this->shared_data->set($key, $value);
}
/**
* Magic "__unset" method
*
* Allows the ability to arbitrarily remove shared data from this instance
* while treating it as an instance property
*
* @param string $key The name of the shared data
* @return void
*/
public function __unset($key)
{
$this->shared_data->remove($key);
}
}