Automattic\WooCommerce\Internal\PushNotifications\DataStores
StepLogFileReader::read
Returns the newest lines written under a source between two times.
Reads one UTC day at a time, newest first, and stops once it holds enough rows, so a short page over a long range does not open every day's files. Rows are turned into what the caller wants as each line is read, and the set is trimmed after every file, so neither the range nor one large file decides how much is held at once. has_more says whether that left older lines unread.
Method of the class: StepLogFileReader{}
No Hooks.
Returns
array{rows:. array<int, array{timestamp: int, level: string, message: string, context: array|null, raw: string|null}>, covered_from: int, has_more: bool}
Usage
$StepLogFileReader = new StepLogFileReader(); $StepLogFileReader->read( $source, $from, $to, $limit, ?callable $expand ): array;
- $source(string) (required)
- The log source, e.g.
push-notifications-store-order. - $from(int) (required)
- Earliest timestamp to include, inclusive.
- $to(int) (required)
- Latest timestamp to include, inclusive.
- $limit(int)
- The fewest rows to collect before stopping.
Default:PHP_INT_MAX - ?callable $expand
- .
Default:null
Changelog
| Since 11.3.0 | Introduced. |
StepLogFileReader::read() StepLogFileReader::read code WC 11.3.0
public function read( string $source, int $from, int $to, int $limit = PHP_INT_MAX, ?callable $expand = null ): array {
$directory = Settings::get_log_directory( false );
$days = self::get_days( $from, $to );
$rows = array();
$covered_from = $to;
$has_more = false;
foreach ( $days as $index => $day ) {
$covered_from = max( $from, $day );
foreach ( self::get_filenames( $source, $day ) as $filename ) {
$file = new File( $directory . $filename );
if ( ! $file->is_readable() ) {
continue;
}
$stream = $file->get_stream();
if ( ! is_resource( $stream ) ) {
continue;
}
while ( false !== ( $line = fgets( $stream ) ) ) { // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
$row = self::parse_line( $line );
if ( null === $row || $row['timestamp'] < $from || $row['timestamp'] > $to ) {
continue;
}
if ( null === $expand ) {
$rows[] = $row;
continue;
}
foreach ( $expand( $row ) as $expanded ) {
$rows[] = $expanded;
}
}
$file->close_stream();
$kept = self::keep_newest( $rows, $limit );
$has_more = $has_more || count( $kept ) < count( $rows );
$rows = $kept;
}
/*
* Every remaining day is older than everything held, so stopping at
* a day boundary cannot skip a row that belonged on this page. The
* boundary is also a second boundary, which the page's whole-second
* rule needs.
*/
if ( count( $rows ) >= $limit ) {
$has_more = $has_more || isset( $days[ $index + 1 ] );
break;
}
}
usort( $rows, fn( array $a, array $b ) => $a['timestamp'] <=> $b['timestamp'] );
return array(
'rows' => $rows,
'covered_from' => $covered_from,
'has_more' => $has_more,
);
}