Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved usage of temp directory. #141

Merged
merged 3 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Bolt.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,25 @@ final class Bolt

public function __construct(private IConnection $connection)
{
if (!file_exists(getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR)) {
mkdir(getcwd() . DIRECTORY_SEPARATOR . 'temp');
$_ENV['TEMP_DIR'] = getenv('TEMP') ?: getenv('TMPDIR') ?: (dirname(__DIR__) . DIRECTORY_SEPARATOR . 'temp');
var_dump($_ENV['TEMP_DIR']);
if (!file_exists($_ENV['TEMP_DIR'])) {
mkdir($_ENV['TEMP_DIR'], recursive: true);
}

if (!getenv('BOLT_ANALYTICS_OPTOUT')) {
if (!file_exists($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR)) {
mkdir($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics');
}
$this->track();
}

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

private function track(): void
{
foreach (glob(getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'queries.*.cnt') as $file) {
foreach (glob($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.*.cnt') as $file) {
$time = intval(explode('.', basename($file))[1]);
if ($time < strtotime('today')) {
$count = file_get_contents($file);
Expand Down
7 changes: 3 additions & 4 deletions src/helpers/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ class FileCache implements CacheInterface

public function __construct()
{
$this->tempDir = getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR;

$this->tempDir = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-filecache' . DIRECTORY_SEPARATOR;
if (!file_exists($this->tempDir)) {
mkdir(rtrim($this->tempDir, DIRECTORY_SEPARATOR));
mkdir($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-filecache');
}

// clean old
foreach (scandir($this->tempDir . 'temp') as $file) {
foreach (scandir($this->tempDir) as $file) {
if ($file == '.' || $file == '..')
continue;
if (filemtime($this->tempDir . $file) < strtotime('-1 hour'))
Expand Down
2 changes: 1 addition & 1 deletion src/protocol/AProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function write(iterable $generator): void

private function track(): void
{
$file = getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt';
$file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt';
$count = file_exists($file) ? intval(file_get_contents($file)) : 0;
file_put_contents($file, $count + 1);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/BoltTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests
*/
class BoltTest extends ATest
class BoltTest extends TestLayer
{
public function testSockets(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/PerformanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests
*/
class PerformanceTest extends ATest
class PerformanceTest extends TestLayer
{
public function test50KRecords(): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/ATest.php → tests/TestLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Class ATest
* @package Bolt\tests
*/
class ATest extends \PHPUnit\Framework\TestCase
abstract class TestLayer extends \PHPUnit\Framework\TestCase
{
public static function setUpBeforeClass(): void
{
Expand Down
4 changes: 2 additions & 2 deletions tests/connection/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Bolt\tests\connection;

use Bolt\Bolt;
use Bolt\tests\ATest;
use Bolt\tests\TestLayer;
use Bolt\connection\{
IConnection,
Socket,
Expand All @@ -18,7 +18,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\connection
*/
final class ConnectionTest extends ATest
final class ConnectionTest extends TestLayer
{
public function provideConnections(): array
{
Expand Down
4 changes: 2 additions & 2 deletions tests/packstream/v1/BytesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
use Bolt\Bolt;
use Bolt\packstream\Bytes;
use Bolt\protocol\AProtocol;
use Bolt\tests\ATest;
use Bolt\tests\TestLayer;

/**
* Class BytesTest
* @package Bolt\tests\packstream\v1
*/
class BytesTest extends ATest
class BytesTest extends TestLayer
{
public function testInit(): AProtocol
{
Expand Down
4 changes: 2 additions & 2 deletions tests/packstream/v1/PackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Bolt\Bolt;
use Bolt\protocol\AProtocol;
use Bolt\tests\ATest;
use Bolt\tests\TestLayer;

/**
* Class PackerTest
Expand All @@ -13,7 +13,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\packstream\v1
*/
class PackerTest extends ATest
class PackerTest extends TestLayer
{
public function testInit(): AProtocol
{
Expand Down
4 changes: 2 additions & 2 deletions tests/packstream/v1/UnpackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Bolt\Bolt;
use Bolt\protocol\{AProtocol, Response};
use Bolt\tests\ATest;
use Bolt\tests\TestLayer;
use Bolt\enum\Signature;

/**
Expand All @@ -14,7 +14,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\packstream\v1
*/
class UnpackerTest extends ATest
class UnpackerTest extends TestLayer
{
public function testInit(): AProtocol
{
Expand Down
13 changes: 10 additions & 3 deletions tests/protocol/ATest.php → tests/protocol/ProtocolLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests
*/
abstract class ATest extends TestCase
abstract class ProtocolLayer extends TestCase
{
/**
* @var string Temporal buffer for packed message to be read
Expand Down Expand Up @@ -100,8 +100,15 @@ public function readCallback(int $length = 2048): string
*/
protected function setUp(): void
{
if (!file_exists(getcwd() . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR)) {
mkdir(getcwd() . DIRECTORY_SEPARATOR . 'temp');
$_ENV['TEMP_DIR'] = getenv('TEMP') ?: getenv('TMPDIR') ?: (dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'temp');
if (!file_exists($_ENV['TEMP_DIR'])) {
mkdir($_ENV['TEMP_DIR'], recursive: true);
}

if (!getenv('BOLT_ANALYTICS_OPTOUT')) {
if (!file_exists($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR)) {
mkdir($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics');
}
}

self::$readBuffer = '';
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol/V1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V1Test extends ATest
class V1Test extends ProtocolLayer
{
public function test__construct(): V1
{
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol/V2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V2Test extends ATest
class V2Test extends ProtocolLayer
{
public function test__construct(): V2
{
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol/V3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V3Test extends ATest
class V3Test extends ProtocolLayer
{
public function test__construct(): V3
{
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol/V4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V4Test extends ATest
class V4Test extends ProtocolLayer
{
public function test__construct(): V4
{
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol/V4_1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V4_1Test extends ATest
class V4_1Test extends ProtocolLayer
{
public function test__construct(): V4_1
{
Expand Down
3 changes: 1 addition & 2 deletions tests/protocol/V4_2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Bolt\tests\protocol;

use Bolt\protocol\V4_2;
use Bolt\packstream\v1\{Packer, Unpacker};

/**
* Class V4_2Test
Expand All @@ -12,7 +11,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V4_2Test extends ATest
class V4_2Test extends ProtocolLayer
{
public function test__construct(): V4_2
{
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol/V4_3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V4_3Test extends ATest
class V4_3Test extends ProtocolLayer
{
public function test__construct(): V4_3
{
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol/V4_4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V4_4Test extends ATest
class V4_4Test extends ProtocolLayer
{
public function test__construct(): V4_4
{
Expand Down
3 changes: 1 addition & 2 deletions tests/protocol/V5Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Bolt\tests\protocol;

use Bolt\protocol\V5;
use Bolt\packstream\v1\{Packer, Unpacker};

/**
* Class V5Test
Expand All @@ -12,7 +11,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V5Test extends ATest
class V5Test extends ProtocolLayer
{
public function test__construct(): V5
{
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol/V5_1Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V5_1Test extends \Bolt\tests\protocol\ATest
class V5_1Test extends \Bolt\tests\protocol\ProtocolLayer
{
public function test__construct(): V5_1
{
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol/V5_2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V5_2Test extends \Bolt\tests\protocol\ATest
class V5_2Test extends \Bolt\tests\protocol\ProtocolLayer
{
public function test__construct(): V5_2
{
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol/V5_3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V5_3Test extends \Bolt\tests\protocol\ATest
class V5_3Test extends \Bolt\tests\protocol\ProtocolLayer
{
public function test__construct(): V5_3
{
Expand Down
2 changes: 1 addition & 1 deletion tests/protocol/V5_4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class V5_4Test extends \Bolt\tests\protocol\ATest
class V5_4Test extends \Bolt\tests\protocol\ProtocolLayer
{
public function test__construct(): V5_4
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Bolt\tests\structures;

use Bolt\tests\ATest;
use Bolt\tests\TestLayer;
use Exception;

/**
Expand All @@ -11,7 +11,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol
*/
class AStructures extends ATest
class StructureLayer extends TestLayer
{
/**
* How many iterations do for each date/time test
Expand Down
2 changes: 1 addition & 1 deletion tests/structures/v1/StructuresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests
*/
class StructuresTest extends \Bolt\tests\structures\AStructures
class StructuresTest extends \Bolt\tests\structures\StructureLayer
{
public function testInit(): AProtocol|V4_4|V4_3|V4_2|V3
{
Expand Down
2 changes: 1 addition & 1 deletion tests/structures/v4_3/StructuresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol\v4_3
*/
class StructuresTest extends \Bolt\tests\structures\AStructures
class StructuresTest extends \Bolt\tests\structures\StructureLayer
{
public function testInit(): AProtocol|V4_4|V4_3
{
Expand Down
2 changes: 1 addition & 1 deletion tests/structures/v5/StructuresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @link https://github.com/neo4j-php/Bolt
* @package Bolt\tests\protocol\v5
*/
class StructuresTest extends \Bolt\tests\structures\AStructures
class StructuresTest extends \Bolt\tests\structures\StructureLayer
{
public function testInit(): AProtocol|V4_3|V4_4|V5|V5_1
{
Expand Down
Loading