1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Basis\Providers;
|
4
|
|
|
|
5
|
|
|
use Basis\Config;
|
6
|
|
|
use Basis\Filesystem;
|
7
|
|
|
use League\Container\ServiceProvider\AbstractServiceProvider;
|
8
|
|
|
use Tarantool\Client as TarantoolClient;
|
9
|
|
|
use Tarantool\Connection\Connection;
|
10
|
|
|
use Tarantool\Connection\SocketConnection;
|
11
|
|
|
use Tarantool\Mapper\Client;
|
12
|
|
|
use Tarantool\Mapper\Manager;
|
13
|
|
|
use Tarantool\Mapper\Migrations\Migrator;
|
14
|
|
|
use Tarantool\Mapper\Schema\Meta;
|
15
|
|
|
use Tarantool\Mapper\Schema\Schema;
|
16
|
|
|
use Tarantool\Packer\Packer;
|
17
|
|
|
use Tarantool\Packer\PurePacker;
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class Tarantool extends AbstractServiceProvider
|
21
|
|
|
{
|
22
|
|
|
protected $provides = [
|
23
|
|
|
Client::class,
|
24
|
|
|
Connection::class,
|
25
|
|
|
Manager::class,
|
26
|
|
|
Migrator::class,
|
27
|
|
|
Meta::class,
|
28
|
|
|
Packer::class,
|
29
|
|
|
Schema::class,
|
30
|
|
|
TarantoolClient::class,
|
31
|
|
|
];
|
32
|
|
|
|
33
|
2 |
|
public function register()
|
34
|
|
|
{
|
35
|
|
|
$this->getContainer()->share(Client::class, function () {
|
36
|
1 |
|
return new Client(
|
37
|
1 |
|
$this->getContainer()->get(Connection::class),
|
38
|
1 |
|
$this->getContainer()->get(Packer::class)
|
39
|
1 |
|
);
|
40
|
2 |
|
});
|
41
|
|
|
|
42
|
|
|
$this->getContainer()->share(Connection::class, function () {
|
43
|
1 |
|
$config = $this->getContainer()->get(Config::class);
|
44
|
1 |
|
return new SocketConnection(
|
45
|
1 |
|
$config['tarantool.host'],
|
46
|
1 |
|
$config['tarantool.port']
|
47
|
1 |
|
);
|
48
|
2 |
|
});
|
49
|
|
|
|
50
|
|
|
$this->getContainer()->share(Meta::class, function () {
|
51
|
|
|
return $this->getContainer()->get(Manager::class)->getMeta();
|
52
|
2 |
|
});
|
53
|
|
|
|
54
|
|
|
$this->getContainer()->share(Manager::class, function () {
|
55
|
1 |
|
return new Manager(
|
56
|
1 |
|
$this->getContainer()->get(Client::class)
|
57
|
1 |
|
);
|
58
|
2 |
|
});
|
59
|
|
|
|
60
|
|
|
$this->container->share(Migrator::class, function() {
|
61
|
1 |
|
$migrator = new Migrator();
|
62
|
1 |
|
$fs = $this->container->get(Filesystem::class);
|
63
|
1 |
|
foreach($fs->listFiles('resources/migrations') as $path) {
|
64
|
1 |
|
list($ym, $filename) = explode('/', $path);
|
65
|
1 |
|
$namespace = date_create_from_format('Ym', $ym)->format('FY');
|
66
|
1 |
|
$class = $namespace.'\\'.substr($filename, 0, -4);
|
67
|
1 |
|
if(!class_exists($class, false)) {
|
68
|
1 |
|
include $fs->getPath('resources/migrations/'.$path);
|
69
|
1 |
|
}
|
70
|
1 |
|
$migrator->registerMigration($class);
|
71
|
1 |
|
}
|
72
|
1 |
|
return $migrator;
|
73
|
2 |
|
});
|
74
|
|
|
|
75
|
|
|
$this->getContainer()->share(Packer::class, function () {
|
76
|
1 |
|
return new PurePacker();
|
77
|
2 |
|
});
|
78
|
|
|
|
79
|
|
|
$this->getContainer()->share(Schema::class, function () {
|
80
|
|
|
return $this->getContainer()->get(Manager::class)->getSchema();
|
81
|
2 |
|
});
|
82
|
|
|
|
83
|
2 |
|
$this->getContainer()->share(TarantoolClient::class, function() {
|
84
|
|
|
return $this->getContainer()->get(Client::class);
|
85
|
2 |
|
});
|
86
|
|
|
|
87
|
|
|
}
|
88
|
|
|
} |