1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Modlr\Rest; |
4
|
|
|
|
5
|
|
|
use As3\Modlr\Api\AdapterInterface; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* REST Kernel. |
11
|
|
|
* Handles incoming Requests, converts them to REST request format, and handles them via the Adapter. |
12
|
|
|
* |
13
|
|
|
* @author Jacob Bare <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class RestKernel |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var AdapterInterface |
19
|
|
|
*/ |
20
|
|
|
private $adapter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var RestConfiguration |
24
|
|
|
*/ |
25
|
|
|
private $config; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $debug = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor. |
34
|
|
|
* |
35
|
|
|
* @param AdapterInterface $adapter |
36
|
|
|
* @param RestConfiguration $config |
37
|
|
|
*/ |
38
|
|
|
public function __construct(AdapterInterface $adapter, RestConfiguration $config) |
39
|
|
|
{ |
40
|
|
|
$this->adapter = $adapter; |
41
|
|
|
$this->config = $config; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Enables/disables debug. |
46
|
|
|
* |
47
|
|
|
* @param bool $debug |
48
|
|
|
* @return self |
49
|
|
|
*/ |
50
|
|
|
public function enableDebug($debug = true) |
51
|
|
|
{ |
52
|
|
|
$this->debug = (bool) $debug; |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Processes an incoming Request object, routes it to the adapter, and returns a response. |
58
|
|
|
* |
59
|
|
|
* @param Request $request |
60
|
|
|
* @return Response $response |
61
|
|
|
*/ |
62
|
|
|
public function handle(Request $request) |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
|
|
$restRequest = new RestRequest($this->config, $request->getMethod(), $request->getUri(), $request->getContent()); |
|
|
|
|
66
|
|
|
$restResponse = $this->adapter->processRequest($restRequest); |
67
|
|
|
} catch (\Exception $e) { |
68
|
|
|
if (true === $this->debugEnabled()) { |
69
|
|
|
throw $e; |
70
|
|
|
} |
71
|
|
|
$restResponse = $this->adapter->handleException($e); |
72
|
|
|
} |
73
|
|
|
return new Response($restResponse->getContent(), $restResponse->getStatus(), $restResponse->getHeaders()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Whether debug is enabled. |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function debugEnabled() |
82
|
|
|
{ |
83
|
|
|
return $this->debug; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.