1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tarantool\Client\Packer; |
4
|
|
|
|
5
|
|
|
use MessagePack\BufferUnpacker; |
6
|
|
|
use MessagePack\Packer; |
|
|
|
|
7
|
|
|
use Tarantool\Client\Exception\Exception; |
8
|
|
|
use Tarantool\Client\IProto; |
9
|
|
|
use Tarantool\Client\Packer\Packer as ClientPacker; |
10
|
|
|
use Tarantool\Client\Request\Request; |
11
|
|
|
use Tarantool\Client\Response; |
12
|
|
|
|
13
|
|
|
class PurePacker implements ClientPacker |
14
|
|
|
{ |
15
|
|
|
private $packer; |
16
|
|
|
private $unpacker; |
17
|
59 |
|
|
18
|
|
|
public function __construct(Packer $packer = null, BufferUnpacker $unpacker = null) |
19
|
59 |
|
{ |
20
|
59 |
|
$this->packer = $packer ?: new Packer(); |
21
|
59 |
|
$this->unpacker = $unpacker ?: new BufferUnpacker(); |
22
|
|
|
} |
23
|
113 |
|
|
24
|
|
|
public function pack(Request $request, $sync = null) |
25
|
113 |
|
{ |
26
|
113 |
|
$content = $this->packer->packMapHeader(2). |
27
|
113 |
|
$this->packer->packInt(IProto::CODE). |
28
|
|
|
$this->packer->packInt($request->getType()). |
29
|
|
|
$this->packer->packInt(IProto::SYNC). |
30
|
113 |
|
$this->packer->packInt($sync); |
31
|
100 |
|
|
32
|
|
|
if (null !== $body = $request->getBody()) { |
33
|
|
|
$content .= $this->packer->packMap($body); |
34
|
113 |
|
} |
35
|
|
|
|
36
|
|
|
return PackUtils::packLength(strlen($content)).$content; |
37
|
108 |
|
} |
38
|
|
|
|
39
|
108 |
|
public function unpack($data) |
40
|
|
|
{ |
41
|
108 |
|
$this->unpacker->reset($data); |
42
|
2 |
|
|
43
|
|
|
try { |
44
|
|
|
$header = $this->unpacker->unpackMap(); |
45
|
106 |
|
$body = $this->unpacker->unpackMap(); |
46
|
|
|
} catch (\Exception $e) { |
47
|
106 |
|
throw new Exception('Unable to unpack data.', 0, $e); |
48
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
49
|
106 |
|
throw new Exception('Unable to unpack data.', 0, $e); |
50
|
23 |
|
} |
51
|
|
|
|
52
|
|
|
$code = $header[IProto::CODE]; |
53
|
86 |
|
|
54
|
|
|
if ($code >= Response::TYPE_ERROR) { |
55
|
|
|
throw new Exception($body[IProto::ERROR], $code & (Response::TYPE_ERROR - 1)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return new Response($header[IProto::SYNC], $body ? $body[IProto::DATA] : null); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
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: