1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Basis\Provider; |
4
|
|
|
|
5
|
|
|
use Basis\Config; |
6
|
|
|
use Basis\Filesystem; |
7
|
|
|
use Basis\Service; |
8
|
|
|
use Exception; |
9
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider; |
10
|
|
|
use Tarantool\Client\Client; |
11
|
|
|
use Tarantool\Mapper\Bootstrap; |
12
|
|
|
use Tarantool\Mapper\Entity; |
13
|
|
|
use Tarantool\Mapper\Mapper; |
14
|
|
|
use Tarantool\Mapper\Plugin; |
15
|
|
|
use Tarantool\Mapper\Plugin\Annotation; |
16
|
|
|
use Tarantool\Mapper\Plugin\Sequence; |
17
|
|
|
use Tarantool\Mapper\Plugin\Spy; |
18
|
|
|
use Tarantool\Mapper\Plugin\Temporal; |
19
|
|
|
use Tarantool\Mapper\Schema; |
20
|
|
|
|
21
|
|
|
class TarantoolProvider extends AbstractServiceProvider |
22
|
|
|
{ |
23
|
|
|
protected $provides = [ |
24
|
|
|
Bootstrap::class, |
25
|
|
|
Client::class, |
26
|
|
|
Mapper::class, |
27
|
|
|
Schema::class, |
28
|
|
|
Spy::class, |
29
|
|
|
Temporal::class, |
30
|
|
|
]; |
31
|
|
|
|
32
|
56 |
|
public function register() |
33
|
|
|
{ |
34
|
|
|
$this->container->share(Bootstrap::class, function () { |
|
|
|
|
35
|
56 |
|
return $this->container->get(Mapper::class)->getBootstrap(); |
36
|
56 |
|
}); |
37
|
|
|
|
38
|
|
|
$this->getContainer()->share(Client::class, function () { |
|
|
|
|
39
|
56 |
|
$config = $this->getContainer()->get(Config::class); |
40
|
|
|
$params = [ |
41
|
56 |
|
'uri' => $config['tarantool.connection'], |
42
|
|
|
'persistent' => false, |
43
|
56 |
|
]; |
44
|
|
|
$client = Client::fromOptions(array_merge($config['tarantool.params'], $params)); |
45
|
56 |
|
try { |
46
|
|
|
$client->evaluate("box.session.su('admin')"); |
47
|
|
|
} catch (Exception $e) { |
|
|
|
|
48
|
56 |
|
} |
49
|
56 |
|
return $client; |
50
|
|
|
}); |
51
|
|
|
|
52
|
56 |
|
$this->getContainer()->share(Mapper::class, function () { |
|
|
|
|
53
|
56 |
|
$mapper = new Mapper($this->getContainer()->get(Client::class)); |
54
|
|
|
$filesystem = $this->getContainer()->get(Filesystem::class); |
55
|
56 |
|
|
56
|
56 |
|
$mapperCache = $filesystem->getPath('.cache/mapper-meta.php'); |
57
|
|
|
if (file_exists($mapperCache)) { |
58
|
|
|
$meta = include $mapperCache; |
59
|
|
|
$mapper->setMeta($meta); |
60
|
|
|
} |
61
|
56 |
|
|
62
|
|
|
$annotation = $mapper->getPlugin(Annotation::class); |
63
|
56 |
|
|
64
|
56 |
|
foreach ($filesystem->listClasses('Entity') as $class) { |
65
|
|
|
$annotation->register($class); |
66
|
56 |
|
} |
67
|
56 |
|
foreach ($filesystem->listClasses('Repository') as $class) { |
68
|
|
|
$annotation->register($class); |
69
|
|
|
} |
70
|
56 |
|
|
71
|
56 |
|
$mapper->getPlugin(Sequence::class); |
72
|
|
|
$mapper->getPlugin(Spy::class); |
73
|
56 |
|
|
74
|
56 |
|
$mapper->getPlugin(Temporal::class) |
75
|
56 |
|
->getAggregator() |
76
|
|
|
->setReferenceAggregation(false); |
77
|
56 |
|
|
78
|
|
|
$mapper->application = $this->getContainer(); |
|
|
|
|
79
|
56 |
|
|
80
|
|
|
$mapper->serviceName = $this->getContainer()->get(Service::class)->getName(); |
|
|
|
|
81
|
|
|
|
82
|
56 |
|
$mapper->getPlugin(new class($mapper) extends Plugin { |
83
|
|
|
public function afterInstantiate(Entity $entity) : Entity |
84
|
56 |
|
{ |
85
|
56 |
|
$entity->app = $this->mapper->application; |
|
|
|
|
86
|
|
|
return $entity; |
87
|
|
|
} |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
return $mapper; |
91
|
|
|
}); |
92
|
|
|
|
93
|
|
|
$this->getContainer()->share(Schema::class, function () { |
|
|
|
|
94
|
|
|
return $this->getContainer()->get(Mapper::class)->getSchema(); |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
$this->getContainer()->share(Spy::class, function () { |
|
|
|
|
98
|
|
|
return $this->getContainer()->get(Mapper::class)->getPlugin(Spy::class); |
99
|
|
|
}); |
100
|
|
|
|
101
|
|
|
$this->getContainer()->share(Temporal::class, function () { |
|
|
|
|
102
|
|
|
return $this->getContainer()->get(Mapper::class)->getPlugin(Temporal::class); |
103
|
|
|
}); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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: