3v4l.org

run code in 300+ PHP versions simultaneously
<?php // just make this example code not pitch a bunch of errors. :P trait ItsAllFakeAnyway { public function __call($func, $args) { printf("%s::%s(%s)\n", __CLASS__, $func, json_encode($args)); } } /* Whisper TLDR: - Whisper is a time-series database format, like RRD but better. - Each Whisper file contains one or more Archives representing a series of points at varying precisions */ interface WhisperInterface { /*...*/ } class Whisper implements WhisperInterface { use ItsAllFakeAnyway; public function generateArchives() { while($header = $this->readBinFile()) { yield $this->createArchive($header); } } protected function createArchive($header) { return new Archive($header); } } interface ArchiveInterface { /*...*/ } class Archive implements ArchiveInterface { use ItsAllFakeAnyway; public function generatePoints() { while($point = $this->readBinFile()) { yield $point; } } } /* The intention of the WhisperAggregator class is to ingest metrics from many Whisper files and aggregate them into a single file. While you *can* configure the thing that initially ingests the metrics to do this *for* you, I did not do that initially and I have to backfill the aggregations as below. */ class WhisperAggregator { use ItsAllFakeAnyway; public function processWhisper(WhisperInterface $w) { foreach($w->generateArchives() as $a) { $this->processArchive($a); } } protected function processArchive(ArchiveInterface $a) { foreach($w->generatePoints() as $p) { $this->addPointToAggregatedArchive($a); } } } // eg: $a = new WhisperAggregator(); foreach(['a.wsp', 'b.wsp'] as $file) { $a->processWhisper(new Whisper($file)); } $a->writeTo('agg.wsp'); /* This works just fine until I have a case where I need to modify the point data in order to backfill aggregations that were entirely missing at certain points in time, eg a "count" aggregation. So to accomplish this I want to simply change each point value to 1. I've accomplished this as below, but I'm curious if there is an applicable design pattern for something like this that avoids the below extensions. I was considering a Decorator pattern, but I don't know how best to apply a Decorator while also maintaining the interface contract short of writing a passthrough method for every interface method, which would be far more work than extending the class. */ class WhisperShim extends Whisper { protected function createArchive($header) { return new ArchiveShim($header); } } class ArchiveShim extends Archive { public function generatePoints() { foreach(parent::generatePoints() as $point) { $point['value'] = 1; yield $point; } } } // eg: $a = new WhisperAggregator(); foreach(['a.wsp', 'b.wsp'] as $file) { $a->processWhisper(new WhisperShim($file)); } $a->writeTo('agg.wsp');
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Whisper::readBinFile([]) Whisper::readBinFile([]) WhisperAggregator::writeTo(["agg.wsp"]) Whisper::readBinFile([]) Whisper::readBinFile([]) WhisperAggregator::writeTo(["agg.wsp"])
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Whisper::readBinFile([]) Whisper::readBinFile([]) WhisperAggregator::writeTo(["agg.wsp"]) Whisper::readBinFile([]) Whisper::readBinFile([]) WhisperAggregator::writeTo(["agg.wsp"])

preferences:
207.47 ms | 402 KiB | 203 Q