class-abstract-buffer.php
3.84 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace WP_Rocket\Buffer;
use WP_Rocket\Logger\Logger;
/**
* Handle page cache and optimizations.
*
* @since 3.3
* @author Grégory Viguier
*/
abstract class Abstract_Buffer {
/**
* Process identifier used by the logger.
*
* @var string
* @since 3.3
* @access protected
* @author Grégory Viguier
*/
protected $process_id;
/**
* Instance of the Tests class.
*
* @var Tests
* @since 3.3
* @access protected
* @author Grégory Viguier
*/
protected $tests;
/**
* Constructor.
*
* @since 3.3
* @access public
* @author Grégory Viguier
*
* @param Tests $tests Tests instance.
*/
public function __construct( Tests $tests ) {
$this->tests = $tests;
}
/** ----------------------------------------------------------------------------------------- */
/** PROCESS ================================================================================= */
/** ----------------------------------------------------------------------------------------- */
/**
* Launch the process if the tests succeed.
* This should be the first thing to use after initializing the class.
*
* @since 3.3
* @access public
* @see $this->tests->can_init_process()
* @author Grégory Viguier
*/
abstract public function maybe_init_process();
/**
* Process the page buffer if the 2nd set of tests succeed.
* It should be used like this:
* ob_start( [ $this, 'maybe_process_buffer' ] );
*
* @since 3.3
* @access public
* @see $this->tests->can_process_buffer()
* @author Grégory Viguier
*
* @param string $buffer The buffer content.
* @return string The buffered content
*/
abstract public function maybe_process_buffer( $buffer );
/** ----------------------------------------------------------------------------------------- */
/** LOG ===================================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Log the last test "error".
*
* @since 3.3
* @access protected
* @author Grégory Viguier
*/
protected function log_last_test_error() {
$error = $this->tests->get_last_error();
$this->log( $error['message'], $error['data'] );
}
/**
* Log events.
*
* @since 3.3
* @access protected
* @author Grégory Viguier
*
* @param string $message A message to log.
* @param array $data Related data.
* @param string $type Event type to log. Possible values are 'info', 'error', and 'debug' (default).
*/
protected function log( $message, $data = [], $type = 'debug' ) {
$data = array_merge(
[
$this->get_process_id(),
'request_uri' => $this->tests->get_raw_request_uri(),
],
$data
);
if ( isset( $data['cookies'] ) ) {
$data['cookies'] = Logger::remove_auth_cookies( $data['cookies'] );
}
switch ( $type ) {
case 'info':
Logger::info( $message, $data );
break;
case 'error':
Logger::error( $message, $data );
break;
default:
Logger::debug( $message, $data );
}
}
/**
* Get the process identifier.
*
* @since 3.3
* @access public
* @author Grégory Viguier
*
* @return string
*/
public function get_process_id() {
return $this->process_id . ' - Thread #' . Logger::get_thread_id();
}
/** ----------------------------------------------------------------------------------------- */
/** VARIOUS TOOLS =========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Tell if the page content is HTML.
*
* @since 3.3
* @access protected
* @author Grégory Viguier
*
* @param string $buffer The buffer content.
* @return bool
*/
protected function is_html( $buffer ) {
return preg_match( '/<\/html>/i', $buffer );
}
}