Skip to content

Commit 438c021

Browse files
Merge pull request #141 from neo4j-php/temp_dir
improved usage of temp directory.
2 parents 0d4c229 + 39648c5 commit 438c021

28 files changed

+53
-42
lines changed

src/Bolt.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,25 @@ final class Bolt
2626

2727
public function __construct(private IConnection $connection)
2828
{
29-
if (!file_exists(getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR)) {
30-
mkdir(getcwd() . DIRECTORY_SEPARATOR . 'temp');
29+
$_ENV['TEMP_DIR'] = getenv('TEMP') ?: getenv('TMPDIR') ?: (dirname(__DIR__) . DIRECTORY_SEPARATOR . 'temp');
30+
var_dump($_ENV['TEMP_DIR']);
31+
if (!file_exists($_ENV['TEMP_DIR'])) {
32+
mkdir($_ENV['TEMP_DIR'], recursive: true);
3133
}
34+
3235
if (!getenv('BOLT_ANALYTICS_OPTOUT')) {
36+
if (!file_exists($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR)) {
37+
mkdir($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics');
38+
}
3339
$this->track();
3440
}
41+
3542
$this->setProtocolVersions(5.4, 5, 4.4);
3643
}
3744

3845
private function track(): void
3946
{
40-
foreach (glob(getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'queries.*.cnt') as $file) {
47+
foreach (glob($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.*.cnt') as $file) {
4148
$time = intval(explode('.', basename($file))[1]);
4249
if ($time < strtotime('today')) {
4350
$count = file_get_contents($file);

src/helpers/FileCache.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ class FileCache implements CacheInterface
1717

1818
public function __construct()
1919
{
20-
$this->tempDir = getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR;
21-
20+
$this->tempDir = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-filecache' . DIRECTORY_SEPARATOR;
2221
if (!file_exists($this->tempDir)) {
23-
mkdir(rtrim($this->tempDir, DIRECTORY_SEPARATOR));
22+
mkdir($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-filecache');
2423
}
2524

2625
// clean old
27-
foreach (scandir($this->tempDir . 'temp') as $file) {
26+
foreach (scandir($this->tempDir) as $file) {
2827
if ($file == '.' || $file == '..')
2928
continue;
3029
if (filemtime($this->tempDir . $file) < strtotime('-1 hour'))

src/protocol/AProtocol.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected function write(iterable $generator): void
7373

7474
private function track(): void
7575
{
76-
$file = getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt';
76+
$file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt';
7777
$count = file_exists($file) ? intval(file_get_contents($file)) : 0;
7878
file_put_contents($file, $count + 1);
7979
}

tests/BoltTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @link https://github.com/neo4j-php/Bolt
1515
* @package Bolt\tests
1616
*/
17-
class BoltTest extends ATest
17+
class BoltTest extends TestLayer
1818
{
1919
public function testSockets(): void
2020
{

tests/PerformanceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @link https://github.com/neo4j-php/Bolt
1515
* @package Bolt\tests
1616
*/
17-
class PerformanceTest extends ATest
17+
class PerformanceTest extends TestLayer
1818
{
1919
public function test50KRecords(): void
2020
{

tests/ATest.php renamed to tests/TestLayer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Class ATest
1010
* @package Bolt\tests
1111
*/
12-
class ATest extends \PHPUnit\Framework\TestCase
12+
abstract class TestLayer extends \PHPUnit\Framework\TestCase
1313
{
1414
public static function setUpBeforeClass(): void
1515
{

tests/connection/ConnectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Bolt\tests\connection;
44

55
use Bolt\Bolt;
6-
use Bolt\tests\ATest;
6+
use Bolt\tests\TestLayer;
77
use Bolt\connection\{
88
IConnection,
99
Socket,
@@ -18,7 +18,7 @@
1818
* @link https://github.com/neo4j-php/Bolt
1919
* @package Bolt\tests\connection
2020
*/
21-
final class ConnectionTest extends ATest
21+
final class ConnectionTest extends TestLayer
2222
{
2323
public function provideConnections(): array
2424
{

tests/packstream/v1/BytesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
use Bolt\Bolt;
66
use Bolt\packstream\Bytes;
77
use Bolt\protocol\AProtocol;
8-
use Bolt\tests\ATest;
8+
use Bolt\tests\TestLayer;
99

1010
/**
1111
* Class BytesTest
1212
* @package Bolt\tests\packstream\v1
1313
*/
14-
class BytesTest extends ATest
14+
class BytesTest extends TestLayer
1515
{
1616
public function testInit(): AProtocol
1717
{

tests/packstream/v1/PackerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Bolt\Bolt;
66
use Bolt\protocol\AProtocol;
7-
use Bolt\tests\ATest;
7+
use Bolt\tests\TestLayer;
88

99
/**
1010
* Class PackerTest
@@ -13,7 +13,7 @@
1313
* @link https://github.com/neo4j-php/Bolt
1414
* @package Bolt\tests\packstream\v1
1515
*/
16-
class PackerTest extends ATest
16+
class PackerTest extends TestLayer
1717
{
1818
public function testInit(): AProtocol
1919
{

tests/packstream/v1/UnpackerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Bolt\Bolt;
66
use Bolt\protocol\{AProtocol, Response};
7-
use Bolt\tests\ATest;
7+
use Bolt\tests\TestLayer;
88
use Bolt\enum\Signature;
99

1010
/**
@@ -14,7 +14,7 @@
1414
* @link https://github.com/neo4j-php/Bolt
1515
* @package Bolt\tests\packstream\v1
1616
*/
17-
class UnpackerTest extends ATest
17+
class UnpackerTest extends TestLayer
1818
{
1919
public function testInit(): AProtocol
2020
{

0 commit comments

Comments
 (0)