|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Tarantool Client package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Packer; |
|
15
|
|
|
|
|
16
|
|
|
use MessagePack\BufferUnpacker; |
|
17
|
|
|
use MessagePack\Packer; |
|
|
|
|
|
|
18
|
|
|
use Tarantool\Client\Keys; |
|
19
|
|
|
use Tarantool\Client\Packer\Packer as ClientPacker; |
|
20
|
|
|
use Tarantool\Client\Request\Request; |
|
21
|
|
|
use Tarantool\Client\Response; |
|
22
|
|
|
|
|
23
|
|
|
final class PurePacker implements ClientPacker |
|
24
|
|
|
{ |
|
25
|
|
|
private $packer; |
|
26
|
|
|
private $unpacker; |
|
27
|
|
|
|
|
28
|
138 |
|
public function __construct(?Packer $packer = null, ?BufferUnpacker $unpacker = null) |
|
29
|
|
|
{ |
|
30
|
138 |
|
$this->packer = $packer ?: new Packer(); |
|
31
|
138 |
|
$this->unpacker = $unpacker ?: new BufferUnpacker(); |
|
32
|
138 |
|
} |
|
33
|
|
|
|
|
34
|
114 |
|
public function pack(Request $request, int $sync) : string |
|
35
|
|
|
{ |
|
36
|
114 |
|
$packet = $this->packer->packMapHeader(2). |
|
37
|
114 |
|
$this->packer->packInt(Keys::CODE). |
|
38
|
114 |
|
$this->packer->packInt($request->getType()). |
|
39
|
114 |
|
$this->packer->packInt(Keys::SYNC). |
|
40
|
114 |
|
$this->packer->packInt($sync). |
|
41
|
114 |
|
$this->packer->packMap($request->getBody()); |
|
42
|
|
|
|
|
43
|
114 |
|
return PacketLength::pack(\strlen($packet)).$packet; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
99 |
|
public function unpack(string $data) : Response |
|
47
|
|
|
{ |
|
48
|
99 |
|
$unpacker = $this->unpacker->withBuffer($data); |
|
49
|
|
|
|
|
50
|
99 |
|
return new Response($unpacker->unpackMap(), |
|
51
|
|
|
static function () use ($unpacker) { |
|
52
|
92 |
|
return $unpacker->unpackMap(); |
|
53
|
97 |
|
} |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: