|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper; |
|
4
|
|
|
|
|
5
|
|
|
use Tarantool\Client\Client; |
|
|
|
|
|
|
6
|
|
|
use Exception; |
|
7
|
|
|
|
|
8
|
|
|
class Mapper |
|
9
|
|
|
{ |
|
10
|
|
|
private $client; |
|
11
|
|
|
private $plugins = []; |
|
12
|
|
|
private $schema; |
|
13
|
|
|
private $bootstrap; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(Client $client) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->client = $client; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function addPlugin($class) |
|
21
|
|
|
{ |
|
22
|
|
|
if (!is_subclass_of($class, Plugin::class)) { |
|
|
|
|
|
|
23
|
|
|
throw new Exception("Plugin should extend " . Plugin::class . " class"); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$plugin = is_object($class) ? $class : new $class($this); |
|
27
|
|
|
$this->plugins[get_class($plugin)] = $plugin; |
|
28
|
|
|
|
|
29
|
|
|
return $plugin; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function create($space, $data) |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->getRepository($space)->create($data)->save(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function findOne($space, $params = []) |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->getRepository($space)->findOne($params); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function findOrCreate($space, $params = []) |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->getRepository($space)->findOrCreate($params)->save(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function find($space, $params = []) |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->getRepository($space)->find($params); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function findRepository(Entity $instance) |
|
53
|
|
|
{ |
|
54
|
|
|
foreach ($this->getSchema()->getSpaces() as $space) { |
|
55
|
|
|
if ($space->getRepository()->knows($instance)) { |
|
56
|
|
|
return $space->getRepository(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
throw new Exception("No Repository for given Entity"); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getBootstrap() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->bootstrap ?: $this->bootstrap = new Bootstrap($this); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getClient() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->client; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getPlugin($class) |
|
74
|
|
|
{ |
|
75
|
|
|
if (!array_key_exists($class, $this->plugins)) { |
|
76
|
|
|
throw new Exception("No plugin $class"); |
|
77
|
|
|
} |
|
78
|
|
|
return $this->plugins[$class]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function hasPlugin($class) |
|
82
|
|
|
{ |
|
83
|
|
|
return array_key_exists($class, $this->plugins); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getPlugins() |
|
87
|
|
|
{ |
|
88
|
|
|
return array_values($this->plugins); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getRepository($space) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->getSchema()->getSpace($space)->getRepository(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getRepositories() |
|
97
|
|
|
{ |
|
98
|
|
|
$repositories = []; |
|
99
|
|
|
foreach ($this->getSchema()->getSpaces() as $space) { |
|
100
|
|
|
if ($space->repositoryExists()) { |
|
101
|
|
|
$repositories[] = $space->getRepository(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
return $repositories; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getSchema() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->schema ?: $this->schema = new Schema($this); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function remove($space, $params = []) |
|
113
|
|
|
{ |
|
114
|
|
|
if ($space instanceof Entity) { |
|
115
|
|
|
$this->findRepository($space)->removeEntity($space); |
|
116
|
|
|
} else { |
|
117
|
|
|
$this->getRepository($space)->remove($params); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function save(Entity $instance) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->findRepository($instance)->save($instance); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: