diff --git a/src/protocol/AProtocol.php b/src/protocol/AProtocol.php index 66fe4e3..37fd374 100644 --- a/src/protocol/AProtocol.php +++ b/src/protocol/AProtocol.php @@ -27,6 +27,8 @@ abstract class AProtocol public ServerState $serverState; + private int $writeCalls = 0; + /** * Multiple RUN statements in transaction generates "streams" which are pulled or discarded * We are keeping track of open streams to keep valid Server State @@ -63,21 +65,12 @@ public function __construct( */ protected function write(iterable $generator): void { - $this->track(); + $this->writeCalls++; foreach ($generator as $buffer) { $this->connection->write($buffer); } } - private function track(): void - { - if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR']. DIRECTORY_SEPARATOR)) { - $file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt'; - $count = file_exists($file) ? intval(file_get_contents($file)) : 0; - file_put_contents($file, $count + 1); - } - } - /** * Read from connection * @throws BoltException @@ -172,4 +165,13 @@ public function getResponse(): Response return $response; } + + public function __destruct() + { + if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR']. DIRECTORY_SEPARATOR)) { + $file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt'; + $count = file_exists($file) ? intval(file_get_contents($file)) : 0; + file_put_contents($file, $count + $this->writeCalls); + } + } }