1 | <?php |
||
14 | class WebApplication extends AbstractWebApplication |
||
15 | { |
||
16 | /** |
||
17 | * Application analytics object. |
||
18 | * |
||
19 | * @var Analytics |
||
20 | * @since 1.0 |
||
21 | */ |
||
22 | private $analytics; |
||
23 | |||
24 | /** |
||
25 | * Response mime type. |
||
26 | * |
||
27 | * @var string |
||
28 | * @since 1.0 |
||
29 | */ |
||
30 | public $mimeType = 'application/json'; |
||
31 | |||
32 | /** |
||
33 | * Application router. |
||
34 | * |
||
35 | * @var Router |
||
36 | * @since 1.0 |
||
37 | */ |
||
38 | private $router; |
||
39 | |||
40 | /** |
||
41 | * Method to run the application routines. |
||
42 | * |
||
43 | * @return void |
||
44 | * |
||
45 | * @since 1.0 |
||
46 | */ |
||
47 | 5 | public function doExecute() |
|
48 | { |
||
49 | // On a GET request to the live domain, submit analytics data |
||
50 | 5 | if ($this->input->getMethod() === 'GET' |
|
51 | 5 | && strpos($this->input->server->getString('HTTP_HOST', ''), 'developer.joomla.org') === 0 |
|
52 | 5 | && $this->analytics) |
|
53 | 5 | { |
|
54 | $this->analytics->setAsyncRequest(true) |
||
55 | ->setProtocolVersion('1') |
||
56 | ->setTrackingId('UA-544070-16') |
||
57 | ->setClientId(Uuid::uuid4()->toString()) |
||
58 | ->setDocumentPath($this->get('uri.base.path')) |
||
59 | ->setIpOverride($this->input->server->getString('REMOTE_ADDR', '127.0.0.1')) |
||
60 | ->setUserAgentOverride($this->input->server->getString('HTTP_USER_AGENT', 'JoomlaStats/1.0')); |
||
61 | |||
62 | // Don't allow sending Analytics data to cause a failure |
||
63 | try |
||
64 | { |
||
65 | $this->analytics->sendPageview(); |
||
66 | } |
||
67 | catch (\Exception $e) |
||
68 | { |
||
69 | // Log the error for reference |
||
70 | $this->getLogger()->error( |
||
71 | 'Error sending analytics data.', |
||
72 | ['exception' => $e] |
||
73 | ); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | try |
||
78 | { |
||
79 | 5 | $this->router->getController($this->get('uri.route'))->execute(); |
|
80 | } |
||
81 | 5 | catch (\Exception $e) |
|
82 | { |
||
83 | // Log the error for reference |
||
84 | 4 | $this->getLogger()->error( |
|
85 | 4 | sprintf('Uncaught Exception of type %s caught.', get_class($e)), |
|
86 | 4 | ['exception' => $e] |
|
87 | 4 | ); |
|
88 | |||
89 | 4 | $this->setErrorHeader($e); |
|
90 | |||
91 | $data = [ |
||
92 | 4 | 'error' => true, |
|
93 | 4 | 'message' => $e->getMessage(), |
|
94 | 4 | ]; |
|
95 | |||
96 | 4 | $this->setBody(json_encode($data)); |
|
97 | } |
||
98 | 5 | } |
|
99 | |||
100 | /** |
||
101 | * Set the application's analytics object. |
||
102 | * |
||
103 | * @param Analytics $analytics Analytics object to set. |
||
104 | * |
||
105 | * @return $this |
||
106 | * |
||
107 | * @since 1.0 |
||
108 | */ |
||
109 | public function setAnalytics(Analytics $analytics) |
||
110 | { |
||
111 | $this->analytics = $analytics; |
||
112 | |||
113 | return $this; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Set the HTTP Response Header for error conditions. |
||
118 | * |
||
119 | * @param \Exception $exception The Exception object to process. |
||
120 | * |
||
121 | * @return void |
||
122 | * |
||
123 | * @since 1.0 |
||
124 | */ |
||
125 | 4 | private function setErrorHeader(\Exception $exception) |
|
151 | |||
152 | /** |
||
153 | * Set the application's router. |
||
154 | * |
||
155 | * @param Router $router Router object to set. |
||
156 | * |
||
157 | * @return $this |
||
158 | * |
||
159 | * @since 1.0 |
||
160 | */ |
||
161 | 1 | public function setRouter(Router $router) |
|
167 | } |
||
168 |