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 MessagePack\TypeTransformer\Extension; |
19
|
|
|
use Tarantool\Client\Keys; |
20
|
|
|
use Tarantool\Client\Packer\Packer as ClientPacker; |
21
|
|
|
use Tarantool\Client\Request\Request; |
22
|
|
|
use Tarantool\Client\Response; |
23
|
|
|
|
24
|
|
|
final class PurePacker implements ClientPacker |
25
|
|
|
{ |
26
|
|
|
private $packer; |
27
|
|
|
private $unpacker; |
28
|
|
|
|
29
|
153 |
|
public function __construct(?Packer $packer = null, ?BufferUnpacker $unpacker = null) |
30
|
|
|
{ |
31
|
153 |
|
$this->packer = $packer ?: new Packer(); |
32
|
153 |
|
$this->unpacker = $unpacker ?: new BufferUnpacker(); |
33
|
153 |
|
} |
34
|
|
|
|
35
|
15 |
|
public static function fromExtensions(Extension $extension, Extension ...$extensions) : self |
36
|
|
|
{ |
37
|
15 |
|
$extensions = [-1 => $extension] + $extensions; |
38
|
|
|
|
39
|
15 |
|
return new self( |
40
|
15 |
|
new Packer(null, $extensions), |
41
|
15 |
|
new BufferUnpacker('', null, $extensions) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
128 |
|
public function pack(Request $request, int $sync) : string |
46
|
|
|
{ |
47
|
128 |
|
$packet = $this->packer->packMapHeader(2). |
48
|
128 |
|
$this->packer->packInt(Keys::CODE). |
49
|
128 |
|
$this->packer->packInt($request->getType()). |
50
|
128 |
|
$this->packer->packInt(Keys::SYNC). |
51
|
128 |
|
$this->packer->packInt($sync). |
52
|
128 |
|
$this->packer->packMap($request->getBody()); |
53
|
|
|
|
54
|
128 |
|
return PacketLength::pack(\strlen($packet)).$packet; |
55
|
|
|
} |
56
|
|
|
|
57
|
113 |
|
public function unpack(string $packet) : Response |
58
|
|
|
{ |
59
|
113 |
|
$this->unpacker->reset($packet); |
60
|
|
|
|
61
|
113 |
|
return new Response( |
62
|
113 |
|
$this->unpacker->unpackMap(), |
63
|
111 |
|
$this->unpacker->unpackMap() |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: