Skip to content

Commit 1cea541

Browse files
author
kazsaj
committed
Merge remote-tracking branch 'upstream/master'
Conflicts: src/Docnet/JAPI.php Merging changes from Docnet master branch
2 parents 1c5e01c + ab68e1e commit 1cea541

File tree

4 files changed

+142
-10
lines changed

4 files changed

+142
-10
lines changed

src/Docnet/JAPI.php

+40-10
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@ class JAPI
3737
private static $obj_config = NULL;
3838

3939
/**
40-
* @var null
40+
* @var JAPI\Router
4141
*/
42-
private static $flt_startup = NULL;
42+
private static $obj_router = NULL;
4343

4444
/**
45-
* @var \Docnet\JAPI\Router
45+
* @var JAPI\Logger
4646
*/
47-
private static $obj_router = NULL;
47+
private $obj_logger = NULL;
48+
49+
/**
50+
* @var null|float
51+
*/
52+
private static $flt_startup = NULL;
4853

4954
/**
5055
* When creating a new JAPI, hook up the shutdown function and set Config
@@ -82,7 +87,6 @@ public function run()
8287
* Custom shutdown function
8388
*
8489
* @todo Consider checking if headers have already been sent
85-
* @todo Consider checking isLive before outputting message
8690
*/
8791
public function timeToDie()
8892
{
@@ -115,17 +119,19 @@ protected function jsonError($mix_message = NULL, $int_code = 500)
115119
header($_SERVER["SERVER_PROTOCOL"] . " 500 Internal Server Error", TRUE, 500);
116120
}
117121
if ($mix_message instanceof \Exception) {
118-
$str_message = (self::getConfig()->isLive() ? 'Exception' : get_class($mix_message) . ': ' . $mix_message->getMessage());
122+
$str_log = get_class($mix_message) . ': ' . $mix_message->getMessage();
123+
$str_message = self::getConfig()->isLive() ? 'Exception' : $str_log;
119124
} elseif (is_string($mix_message)) {
120-
$str_message = $mix_message;
125+
$str_log = $str_message = $mix_message;
121126
} else {
122-
$str_message = 'Unknown error';
127+
$str_log = $str_message = 'Unknown error';
123128
}
124129
header('Content-type: application/json');
125130
echo json_encode(array(
126131
'response' => (int)$int_code,
127132
'msg' => $str_message
128133
));
134+
$this->log(LOG_ERR, "[JAPI exiting with {$int_code}] " . $str_log);
129135
exit();
130136
}
131137

@@ -137,7 +143,7 @@ protected function jsonError($mix_message = NULL, $int_code = 500)
137143
public static function getRouter()
138144
{
139145
if (NULL === self::$obj_router) {
140-
self::$obj_router = new \Docnet\JAPI\Router();
146+
self::$obj_router = new JAPI\Router();
141147
}
142148
return self::$obj_router;
143149
}
@@ -147,7 +153,7 @@ public static function getRouter()
147153
*
148154
* @param JAPI\Interfaces\Router $obj_router
149155
*/
150-
public function setRouter(\Docnet\JAPI\Interfaces\Router $obj_router)
156+
public function setRouter(JAPI\Interfaces\Router $obj_router)
151157
{
152158
self::$obj_router = $obj_router;
153159
}
@@ -176,4 +182,28 @@ public static function getDuration($int_dp = 4)
176182
return round(microtime(TRUE) - self::$flt_startup, $int_dp);
177183
}
178184

185+
/**
186+
* Log to the current Logger, create one if needed
187+
*
188+
* @param $int_level
189+
* @param $str_message
190+
*/
191+
protected function log($int_level, $str_message)
192+
{
193+
if(NULL === $this->obj_logger) {
194+
$this->obj_logger = new JAPI\Logger();
195+
}
196+
$this->obj_logger->log($int_level, $str_message);
197+
}
198+
199+
/**
200+
* Set a custom Logger
201+
*
202+
* @param JAPI\Interfaces\Logger $obj_logger
203+
*/
204+
public function setLogger(JAPI\Interfaces\Logger $obj_logger)
205+
{
206+
$this->obj_logger = $obj_logger;
207+
}
208+
179209
}

src/Docnet/JAPI/Controller.php

+10
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ protected function getBody()
9999
return file_get_contents('php://input');
100100
}
101101

102+
/**
103+
* Get the request body as a JSON object
104+
*
105+
* @return mixed
106+
*/
107+
protected function getJson()
108+
{
109+
return json_decode($this->getBody());
110+
}
111+
102112
/**
103113
* Get a request parameter. Check GET then POST data.
104114
*

src/Docnet/JAPI/Interfaces/Logger.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright 2014 Docnet
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace Docnet\JAPI\Interfaces;
18+
19+
/**
20+
* JAPI Logger Interface
21+
*
22+
* @author Tom Walder <[email protected]>
23+
*/
24+
interface Logger
25+
{
26+
27+
/**
28+
* Log a message
29+
*
30+
* @param $int_level
31+
* @param $str_message
32+
*/
33+
public function log($int_level, $str_message);
34+
35+
}

src/Docnet/JAPI/Logger.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright 2014 Docnet
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
namespace Docnet\JAPI;
18+
/**
19+
* JAPI Logger
20+
*
21+
* @author Tom Walder <[email protected]>
22+
*/
23+
class Logger implements Interfaces\Logger
24+
{
25+
26+
/**
27+
* Default PHP log levels as strings
28+
*
29+
* @var array
30+
*/
31+
protected $arr_levels = array(
32+
LOG_EMERG => 'EMERG',
33+
LOG_ALERT => 'ALERT',
34+
LOG_CRIT => 'CRIT',
35+
LOG_ERR => 'ERR',
36+
LOG_WARNING => 'WARNING',
37+
LOG_NOTICE => 'NOTICE',
38+
LOG_INFO => 'INFO',
39+
LOG_DEBUG => 'DEBUG'
40+
);
41+
42+
/**
43+
* Log a message
44+
*
45+
* @param $int_level
46+
* @param $str_message
47+
*/
48+
public function log($int_level, $str_message)
49+
{
50+
if(!isset($this->arr_levels[$int_level])) {
51+
$int_level = LOG_ERR;
52+
}
53+
syslog($int_level, $str_message);
54+
error_log("[{$this->arr_levels[$int_level]}] {$str_message}");
55+
}
56+
57+
}

0 commit comments

Comments
 (0)