1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vectorface\SnappyRouter\Handler; |
4
|
|
|
|
5
|
|
|
use \Exception; |
6
|
|
|
use Vectorface\SnappyRouter\Exception\RouterExceptionInterface; |
7
|
|
|
use Vectorface\SnappyRouter\Response\AbstractResponse; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The base class for all handlers that implement a request/response style of |
11
|
|
|
* invokation. |
12
|
|
|
* @copyright Copyright (c) 2014, VectorFace, Inc. |
13
|
|
|
* @author Dan Bruce <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractRequestHandler extends AbstractHandler |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Returns true if the handler determines it should handle this request and false otherwise. |
19
|
|
|
* @param string $path The URL path for the request. |
20
|
|
|
* @param array $query The query parameters. |
21
|
|
|
* @param array $post The post data. |
22
|
|
|
* @param string $verb The HTTP verb used in the request. |
23
|
|
|
* @return boolean Returns true if this handler will handle the request and false otherwise. |
24
|
|
|
*/ |
25
|
|
|
abstract public function isAppropriate($path, $query, $post, $verb); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns a request object extracted from the request details (path, query, etc). The method |
29
|
|
|
* isAppropriate() must have returned true, otherwise this method should return null. |
30
|
|
|
* @return \Vectorface\SnappyRouter\Request\HttpRequest|null Returns a |
31
|
|
|
* Request object or null if this handler is not appropriate. |
32
|
|
|
*/ |
33
|
|
|
abstract public function getRequest(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Provides the handler with an opportunity to perform any last minute |
37
|
|
|
* error handling logic. The returned value will be serialized by the |
38
|
|
|
* handler's encoder. |
39
|
|
|
* @param Exception $e The exception that was thrown. |
40
|
|
|
* @return mixed Returns a serializable value that will be encoded and returned |
41
|
|
|
* to the client. |
42
|
|
|
*/ |
43
|
1 |
|
public function handleException(Exception $e) |
44
|
|
|
{ |
45
|
1 |
|
$responseCode = AbstractResponse::RESPONSE_SERVER_ERROR; |
46
|
1 |
|
if ($e instanceof RouterExceptionInterface) { |
47
|
1 |
|
$responseCode = $e->getAssociatedStatusCode(); |
48
|
1 |
|
} |
49
|
1 |
|
\Vectorface\SnappyRouter\http_response_code($responseCode); |
50
|
1 |
|
return parent::handleException($e); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns whether a handler should function in a CLI environment. |
55
|
|
|
* @return bool Returns true if the handler should function in a CLI |
56
|
|
|
* environment and false otherwise. |
57
|
|
|
*/ |
58
|
6 |
|
public function isCliHandler() |
59
|
|
|
{ |
60
|
6 |
|
return false; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|