| 199 |
lars |
1 |
<?php namespace Clockwork\DataSource;
|
|
|
2 |
|
|
|
3 |
use Clockwork\DataSource\DataSource;
|
|
|
4 |
use Clockwork\Request\Request;
|
|
|
5 |
|
|
|
6 |
use Slim\Slim;
|
|
|
7 |
|
|
|
8 |
// Data source for Slim 2 framework, provides controller, request and response information
|
|
|
9 |
class SlimDataSource extends DataSource
|
|
|
10 |
{
|
|
|
11 |
// Slim instance
|
|
|
12 |
protected $slim;
|
|
|
13 |
|
|
|
14 |
// Create a new data source, takes Slim instance as an argument
|
|
|
15 |
public function __construct(Slim $slim)
|
|
|
16 |
{
|
|
|
17 |
$this->slim = $slim;
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
// Adds request and response information to the request
|
|
|
21 |
public function resolve(Request $request)
|
|
|
22 |
{
|
|
|
23 |
$request->method = $this->getRequestMethod();
|
|
|
24 |
$request->uri = $this->getRequestUri();
|
|
|
25 |
$request->controller = $this->getController();
|
|
|
26 |
$request->headers = $this->getRequestHeaders();
|
|
|
27 |
$request->responseStatus = $this->getResponseStatus();
|
|
|
28 |
|
|
|
29 |
return $request;
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
// Get a textual representation of current route's controller
|
|
|
33 |
protected function getController()
|
|
|
34 |
{
|
|
|
35 |
$matchedRoutes = $this->slim->router()->getMatchedRoutes(
|
|
|
36 |
$this->slim->request()->getMethod(), $this->slim->request()->getResourceUri()
|
|
|
37 |
);
|
|
|
38 |
|
|
|
39 |
if (! count($matchedRoutes)) return;
|
|
|
40 |
|
|
|
41 |
$controller = end($matchedRoutes)->getCallable();
|
|
|
42 |
|
|
|
43 |
if ($controller instanceof \Closure) {
|
|
|
44 |
$controller = 'anonymous function';
|
|
|
45 |
} elseif (is_object($controller)) {
|
|
|
46 |
$controller = 'instance of ' . get_class($controller);
|
|
|
47 |
} elseif (is_array($controller) && count($controller) == 2) {
|
|
|
48 |
if (is_object($controller[0])) {
|
|
|
49 |
$controller = get_class($controller[0]) . '->' . $controller[1];
|
|
|
50 |
} else {
|
|
|
51 |
$controller = $controller[0] . '::' . $controller[1];
|
|
|
52 |
}
|
|
|
53 |
} elseif (! is_string($controller)) {
|
|
|
54 |
$controller = null;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
return $controller;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
// Get the request headers
|
|
|
61 |
protected function getRequestHeaders()
|
|
|
62 |
{
|
|
|
63 |
$headers = [];
|
|
|
64 |
|
|
|
65 |
foreach ($_SERVER as $key => $value) {
|
|
|
66 |
if (substr($key, 0, 5) !== 'HTTP_') continue;
|
|
|
67 |
|
|
|
68 |
$header = substr($key, 5);
|
|
|
69 |
$header = str_replace('_', ' ', $header);
|
|
|
70 |
$header = ucwords(strtolower($header));
|
|
|
71 |
$header = str_replace(' ', '-', $header);
|
|
|
72 |
|
|
|
73 |
$value = $this->slim->request()->headers($header, $value);
|
|
|
74 |
|
|
|
75 |
if (! isset($headers[$header])) {
|
|
|
76 |
$headers[$header] = [ $value ];
|
|
|
77 |
} else {
|
|
|
78 |
$headers[$header][] = $value;
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
ksort($headers);
|
|
|
83 |
|
|
|
84 |
return $headers;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
// Get the request method
|
|
|
88 |
protected function getRequestMethod()
|
|
|
89 |
{
|
|
|
90 |
return $this->slim->request()->getMethod();
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
// Get the request URI
|
|
|
94 |
protected function getRequestUri()
|
|
|
95 |
{
|
|
|
96 |
return $this->slim->request()->getPathInfo();
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
// Get the response status code
|
|
|
100 |
protected function getResponseStatus()
|
|
|
101 |
{
|
|
|
102 |
return $this->slim->response()->status();
|
|
|
103 |
}
|
|
|
104 |
}
|