-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAfterRequestHandler.php
More file actions
97 lines (80 loc) · 2.92 KB
/
AfterRequestHandler.php
File metadata and controls
97 lines (80 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace BitSensor\Handler;
use BitSensor\Core\BitSensor;
use BitSensor\Exception\ApiException;
use Proto\Datapoint;
/**
* Handler to run after the PHP script finished. Sends logged data to the BitSensor servers.
* @package BitSensor\Handler
*/
class AfterRequestHandler extends AbstractHandler
{
/**
* Flush output at the end of the request, to enable the browser to
* render the page, while the connection might still be open.
*
* @var boolean
*/
private static $enableOutputFlushing = false;
/**
* Execute fastcgi_finish_request(). Turning this on allows the browser to render the page
* while BitSensor is still working in the background. A side effect is that output
* will not be flushed from the shutdown_hooks that run after BitSensor.
*
* @var boolean
*/
private static $executeFastcgiFinishRequest = false;
/**
* @param bool $enableOutputFlushing
*/
public static function setEnableOutputFlushing(bool $enableOutputFlushing)
{
self::$enableOutputFlushing = $enableOutputFlushing;
}
/** @noinspection PhpDocMissingReturnTagInspection */
/**
* @param bool $executeFastcgiFinishRequest
*/
public static function setExecuteFastcgiFinishRequest($executeFastcgiFinishRequest)
{
if ($executeFastcgiFinishRequest && !function_exists('fastcgi_finish_request'))
return trigger_error("fastcgi is not available, however you wanted to enable it in the BitSensor configuration.
Please install fastcgi or disable executeFastCgi", E_USER_WARNING);
self::$executeFastcgiFinishRequest = $executeFastcgiFinishRequest;
}
public function configure($config)
{
parent::configure($config);
if (array_key_exists('outputFlushing', $config))
self::$enableOutputFlushing = $config['outputFlushing'] == 'on' ? true : false;
if (array_key_exists('executeFastCgi', $config))
self::setExecuteFastcgiFinishRequest($config['executeFastCgi'] == 'on' ? true : false);
}
/**
* Extend a Datapoint with more fields
*
* @param Datapoint $datapoint
* @return mixed
*/
public function doHandle(Datapoint $datapoint)
{
// Correctly sets working directory
chdir(BITSENSOR_WORKING_DIR);
if (self::$enableOutputFlushing) {
ob_flush();
flush();
}
if (self::$executeFastcgiFinishRequest) {
fastcgi_finish_request();
}
try {
$connector = BitSensor::getConnector();
if (!isset($connector))
return trigger_error("BitSensor is configured without connector. Connector configuration should be specified",
E_USER_WARNING);
return $connector->close($datapoint);
} catch (ApiException $e) {
trigger_error($e->getMessage(), E_USER_WARNING);
}
}
}