1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ****************************************************************** |
4
|
|
|
* Created by Marko Kungla on 09 Oct 2016 |
5
|
|
|
* @package toolshedr |
6
|
|
|
* Encoding UTF-8 |
7
|
|
|
* File Response.php |
8
|
|
|
* Code format PSR-2 and 12 |
9
|
|
|
* *******************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Toolshedr\Core; |
12
|
|
|
|
13
|
|
|
use \Toolshedr\Interfaces\ResponseDataInterface; |
14
|
|
|
use \Toolshedr\Core\{ |
15
|
|
|
Headers, |
|
|
|
|
16
|
|
|
Request |
|
|
|
|
17
|
|
|
}; |
18
|
|
|
|
19
|
|
|
class Response |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int Response status code |
23
|
|
|
*/ |
24
|
|
|
private $code; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string status message |
28
|
|
|
*/ |
29
|
|
|
private $message; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ResponseDataInterface data to be returned |
33
|
|
|
*/ |
34
|
|
|
private $data; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Response constructor. |
38
|
|
|
*/ |
39
|
4 |
|
public function __construct() |
40
|
|
|
{ |
41
|
4 |
|
$this->code = 200; |
|
|
|
|
42
|
4 |
|
$this->message = "OK"; |
|
|
|
|
43
|
4 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set status code |
47
|
|
|
* |
48
|
|
|
* @param int $code |
49
|
|
|
*/ |
50
|
|
|
public function setCode(int $code) |
51
|
|
|
{ |
52
|
|
|
$this->code = $code; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set response message |
57
|
|
|
* |
58
|
|
|
* @param string $message |
59
|
|
|
*/ |
60
|
|
|
public function setMessage(string $message) |
61
|
|
|
{ |
62
|
|
|
$this->message = $message; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set response data object |
67
|
|
|
* |
68
|
|
|
* @param ResponseDataInterface $data |
69
|
|
|
*/ |
70
|
|
|
public function setData(ResponseDataInterface $data) |
71
|
|
|
{ |
72
|
|
|
$this->data = $data; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Handle Options request |
77
|
|
|
* |
78
|
|
|
* @param Headers $headers |
79
|
|
|
*/ |
80
|
|
|
public function options(Headers $headers) |
81
|
|
|
{ |
82
|
|
|
if ($headers->containsRequiredHeaders()) { |
83
|
|
|
$this->setCode(202); |
84
|
|
|
$this->setMessage('Good to continue!!!'); |
85
|
|
|
$headers->setStatusCode(202); |
86
|
|
|
} else { |
87
|
|
|
$this->setCode(400); |
88
|
|
|
$this->setMessage('Bad Request!'); |
89
|
|
|
$headers->setStatusCode(400); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Handle requests |
96
|
|
|
* |
97
|
|
|
* @param Request $request |
98
|
|
|
* @param Header $headers |
|
|
|
|
99
|
|
|
*/ |
100
|
|
|
public function handle(Request &$request, Headers $headers) |
101
|
|
|
{ |
102
|
|
|
if ($request->authenticate()) { |
|
|
|
|
103
|
|
|
// go go go go |
104
|
|
|
} else { |
105
|
|
|
$this->setCode(401); |
106
|
|
|
$this->setMessage('Unauthorized!'); |
107
|
|
|
$headers->setStatusCode(401); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Return Output object |
115
|
|
|
* |
116
|
|
|
* @return \stdClass |
117
|
|
|
*/ |
118
|
|
|
public function output() |
119
|
|
|
{ |
120
|
|
|
$output = new \stdClass(); |
|
|
|
|
121
|
|
|
$output->code = $this->code; |
|
|
|
|
122
|
|
|
$output->message = $this->message; |
123
|
|
|
|
124
|
|
|
if (is_object($this->data)) { |
125
|
|
|
$output->data = $this->data; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $output; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: