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
|
|
|
* @param Keeper $keeper |
23
|
|
|
*/ |
24
|
6 |
|
public function __construct(Keeper $keeper) |
25
|
|
|
{ |
26
|
6 |
|
$this->keeper = $keeper; |
27
|
6 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param LifecycleEventArgs $args |
31
|
|
|
*/ |
32
|
2 |
|
public function postPersist(LifecycleEventArgs $args) |
33
|
|
|
{ |
34
|
2 |
|
$this->keeper->set($this->getKeyFromEntity($args), new \DateTime()); |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param LifecycleEventArgs $args |
39
|
|
|
*/ |
40
|
2 |
|
public function postRemove(LifecycleEventArgs $args) |
41
|
|
|
{ |
42
|
2 |
|
$this->keeper->set($this->getKeyFromEntity($args), new \DateTime()); |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param LifecycleEventArgs $args |
47
|
|
|
*/ |
48
|
2 |
|
public function postUpdate(LifecycleEventArgs $args) |
49
|
|
|
{ |
50
|
2 |
|
$this->keeper->set($this->getKeyFromEntity($args), new \DateTime()); |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param LifecycleEventArgs $args |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
6 |
|
protected function getKeyFromEntity(LifecycleEventArgs $args) |
59
|
|
|
{ |
60
|
6 |
|
$parts = explode('\\', get_class($args->getEntity())); |
61
|
6 |
|
$entity = array_pop($parts); |
62
|
6 |
|
$namespace = implode('\\', $parts); |
63
|
|
|
|
64
|
6 |
|
$namespaces = $args->getEntityManager()->getConfiguration()->getEntityNamespaces(); |
65
|
6 |
|
foreach ($namespaces as $ns_alias => $ns) { |
66
|
6 |
|
if ($ns == $namespace) { |
67
|
3 |
|
return $ns_alias.':'.$entity; |
68
|
|
|
} |
69
|
6 |
|
} |
70
|
|
|
|
71
|
3 |
|
throw new \RuntimeException(sprintf('Namespace "%s" is not supported from EntityManager', $namespace)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|