1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2014, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Event\Listener; |
10
|
|
|
|
11
|
|
|
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Keeper; |
12
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
13
|
|
|
|
14
|
|
|
class DoctrineListener |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Keeper |
18
|
|
|
*/ |
19
|
|
|
protected $keeper; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $track_individually_entity; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Keeper $keeper |
28
|
|
|
* @param bool $track_individually_entity |
29
|
|
|
*/ |
30
|
21 |
|
public function __construct(Keeper $keeper, $track_individually_entity) |
31
|
|
|
{ |
32
|
21 |
|
$this->keeper = $keeper; |
33
|
21 |
|
$this->track_individually_entity = $track_individually_entity; |
34
|
21 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param LifecycleEventArgs $args |
38
|
|
|
*/ |
39
|
7 |
|
public function postPersist(LifecycleEventArgs $args) |
40
|
|
|
{ |
41
|
7 |
|
$this->update($args, false); |
42
|
6 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param LifecycleEventArgs $args |
46
|
|
|
*/ |
47
|
7 |
|
public function postRemove(LifecycleEventArgs $args) |
48
|
|
|
{ |
49
|
7 |
|
$this->update($args, true); |
50
|
6 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param LifecycleEventArgs $args |
54
|
|
|
*/ |
55
|
7 |
|
public function postUpdate(LifecycleEventArgs $args) |
56
|
|
|
{ |
57
|
7 |
|
$this->update($args, false); |
58
|
6 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param LifecycleEventArgs $args |
62
|
|
|
* @param bool $remove |
63
|
|
|
*/ |
64
|
21 |
|
protected function update(LifecycleEventArgs $args, $remove) |
65
|
|
|
{ |
66
|
21 |
|
$alias = $this->getEntityAlias($args); |
67
|
18 |
|
$this->keeper->set($alias, new \DateTime()); |
68
|
|
|
|
69
|
18 |
|
if ($this->track_individually_entity && ($ids = $this->getEntityIdentifier($args))) { |
70
|
6 |
|
if ($remove) { |
71
|
2 |
|
$this->keeper->remove($alias.$ids); |
72
|
2 |
|
} else { |
73
|
4 |
|
$this->keeper->set($alias.$ids, new \DateTime()); |
74
|
|
|
} |
75
|
6 |
|
} |
76
|
18 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param LifecycleEventArgs $args |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
21 |
|
protected function getEntityAlias(LifecycleEventArgs $args) |
84
|
|
|
{ |
85
|
21 |
|
$class = get_class($args->getEntity()); |
86
|
|
|
|
87
|
21 |
|
foreach ($args->getEntityManager()->getConfiguration()->getEntityNamespaces() as $ns_alias => $ns) { |
88
|
21 |
|
if (strpos($class, $ns) === 0) { |
89
|
18 |
|
return $ns_alias.':'.ltrim(str_replace($ns, '', $class), '\\'); |
90
|
|
|
} |
91
|
21 |
|
} |
92
|
|
|
|
93
|
3 |
|
throw new \RuntimeException(sprintf('Entity "%s" is not supported from EntityManager', $class)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param LifecycleEventArgs $args |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
12 |
|
protected function getEntityIdentifier(LifecycleEventArgs $args) |
102
|
|
|
{ |
103
|
|
|
$ids = $args |
104
|
12 |
|
->getEntityManager() |
105
|
12 |
|
->getClassMetadata(get_class($args->getEntity())) |
106
|
12 |
|
->getIdentifierValues($args->getEntity()); |
107
|
12 |
|
return $ids ? Keeper::IDENTIFIER_PREFIX.implode(Keeper::IDENTIFIER_SEPARATOR, $ids) : ''; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|