|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Basis\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Basis\Service; |
|
6
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
|
7
|
|
|
use Tarantool\Client\Client; |
|
8
|
|
|
use Tarantool\Mapper\Mapper; |
|
9
|
|
|
use Tarantool\Mapper\Plugin\Sequence; |
|
10
|
|
|
use Tarantool\Mapper\Plugin\Spy; |
|
11
|
|
|
use Tarantool\Mapper\Plugin\Temporal; |
|
12
|
|
|
use Tarantool\Mapper\Pool; |
|
13
|
|
|
|
|
14
|
|
|
class PoolProvider extends AbstractServiceProvider |
|
15
|
|
|
{ |
|
16
|
|
|
protected $provides = [ |
|
17
|
|
|
Pool::class, |
|
18
|
|
|
]; |
|
19
|
|
|
|
|
20
|
|
|
public function register() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->getContainer()->share(Pool::class, function () { |
|
|
|
|
|
|
23
|
|
|
$pool = new Pool(); |
|
24
|
|
|
$container = $this->getContainer(); |
|
25
|
|
|
$pool->registerResolver(function ($name) use ($container) { |
|
26
|
|
|
|
|
27
|
|
|
if ($name == 'default' || $name == $container->get(Service::class)->getName()) { |
|
28
|
|
|
$mapper = $container->get(Mapper::class); |
|
29
|
|
|
$mapper->serviceName = $container->get(Service::class)->getName(); |
|
30
|
|
|
return $mapper; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$service = $container->get(Service::class); |
|
34
|
|
|
|
|
35
|
|
|
if (in_array($name, $service->listServices())) { |
|
36
|
|
|
$address = $service->getHost($name.'-db')->address; |
|
37
|
|
|
$client = Client::fromDsn('tcp://'.$address.':3301'); |
|
38
|
|
|
$client->evaluate("box.session.su('admin')"); |
|
39
|
|
|
$mapper = new Mapper($client); |
|
40
|
|
|
$mapper->getPlugin(Sequence::class); |
|
41
|
|
|
$mapper->getPlugin(Spy::class); |
|
42
|
|
|
$mapper->getPlugin(Temporal::class) |
|
43
|
|
|
->getAggregator() |
|
44
|
|
|
->setReferenceAggregation(false); |
|
45
|
|
|
|
|
46
|
|
|
$mapper->serviceName = $name; |
|
|
|
|
|
|
47
|
|
|
return $mapper; |
|
48
|
|
|
} |
|
49
|
|
|
}); |
|
50
|
|
|
|
|
51
|
|
|
return $pool; |
|
52
|
|
|
}); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: