1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis\Provider; |
4
|
|
|
|
5
|
|
|
use Basis\Service; |
6
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
7
|
|
|
use Tarantool\Client\Connection\StreamConnection; |
8
|
|
|
use Tarantool\Client\Packer\PurePacker; |
9
|
|
|
use Tarantool\Client\Request\DeleteRequest; |
10
|
|
|
use Tarantool\Client\Request\InsertRequest; |
11
|
|
|
use Tarantool\Client\Request\ReplaceRequest; |
12
|
|
|
use Tarantool\Client\Request\UpdateRequest; |
13
|
|
|
use Tarantool\Mapper\Client; |
14
|
|
|
use Tarantool\Mapper\Mapper; |
15
|
|
|
use Tarantool\Mapper\Plugin; |
16
|
|
|
use Tarantool\Mapper\Plugin\Spy; |
17
|
|
|
use Tarantool\Mapper\Plugin\Temporal; |
18
|
|
|
use Tarantool\Mapper\Pool; |
19
|
|
|
use Tarantool\Mapper\Schema; |
20
|
|
|
|
21
|
|
|
class PoolProvider extends AbstractServiceProvider |
22
|
|
|
{ |
23
|
|
|
protected $provides = [ |
24
|
|
|
Pool::class, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
public function register() |
28
|
|
|
{ |
29
|
|
|
$this->getContainer()->share(Pool::class, function () { |
30
|
|
|
$container = $this->getContainer(); |
31
|
|
|
|
32
|
|
|
$pool = new Pool(); |
33
|
|
|
$pool->register('default', $container->get(Mapper::class)); |
34
|
|
|
|
35
|
|
|
$service = $container->get(Service::class); |
36
|
|
|
$pool->register($service->getName(), $container->get(Mapper::class)); |
37
|
|
|
|
38
|
|
|
$pool->registerResolver(function ($name) use ($service) { |
39
|
|
|
if (in_array($name, $service->listServices())) { |
40
|
|
|
$connection = new StreamConnection('tcp://'.$name.'-db:3301'); |
41
|
|
|
$packer = new PurePacker(); |
42
|
|
|
$client = new Client($connection, $packer); |
43
|
|
|
$client->disableRequest(DeleteRequest::class); |
44
|
|
|
$client->disableRequest(InsertRequest::class); |
45
|
|
|
$client->disableRequest(ReplaceRequest::class); |
46
|
|
|
$client->disableRequest(UpdateRequest::class); |
47
|
|
|
$mapper = new Mapper($client); |
48
|
|
|
$mapper->getPlugin(Temporal::class); |
49
|
|
|
$mapper->getPlugin(Spy::class); |
50
|
|
|
return $mapper; |
51
|
|
|
} |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
return $pool; |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|