1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tarantool\Mapper\Plugin; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use Tarantool\Mapper\Plugin; |
8
|
|
|
use Tarantool\Mapper\Space; |
9
|
|
|
use Tarantool\Mapper\Entity; |
10
|
|
|
|
11
|
|
|
class Compute extends Plugin |
12
|
|
|
{ |
13
|
|
|
protected $dependency = []; |
14
|
|
|
protected $entities = []; |
15
|
|
|
|
16
|
|
|
public function afterCreate(Entity $entity, Space $space) |
17
|
|
|
{ |
18
|
|
|
$name = $space->getName(); |
19
|
|
|
|
20
|
|
|
if (array_key_exists($name, $this->dependency)) { |
21
|
|
|
foreach ($this->dependency[$name] as [$target, $callback]) { |
22
|
|
|
$this->initializePresenter($target, $callback, $entity); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function afterRemove(Entity $entity, Space $space) |
28
|
|
|
{ |
29
|
|
|
$name = $space->getName(); |
30
|
|
|
|
31
|
|
|
if (array_key_exists($name, $this->dependency)) { |
32
|
|
|
foreach ($this->dependency[$name] as [$target, $callback]) { |
33
|
|
|
$this->getMapper()->remove($target, ['id' => $entity->id]); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function afterUpdate(Entity $entity, Space $space) |
39
|
|
|
{ |
40
|
|
|
$name = $space->getName(); |
41
|
|
|
|
42
|
|
|
if (array_key_exists($name, $this->dependency)) { |
43
|
|
|
foreach ($this->dependency[$name] as [$target, $callback]) { |
44
|
|
|
$child = $this->getMapper()->findOne($target, $entity->id); |
|
|
|
|
45
|
|
|
foreach ($callback($entity) as $k => $v) { |
|
|
|
|
46
|
|
|
$child->$k = $v; |
47
|
|
|
} |
48
|
|
|
$child->save(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function beforeCreate(Entity $entity, Space $space) |
54
|
|
|
{ |
55
|
|
|
if (in_array($entity, $this->entities)) { |
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
foreach ($this->dependency as $source => $dependencies) { |
59
|
|
|
foreach ($dependencies as [$target]) { |
60
|
|
|
if ($target == $space->getName()) { |
|
|
|
|
61
|
|
|
throw new Exception("Space $target is computed from $source"); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function register($source, $target, $callback) |
68
|
|
|
{ |
69
|
|
|
if ($callback instanceof Closure) { |
70
|
|
|
if (!array_key_exists($source, $this->dependency)) { |
71
|
|
|
$this->dependency[$source] = []; |
72
|
|
|
} |
73
|
|
|
$this->dependency[$source][] = [$target, $callback]; |
74
|
|
|
foreach ($this->getMapper()->find($source) as $entity) { |
75
|
|
|
$this->initializePresenter($target, $callback, $entity); |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
|
|
$this->registerLuaTriggers($source, $target, $callback); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function initializePresenter($target, $callback, Entity $source) |
83
|
|
|
{ |
84
|
|
|
$entity = $this->getMapper()->getRepository($target)->create($source->id); |
85
|
|
|
foreach ($callback($source) as $k => $v) { |
86
|
|
|
$entity->$k = $v; |
87
|
|
|
} |
88
|
|
|
$this->entities[] = $entity; |
89
|
|
|
$entity->save(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function registerLuaTriggers($source, $destination, $body) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
throw new Exception("Lua triggers not implemented"); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.