Skip to content

Commit 1753579

Browse files
access tmp file only once
1 parent b9650b2 commit 1753579

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/protocol/AProtocol.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ abstract class AProtocol
2727

2828
public ServerState $serverState;
2929

30+
private int $writeCalls = 0;
31+
3032
/**
3133
* Multiple RUN statements in transaction generates "streams" which are pulled or discarded
3234
* We are keeping track of open streams to keep valid Server State
@@ -63,21 +65,12 @@ public function __construct(
6365
*/
6466
protected function write(iterable $generator): void
6567
{
66-
$this->track();
68+
$this->writeCalls++;
6769
foreach ($generator as $buffer) {
6870
$this->connection->write($buffer);
6971
}
7072
}
7173

72-
private function track(): void
73-
{
74-
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR']. DIRECTORY_SEPARATOR)) {
75-
$file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt';
76-
$count = file_exists($file) ? intval(file_get_contents($file)) : 0;
77-
file_put_contents($file, $count + 1);
78-
}
79-
}
80-
8174
/**
8275
* Read from connection
8376
* @throws BoltException
@@ -172,4 +165,13 @@ public function getResponse(): Response
172165

173166
return $response;
174167
}
168+
169+
public function __destruct()
170+
{
171+
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR']. DIRECTORY_SEPARATOR)) {
172+
$file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt';
173+
$count = file_exists($file) ? intval(file_get_contents($file)) : 0;
174+
file_put_contents($file, $count + $this->writeCalls);
175+
}
176+
}
175177
}

0 commit comments

Comments
 (0)