Skip to content

Commit f04bcd4

Browse files
check if temp is writable. code cleanup.
1 parent 3a79ee2 commit f04bcd4

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

src/Bolt.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ final class Bolt
2626

2727
public function __construct(private IConnection $connection)
2828
{
29-
$_ENV['TEMP_DIR'] = getenv('TEMP') ?: getenv('TMPDIR') ?: (dirname(__DIR__) . DIRECTORY_SEPARATOR . 'temp');
30-
if (!file_exists($_ENV['TEMP_DIR'])) {
31-
mkdir($_ENV['TEMP_DIR'], recursive: true);
32-
}
33-
29+
$this->tempDirectoryInit();
3430
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
3531
if (!file_exists($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR)) {
3632
mkdir($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics');
@@ -41,6 +37,20 @@ public function __construct(private IConnection $connection)
4137
$this->setProtocolVersions(5.4, 5, 4.4);
4238
}
4339

40+
private function tempDirectoryInit(): void
41+
{
42+
$_ENV['TEMP_DIR'] = getenv('TEMP');
43+
if ($_ENV['TEMP_DIR'] === false || !is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
44+
$_ENV['TEMP_DIR'] = getenv('TMPDIR');
45+
}
46+
if ($_ENV['TEMP_DIR'] === false || !is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
47+
$_ENV['TEMP_DIR'] = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'temp';
48+
}
49+
if (!file_exists($_ENV['TEMP_DIR'])) {
50+
mkdir($_ENV['TEMP_DIR'], recursive: true);
51+
}
52+
}
53+
4454
private function track(): void
4555
{
4656
foreach (glob($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.*.cnt') as $file) {

src/protocol/AProtocol.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,15 @@ public function __construct(
6363
*/
6464
protected function write(iterable $generator): void
6565
{
66-
if (!getenv('BOLT_ANALYTICS_OPTOUT')) {
67-
$this->track();
68-
}
66+
$this->track();
6967
foreach ($generator as $buffer) {
7068
$this->connection->write($buffer);
7169
}
7270
}
7371

7472
private function track(): void
7573
{
76-
if (is_writable($_ENV['TEMP_DIR']. DIRECTORY_SEPARATOR)) {
74+
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR']. DIRECTORY_SEPARATOR)) {
7775
$file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt';
7876
$count = file_exists($file) ? intval(file_get_contents($file)) : 0;
7977
file_put_contents($file, $count + 1);

tests/protocol/ProtocolLayer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,13 @@ public function readCallback(int $length = 2048): string
100100
*/
101101
protected function setUp(): void
102102
{
103-
$_ENV['TEMP_DIR'] = getenv('TEMP') ?: getenv('TMPDIR') ?: (dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'temp');
103+
$_ENV['TEMP_DIR'] = getenv('TEMP');
104+
if ($_ENV['TEMP_DIR'] === false || !is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
105+
$_ENV['TEMP_DIR'] = getenv('TMPDIR');
106+
}
107+
if ($_ENV['TEMP_DIR'] === false || !is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
108+
$_ENV['TEMP_DIR'] = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'temp';
109+
}
104110
if (!file_exists($_ENV['TEMP_DIR'])) {
105111
mkdir($_ENV['TEMP_DIR'], recursive: true);
106112
}

0 commit comments

Comments
 (0)