Skip to content

Commit

Permalink
change of used tmp dir
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanak-michal committed Sep 29, 2024
1 parent e2eeaf0 commit 25e91c3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 37 deletions.
23 changes: 4 additions & 19 deletions src/Bolt.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,19 @@ final class Bolt

public function __construct(private IConnection $connection)
{
$this->tempDirectoryInit();
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
if (!file_exists($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR)) {
mkdir($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics');
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable(sys_get_temp_dir() . DIRECTORY_SEPARATOR)) {
if (!file_exists(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR)) {
mkdir(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-bolt-analytics', recursive: true);
}
$this->track();
}

$this->setProtocolVersions(5.4, 5, 4.4);
}

private function tempDirectoryInit(): void
{
$_ENV['TEMP_DIR'] = getenv('TEMP');
if ($_ENV['TEMP_DIR'] === false || !is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
$_ENV['TEMP_DIR'] = getenv('TMPDIR');
}
if ($_ENV['TEMP_DIR'] === false || !is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
$_ENV['TEMP_DIR'] = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'temp';
}
if (!file_exists($_ENV['TEMP_DIR'])) {
mkdir($_ENV['TEMP_DIR'], recursive: true);
}
}

private function track(): void
{
foreach (glob($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'analytics.*.json') as $file) {
foreach (glob(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'analytics.*.json') as $file) {
$time = intval(explode('.', basename($file))[1]);
if ($time < strtotime('today')) {
$data = (array)json_decode((string)file_get_contents($file), true);
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class FileCache implements CacheInterface

public function __construct()
{
$this->tempDir = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-filecache' . DIRECTORY_SEPARATOR;
$this->tempDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-bolt-filecache' . DIRECTORY_SEPARATOR;
if (!file_exists($this->tempDir)) {
mkdir($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-filecache');
mkdir(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-bolt-filecache', recursive: true);
}

// clean old
Expand Down
4 changes: 2 additions & 2 deletions src/protocol/AProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ public function getResponse(): 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 . 'analytics.' . strtotime('today') . '.json';
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR)) {
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'analytics.' . strtotime('today') . '.json';
$data = file_exists($file) ? (array)json_decode((string)file_get_contents($file), true) : [];
$data['queries'] = ($data['queries'] ?? 0) + $this->writeCalls;
$data['sessions'] = ($data['sessions'] ?? 0) + 1;
Expand Down
17 changes: 3 additions & 14 deletions tests/protocol/ProtocolLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,9 @@ public function readCallback(int $length = 2048): string
*/
protected function setUp(): void
{
$_ENV['TEMP_DIR'] = getenv('TEMP');
if ($_ENV['TEMP_DIR'] === false || !is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
$_ENV['TEMP_DIR'] = getenv('TMPDIR');
}
if ($_ENV['TEMP_DIR'] === false || !is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
$_ENV['TEMP_DIR'] = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'temp';
}
if (!file_exists($_ENV['TEMP_DIR'])) {
mkdir($_ENV['TEMP_DIR'], recursive: true);
}

if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
if (!file_exists($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR)) {
mkdir($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics');
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable(sys_get_temp_dir() . DIRECTORY_SEPARATOR)) {
if (!file_exists(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR)) {
mkdir(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'php-bolt-analytics', recursive: true);
}
}

Expand Down

0 comments on commit 25e91c3

Please sign in to comment.