|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the Apix Project. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
|
8
|
|
|
* |
|
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Apix\Plugin; |
|
14
|
|
|
|
|
15
|
|
|
use Apix\Response; |
|
16
|
|
|
|
|
17
|
|
|
class OutputDebug extends PluginAbstract |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
public static $hook = array('response', 'early'); |
|
21
|
|
|
|
|
22
|
|
|
protected $options = array( |
|
23
|
|
|
'enable' => true, // whether to enable or not |
|
24
|
|
|
'name' => 'debug', // the header name |
|
25
|
|
|
'prepend' => false, // whether to prepend the debugging |
|
26
|
|
|
'timestamp' => 'D, d M Y H:i:s T', // stamp format, default to RFC1123 |
|
27
|
|
|
'extras' => null, // extras to inject, string or array |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
public function update(\SplSubject $response) |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
if (false === $this->options['enable']) { |
|
33
|
|
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$request = $response->getRequest(); |
|
|
|
|
|
|
37
|
|
|
$route = $response->getRoute(); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$headers = $response->getHeaders(); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
if (isset($_SERVER['X_AUTH_USER'])) { |
|
42
|
|
|
$headers['X_AUTH_USER'] = $_SERVER['X_AUTH_USER']; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (isset($_SERVER['X_AUTH_KEY'])) { |
|
46
|
|
|
$headers['X_AUTH_KEY'] = $_SERVER['X_AUTH_KEY']; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$data = array( |
|
50
|
|
|
'timestamp' => gmdate($this->options['timestamp']), |
|
51
|
|
|
'request' => sprintf('%s %s%s', |
|
52
|
|
|
$request->getMethod(), |
|
53
|
|
|
$request->getRequestedUri(), |
|
54
|
|
|
isset($_SERVER['SERVER_PROTOCOL']) |
|
55
|
|
|
? ' ' . $_SERVER['SERVER_PROTOCOL'] : null |
|
56
|
|
|
), |
|
57
|
|
|
'headers' => $headers, |
|
58
|
|
|
'output_format' => $response->getFormat(), |
|
|
|
|
|
|
59
|
|
|
'router_params' => $route->getParams(), |
|
60
|
|
|
'memory' => self::formatBytesToString(memory_get_usage()) |
|
61
|
|
|
. '~' . self::formatBytesToString( |
|
62
|
|
|
memory_get_peak_usage() |
|
63
|
|
|
) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
if (defined('APIX_START_TIME')) { |
|
67
|
|
|
$data['timing'] = round(microtime(true) - APIX_START_TIME, 3) . ' seconds'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
View Code Duplication |
if (null !== $this->options['extras']) { |
|
|
|
|
|
|
71
|
|
|
$data['extras'] = $this->options['extras']; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$name = $this->options['name']; |
|
75
|
|
View Code Duplication |
if (true === $this->options['prepend']) { |
|
|
|
|
|
|
76
|
|
|
$response->results = array($name=>$data)+$response->results; |
|
|
|
|
|
|
77
|
|
|
} else { |
|
78
|
|
|
$response->results[$name] = $data; |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Formats bytes into a human readable string |
|
84
|
|
|
* |
|
85
|
|
|
* @param int $bytes |
|
86
|
|
|
* @param boolean $long |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function formatBytesToString($bytes, $long=false) |
|
90
|
|
|
{ |
|
91
|
|
|
$bytes = (int) $bytes; |
|
92
|
|
|
|
|
93
|
|
|
$unit = false == $long |
|
|
|
|
|
|
94
|
|
|
? array('B','kB','MB','GB','TB','PB','EB') |
|
95
|
|
|
: array( |
|
96
|
|
|
'bytes','kilobytes','megabytes','gigabytes', |
|
97
|
|
|
'terabytes','petabytes','exabytes' |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$i = floor(log($bytes, 1024)); |
|
101
|
|
|
|
|
102
|
|
|
return round($bytes/pow(1024, $i), 2) . ' ' . $unit[$i]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: