diff --git a/src/Bolt.php b/src/Bolt.php index 64a9689..6053a94 100644 --- a/src/Bolt.php +++ b/src/Bolt.php @@ -26,10 +26,9 @@ 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(); } @@ -37,23 +36,9 @@ public function __construct(private IConnection $connection) $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); diff --git a/src/helpers/FileCache.php b/src/helpers/FileCache.php index bd89ffd..deff775 100644 --- a/src/helpers/FileCache.php +++ b/src/helpers/FileCache.php @@ -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 diff --git a/src/protocol/AProtocol.php b/src/protocol/AProtocol.php index e964925..5ef091e 100644 --- a/src/protocol/AProtocol.php +++ b/src/protocol/AProtocol.php @@ -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; diff --git a/tests/protocol/ProtocolLayer.php b/tests/protocol/ProtocolLayer.php index c572944..31430ff 100644 --- a/tests/protocol/ProtocolLayer.php +++ b/tests/protocol/ProtocolLayer.php @@ -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); } }