-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathImportLogger.php
More file actions
234 lines (199 loc) · 5.09 KB
/
Copy pathImportLogger.php
File metadata and controls
234 lines (199 loc) · 5.09 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
namespace lloc\Msls\ContentImport;
use lloc\Msls\ContentImport\LogWriters\AdminNoticeLogger;
use lloc\Msls\ContentImport\LogWriters\LogWriter;
class ImportLogger {
/**
* @var non-empty-string
*/
protected string $levels_delimiter = '/';
/**
* @var array<string, string[]>
*/
protected array $data = array(
'info' => array(),
'error' => array(),
'success' => array(),
);
/**
* @var ImportCoordinates
*/
protected $import_coordinates;
public function __construct( ImportCoordinates $import_coordinates ) {
$this->import_coordinates = $import_coordinates;
}
/**
* Merges the specified log data into this log.
*
* @param ImportLogger|null $logger
*/
public function merge( ImportLogger $logger = null ): void {
if ( null === $logger ) {
return;
}
$this->data = array_merge_recursive( $this->data, $logger->get_data() );
}
/**
* @return array<string, string[]>
*/
public function get_data(): array {
return $this->data;
}
/**
* Saves the log or prints it some place.
*/
public function save(): void {
$default_log_writer = AdminNoticeLogger::instance();
$default_log_writer->set_import_coordinates( $this->import_coordinates );
/**
* Filters the log class or object that should be used to write the log to the destination.
*
* @param mixed $default_log_writer
* @param ImportCoordinates $import_coordinates
*/
$log_writer = apply_filters( 'msls_content_import_log_writer', $default_log_writer, $this->import_coordinates );
if ( empty( $log_writer ) ) {
// We assume that was done on purpose to prevent logging.
return;
}
if ( is_string( $log_writer ) ) {
$log_writer = new $log_writer();
}
if ( ! $log_writer instanceof LogWriter ) {
// Something is fishy, let's use the default one.
$log_writer = $default_log_writer;
}
$log_writer->write( $this->get_data() );
}
/**
* Logs an error.
*
* @param string $where A location string using `/` as level format.
* @param mixed $what What should be stored in the log.
*/
public function log_error( $where, $what ): void {
$this->log( $where, $what, 'error' );
}
/**
* Logs something.
*
* @param string $where A location string using `/` as level format.
* @param mixed $what What should be stored in the log.
* @param string $root Where to log the information.
*/
protected function log( $where, $what, $root = 'info' ): void {
if ( ! isset( $this->data[ $root ] ) ) {
$this->data[ $root ] = array();
}
$data = $this->build_nested_array( $this->build_path( $where ), $what );
$this->data[ $root ] = array_merge_recursive( $this->data[ $root ], $data );
}
/**
* @param string[] $path
* @param mixed $what
*
* @return array<string, mixed>
*/
protected function build_nested_array( $path, $what = '' ): array {
$json = '{"'
. implode( '":{"', $path )
. '":' . wp_json_encode( $what )
. implode(
'',
array_fill(
0,
count( $path ),
'}'
)
);
$data = json_decode( $json, true );
return $data;
}
/**
* @param string $where
*
* @return string[]
*/
protected function build_path( string $where ): array {
$where_path = explode( $this->levels_delimiter, $where );
return $where_path;
}
/**
* Returns the string that will be used to split paths into levels.
*
* @return string
*/
public function get_levels_delimiter(): string {
return $this->levels_delimiter;
}
/**
* Sets the string that will be used to split paths into levels.
*
* @param non-empty-string $levels_delimiter
*/
public function set_levels_delimiter( string $levels_delimiter ): void {
$this->levels_delimiter = $levels_delimiter;
}
/**
* Logs a success.
*
* @param string $where A location string using `/` as level format.
* @param mixed $what What should be stored in the log.
*/
public function log_success( $where, $what ): void {
$this->log( $where, $what, 'success' );
}
/**
* Logs some generic information.
*
* @param string $key
* @param string $message
*/
public function log_information( $key, $message ): void {
$this->data['info'][ $key ] = $message;
}
/**
* @param string $where
*
* @return mixed
*/
public function get_error( $where ) {
return $this->get_nested_value( 'error' . $this->levels_delimiter . $where );
}
/**
* @param string $where
*
* @return mixed
*/
protected function get_nested_value( $where ) {
$path = $this->build_path( $where );
$first = array_shift( $path );
if ( null === $first || ! isset( $this->data[ $first ] ) ) {
return null;
}
$data = $this->data[ $first ];
foreach ( $path as $frag ) {
if ( ! is_array( $data ) || ! isset( $data[ $frag ] ) ) {
return null;
}
$data = $data[ $frag ];
}
return $data;
}
/**
* @param string $where
*
* @return mixed
*/
public function get_success( $where ) {
return $this->get_nested_value( 'success' . $this->levels_delimiter . $where );
}
/**
* @param string $key
*
* @return mixed
*/
public function get_information( $key ) {
return isset( $this->data['info'][ $key ] ) ? $this->data['info'][ $key ] : '';
}
}