Skip to content

Commit d3fdcc9

Browse files
Merge pull request #52 from neo4j-php/issue_51/chunking
Changed return of pack to Generator. Added chunking for write.
2 parents e724777 + 5eb60a9 commit d3fdcc9

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/PackStream/IPacker.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Bolt\PackStream;
44

5+
use Generator;
6+
57
/**
68
* Interface IPacker
79
*
@@ -14,7 +16,7 @@ interface IPacker
1416
/**
1517
* @param $signature
1618
* @param mixed ...$params
17-
* @return string
19+
* @return Generator
1820
*/
19-
public function pack($signature, ...$params): string;
21+
public function pack($signature, ...$params): Generator;
2022
}

src/PackStream/v1/Packer.php

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

55
use Bolt\PackStream\IPacker;
66
use Bolt\error\PackException;
7+
use Generator;
78

89
/**
910
* Class Packer of PackStream version 1
@@ -23,10 +24,10 @@ class Packer implements IPacker
2324
* Pack message with parameters
2425
* @param $signature
2526
* @param mixed ...$params
26-
* @return string
27+
* @return Generator
2728
* @throws PackException
2829
*/
29-
public function pack($signature, ...$params): string
30+
public function pack($signature, ...$params): Generator
3031
{
3132
$output = '';
3233

@@ -49,7 +50,13 @@ public function pack($signature, ...$params): string
4950
}
5051

5152
//structure buffer
52-
return pack('n', mb_strlen($output, '8bit')) . $output . chr(0x00) . chr(0x00);
53+
$len = mb_strlen($output, 'UTF-8');
54+
for ($i = 0; $i < $len; $i += 65535) {
55+
$chunk = mb_substr($output, $i, 65535, '8bit');
56+
yield pack('n', mb_strlen($chunk, '8bit')) . $chunk;
57+
}
58+
59+
yield chr(0x00) . chr(0x00);
5360
}
5461

5562
/**

src/protocol/AProtocol.php

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

33
namespace Bolt\protocol;
44

5-
use Bolt\Bolt;
5+
use Generator;
66
use Bolt\PackStream\{IPacker, IUnpacker};
77
use Bolt\connection\IConnection;
88
use Exception;
@@ -72,12 +72,13 @@ public function goodbye(...$args)
7272

7373
/**
7474
* Write to connection
75-
* @param string $buffer
75+
* @param Generator $generator
7676
* @throws Exception
7777
*/
78-
protected function write(string $buffer)
78+
protected function write(Generator $generator)
7979
{
80-
$this->connection->write($buffer);
80+
foreach ($generator as $buffer)
81+
$this->connection->write($buffer);
8182
}
8283

8384
/**

0 commit comments

Comments
 (0)