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
|
52 |
|
public function register() |
33
|
|
|
{ |
34
|
|
|
$this->container->share(Bootstrap::class, function () { |
|
|
|
|
35
|
52 |
|
return $this->container->get(Mapper::class)->getBootstrap(); |
36
|
52 |
|
}); |
37
|
|
|
|
38
|
|
|
$this->getContainer()->share(Client::class, function () { |
|
|
|
|
39
|
52 |
|
$config = $this->getContainer()->get(Config::class); |
40
|
|
|
$params = [ |
41
|
52 |
|
'uri' => $config['tarantool.connection'], |
42
|
|
|
]; |
43
|
52 |
|
$client = Client::fromOptions(array_merge($config['tarantool.params'], $params)); |
44
|
|
|
try { |
45
|
52 |
|
$client->evaluate("box.session.su('admin')"); |
46
|
|
|
} catch (Exception $e) { |
|
|
|
|
47
|
|
|
} |
48
|
52 |
|
return $client; |
49
|
52 |
|
}); |
50
|
|
|
|
51
|
|
|
$this->getContainer()->share(Mapper::class, function () { |
|
|
|
|
52
|
52 |
|
$mapper = new Mapper($this->getContainer()->get(Client::class)); |
53
|
52 |
|
$filesystem = $this->getContainer()->get(Filesystem::class); |
54
|
|
|
|
55
|
52 |
|
$mapperCache = $filesystem->getPath('.cache/mapper-meta.php'); |
56
|
52 |
|
if (file_exists($mapperCache)) { |
57
|
|
|
$meta = include $mapperCache; |
58
|
|
|
$mapper->setMeta($meta); |
59
|
|
|
} |
60
|
|
|
|
61
|
52 |
|
$annotation = $mapper->getPlugin(Annotation::class); |
62
|
|
|
|
63
|
52 |
|
foreach ($filesystem->listClasses('Entity') as $class) { |
64
|
52 |
|
$annotation->register($class); |
65
|
|
|
} |
66
|
52 |
|
foreach ($filesystem->listClasses('Repository') as $class) { |
67
|
52 |
|
$annotation->register($class); |
68
|
|
|
} |
69
|
|
|
|
70
|
52 |
|
$mapper->getPlugin(Sequence::class); |
71
|
52 |
|
$mapper->getPlugin(Spy::class); |
72
|
|
|
|
73
|
52 |
|
$mapper->getPlugin(Temporal::class) |
74
|
52 |
|
->getAggregator() |
75
|
52 |
|
->setReferenceAggregation(false); |
76
|
|
|
|
77
|
52 |
|
$mapper->application = $this->getContainer(); |
|
|
|
|
78
|
|
|
|
79
|
52 |
|
$mapper->serviceName = $this->getContainer()->get(Service::class)->getName(); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$mapper->getPlugin(new class($mapper) extends Plugin { |
82
|
52 |
|
public function afterInstantiate(Entity $entity) : Entity |
83
|
|
|
{ |
84
|
52 |
|
$entity->app = $this->mapper->application; |
|
|
|
|
85
|
52 |
|
return $entity; |
86
|
|
|
} |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
return $mapper; |
90
|
|
|
}); |
91
|
|
|
|
92
|
|
|
$this->getContainer()->share(Schema::class, function () { |
|
|
|
|
93
|
|
|
return $this->getContainer()->get(Mapper::class)->getSchema(); |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
$this->getContainer()->share(Spy::class, function () { |
|
|
|
|
97
|
|
|
return $this->getContainer()->get(Mapper::class)->getPlugin(Spy::class); |
98
|
|
|
}); |
99
|
|
|
|
100
|
|
|
$this->getContainer()->share(Temporal::class, function () { |
|
|
|
|
101
|
|
|
return $this->getContainer()->get(Mapper::class)->getPlugin(Temporal::class); |
102
|
|
|
}); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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: