1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Tarantool Client package. |
7
|
|
|
* |
8
|
|
|
* (c) Eugene Leonovich <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Packer; |
15
|
|
|
|
16
|
|
|
use MessagePack\BufferUnpacker; |
17
|
|
|
use MessagePack\Exception\UnpackingFailedException; |
18
|
|
|
use MessagePack\Packer; |
|
|
|
|
19
|
|
|
use Tarantool\Client\Exception\UnpackingFailed; |
20
|
|
|
use Tarantool\Client\IProto; |
21
|
|
|
use Tarantool\Client\Packer\Packer as ClientPacker; |
22
|
|
|
use Tarantool\Client\Request\Request; |
23
|
|
|
use Tarantool\Client\Response; |
24
|
|
|
|
25
|
|
|
final class PurePacker implements ClientPacker |
26
|
|
|
{ |
27
|
|
|
private $packer; |
28
|
|
|
private $unpacker; |
29
|
|
|
|
30
|
131 |
|
public function __construct(?Packer $packer = null, ?BufferUnpacker $unpacker = null) |
31
|
|
|
{ |
32
|
131 |
|
$this->packer = $packer ?: new Packer(); |
33
|
131 |
|
$this->unpacker = $unpacker ?: new BufferUnpacker(); |
34
|
131 |
|
} |
35
|
|
|
|
36
|
106 |
|
public function pack(Request $request, ?int $sync = null) : string |
37
|
|
|
{ |
38
|
106 |
|
$content = $this->packer->packMapHeader(2). |
39
|
106 |
|
$this->packer->packInt(IProto::CODE). |
40
|
106 |
|
$this->packer->packInt($request->getType()). |
41
|
106 |
|
$this->packer->packInt(IProto::SYNC). |
42
|
106 |
|
$this->packer->packInt($sync ?: 0). |
43
|
106 |
|
$this->packer->packMap($request->getBody()); |
44
|
|
|
|
45
|
106 |
|
return PackUtils::packLength(\strlen($content)).$content; |
46
|
|
|
} |
47
|
|
|
|
48
|
105 |
|
public function unpack(string $data) : Response |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
105 |
|
$this->unpacker->reset($data); |
52
|
|
|
|
53
|
105 |
|
return new Response( |
54
|
105 |
|
$this->unpacker->unpackMap(), |
55
|
103 |
|
$this->unpacker->unpackMap() |
56
|
|
|
); |
57
|
2 |
|
} catch (UnpackingFailedException $e) { |
58
|
2 |
|
throw new UnpackingFailed('Unable to unpack response.', 0, $e); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
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: