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\CacheKeyBuilder; |
12
|
|
|
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Keeper; |
13
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
14
|
|
|
|
15
|
|
|
class DoctrineListener |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Keeper |
19
|
|
|
*/ |
20
|
|
|
protected $keeper; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var CacheKeyBuilder |
24
|
|
|
*/ |
25
|
|
|
protected $builder; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $track_individually_entity; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Keeper $keeper |
34
|
|
|
* @param CacheKeyBuilder $builder |
35
|
|
|
* @param bool $track_individually_entity |
36
|
|
|
*/ |
37
|
9 |
|
public function __construct(Keeper $keeper, CacheKeyBuilder $builder, $track_individually_entity) |
38
|
|
|
{ |
39
|
9 |
|
$this->keeper = $keeper; |
40
|
9 |
|
$this->builder = $builder; |
41
|
9 |
|
$this->track_individually_entity = $track_individually_entity; |
42
|
9 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param LifecycleEventArgs $args |
46
|
|
|
*/ |
47
|
3 |
|
public function postPersist(LifecycleEventArgs $args) |
48
|
|
|
{ |
49
|
3 |
|
$this->update($args, false); |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param LifecycleEventArgs $args |
54
|
|
|
*/ |
55
|
3 |
|
public function postRemove(LifecycleEventArgs $args) |
56
|
|
|
{ |
57
|
3 |
|
$this->update($args, true); |
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param LifecycleEventArgs $args |
62
|
|
|
*/ |
63
|
3 |
|
public function postUpdate(LifecycleEventArgs $args) |
64
|
|
|
{ |
65
|
3 |
|
$this->update($args, false); |
66
|
3 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param LifecycleEventArgs $args |
70
|
|
|
* @param bool $remove |
71
|
|
|
*/ |
72
|
9 |
|
protected function update(LifecycleEventArgs $args, $remove) |
73
|
|
|
{ |
74
|
9 |
|
$alias = $this->builder->getEntityAlias($args->getEntity(), $args->getEntityManager()); |
75
|
9 |
|
$this->keeper->set($alias, new \DateTime()); |
76
|
|
|
|
77
|
9 |
|
if ($this->track_individually_entity) { |
78
|
6 |
|
$ids = $this->builder->getEntityIdentifier($args->getEntity(), $args->getEntityManager()); |
79
|
6 |
|
if ($ids) { |
|
|
|
|
80
|
6 |
|
if ($remove) { |
81
|
2 |
|
$this->keeper->remove($alias.$ids); |
82
|
2 |
|
} else { |
83
|
4 |
|
$this->keeper->set($alias.$ids, new \DateTime()); |
84
|
|
|
} |
85
|
6 |
|
} |
86
|
6 |
|
} |
87
|
9 |
|
} |
88
|
|
|
} |
89
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: