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 getPlugin($mixed) |
21
|
|
|
{ |
22
|
|
|
if (!is_subclass_of($mixed, Plugin::class)) { |
|
|
|
|
23
|
|
|
throw new Exception("Plugin should extend " . Plugin::class . " class"); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$plugin = is_object($mixed) ? $mixed : new $mixed($this); |
27
|
|
|
$class = get_class($plugin); |
28
|
|
|
|
29
|
|
|
if ($plugin == $mixed && array_key_exists($class, $this->plugins)) { |
30
|
|
|
// overwrite plugin instance |
31
|
|
|
throw new Exception($class.' is registered'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (!array_key_exists($class, $this->plugins)) { |
35
|
|
|
$this->plugins[$class] = $plugin; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->plugins[$class]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function create($space, $data) |
42
|
|
|
{ |
43
|
|
|
return $this->getRepository($space)->create($data)->save(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function findOne($space, $params = []) |
47
|
|
|
{ |
48
|
|
|
return $this->getRepository($space)->findOne($params); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function findOrCreate($space, $params = []) |
52
|
|
|
{ |
53
|
|
|
return $this->getRepository($space)->findOrCreate($params)->save(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function findOrFail($space, $params = []) |
57
|
|
|
{ |
58
|
|
|
return $this->getRepository($space)->findOrFail($params)->save(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function find($space, $params = []) |
62
|
|
|
{ |
63
|
|
|
return $this->getRepository($space)->find($params); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function findRepository(Entity $instance) |
67
|
|
|
{ |
68
|
|
|
foreach ($this->getSchema()->getSpaces() as $space) { |
69
|
|
|
if ($space->getRepository()->knows($instance)) { |
70
|
|
|
return $space->getRepository(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
throw new Exception("No Repository for given Entity"); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getBootstrap() |
78
|
|
|
{ |
79
|
|
|
return $this->bootstrap ?: $this->bootstrap = new Bootstrap($this); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getClient() |
83
|
|
|
{ |
84
|
|
|
return $this->client; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getMeta() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
'schema' => $this->getSchema()->getMeta(), |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function hasPlugin($class) |
95
|
|
|
{ |
96
|
|
|
return array_key_exists($class, $this->plugins); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getPlugins() |
100
|
|
|
{ |
101
|
|
|
return array_values($this->plugins); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getRepository($space) |
105
|
|
|
{ |
106
|
|
|
return $this->getSchema()->getSpace($space)->getRepository(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getRepositories() |
110
|
|
|
{ |
111
|
|
|
$repositories = []; |
112
|
|
|
foreach ($this->getSchema()->getSpaces() as $space) { |
113
|
|
|
if ($space->repositoryExists()) { |
114
|
|
|
$repositories[] = $space->getRepository(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
return $repositories; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getSchema() |
121
|
|
|
{ |
122
|
|
|
return $this->schema ?: $this->schema = new Schema($this); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function remove($space, $params = []) |
126
|
|
|
{ |
127
|
|
|
if ($space instanceof Entity) { |
128
|
|
|
$this->findRepository($space)->removeEntity($space); |
129
|
|
|
} else { |
130
|
|
|
$this->getRepository($space)->remove($params); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function save(Entity $instance) |
135
|
|
|
{ |
136
|
|
|
$this->findRepository($instance)->save($instance); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function setMeta($meta) |
140
|
|
|
{ |
141
|
|
|
if ($this->schema) { |
142
|
|
|
$this->schema->setMeta($meta['schema']); |
|
|
|
|
143
|
|
|
} else { |
144
|
|
|
$this->schema = new Schema($this, $meta['schema']); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: