1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stats; |
4
|
|
|
|
5
|
|
|
use Joomla\Application\AbstractWebApplication; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Web application for the stats server |
9
|
|
|
* |
10
|
|
|
* @since 1.0 |
11
|
|
|
*/ |
12
|
|
|
class WebApplication extends AbstractWebApplication |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Response mime type. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
* @since 1.0 |
19
|
|
|
*/ |
20
|
|
|
public $mimeType = 'application/json'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Application router. |
24
|
|
|
* |
25
|
|
|
* @var Router |
26
|
|
|
* @since 1.0 |
27
|
|
|
*/ |
28
|
|
|
private $router; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Method to run the application routines. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
* |
35
|
|
|
* @since 1.0 |
36
|
|
|
*/ |
37
|
5 |
|
public function doExecute() |
38
|
|
|
{ |
39
|
|
|
try |
40
|
|
|
{ |
41
|
5 |
|
$this->router->getController($this->get('uri.route'))->execute(); |
42
|
|
|
} |
43
|
5 |
|
catch (\Exception $e) |
44
|
|
|
{ |
45
|
|
|
// Log the error for reference |
46
|
4 |
|
$this->getLogger()->error( |
47
|
4 |
|
sprintf('Uncaught Exception of type %s caught.', get_class($e)), |
48
|
4 |
|
['exception' => $e] |
49
|
4 |
|
); |
50
|
|
|
|
51
|
4 |
|
$this->setErrorHeader($e); |
52
|
|
|
|
53
|
|
|
$data = [ |
54
|
4 |
|
'error' => true, |
55
|
4 |
|
'message' => $e->getMessage(), |
56
|
4 |
|
]; |
57
|
|
|
|
58
|
4 |
|
$this->setBody(json_encode($data)); |
59
|
|
|
} |
60
|
5 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the HTTP Response Header for error conditions. |
64
|
|
|
* |
65
|
|
|
* @param \Exception $exception The Exception object to process. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
* |
69
|
|
|
* @since 1.0 |
70
|
|
|
*/ |
71
|
4 |
|
private function setErrorHeader(\Exception $exception) |
72
|
|
|
{ |
73
|
4 |
|
switch ($exception->getCode()) |
74
|
|
|
{ |
75
|
4 |
|
case 401: |
76
|
1 |
|
$this->setHeader('HTTP/1.1 401 Unauthorized', 401, true); |
77
|
|
|
|
78
|
1 |
|
break; |
79
|
|
|
|
80
|
3 |
|
case 403: |
81
|
1 |
|
$this->setHeader('HTTP/1.1 403 Forbidden', 403, true); |
82
|
|
|
|
83
|
1 |
|
break; |
84
|
|
|
|
85
|
2 |
|
case 404: |
86
|
1 |
|
$this->setHeader('HTTP/1.1 404 Not Found', 404, true); |
87
|
|
|
|
88
|
1 |
|
break; |
89
|
|
|
|
90
|
1 |
|
case 500: |
91
|
1 |
|
default: |
92
|
1 |
|
$this->setHeader('HTTP/1.1 500 Internal Server Error', 500, true); |
93
|
|
|
|
94
|
1 |
|
break; |
95
|
4 |
|
} |
96
|
4 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set the application's router. |
100
|
|
|
* |
101
|
|
|
* @param Router $router Router object to set. |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
* |
105
|
|
|
* @since 1.0 |
106
|
|
|
*/ |
107
|
1 |
|
public function setRouter(Router $router) |
108
|
|
|
{ |
109
|
1 |
|
$this->router = $router; |
110
|
|
|
|
111
|
1 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|