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