Skip to content

Commit

Permalink
access tmp file only once
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanak-michal committed Jul 15, 2024
1 parent b9650b2 commit 1753579
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/protocol/AProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 1753579

Please sign in to comment.