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\Service; |
10
|
|
|
|
11
|
|
|
use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\CacheKeyBuilder\EtagHasherInterface; |
12
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
|
15
|
|
|
class CacheKeyBuilder |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
const IDENTIFIER_SEPARATOR = '|'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
const IDENTIFIER_PREFIX = ':'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var EtagHasherInterface |
29
|
|
|
*/ |
30
|
|
|
protected $etag_hasher; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var EntityManagerInterface|null |
34
|
|
|
*/ |
35
|
|
|
protected $em; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param EtagHasherInterface $etag_hasher |
39
|
|
|
*/ |
40
|
9 |
|
public function __construct(EtagHasherInterface $etag_hasher) |
41
|
|
|
{ |
42
|
9 |
|
$this->etag_hasher = $etag_hasher; |
43
|
9 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param EntityManagerInterface $em |
47
|
|
|
* |
48
|
|
|
* @return CacheKeyBuilder |
49
|
|
|
*/ |
50
|
9 |
|
public function setEntityManager(EntityManagerInterface $em) |
51
|
|
|
{ |
52
|
9 |
|
$this->em = $em; |
53
|
9 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param object $entity |
58
|
|
|
* |
59
|
|
|
* @return string|null |
60
|
|
|
*/ |
61
|
3 |
|
public function getEntityAlias($entity) |
62
|
|
|
{ |
63
|
3 |
|
if (!($this->em instanceof EntityManagerInterface)) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
$class = get_class($entity); |
68
|
|
|
|
69
|
3 |
|
foreach ($this->em->getConfiguration()->getEntityNamespaces() as $ns_alias => $ns) { |
70
|
3 |
|
if (strpos($class, $ns) === 0) { |
71
|
2 |
|
return $ns_alias.':'.ltrim(str_replace($ns, '', $class), '\\'); |
72
|
|
|
} |
73
|
3 |
|
} |
74
|
|
|
|
75
|
1 |
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param object $entity |
80
|
|
|
* |
81
|
|
|
* @return string|null |
82
|
|
|
*/ |
83
|
4 |
|
public function getEntityIdentifier($entity) |
84
|
|
|
{ |
85
|
4 |
|
if (!($this->em instanceof EntityManagerInterface)) { |
86
|
1 |
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
$ids = $this->em->getClassMetadata(get_class($entity))->getIdentifierValues($entity); |
90
|
|
|
|
91
|
3 |
|
return $ids ? self::IDENTIFIER_PREFIX.implode(self::IDENTIFIER_SEPARATOR, $ids) : null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Response $response |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
2 |
|
public function getEtag(Response $response) |
100
|
|
|
{ |
101
|
2 |
|
return $this->etag_hasher->hash($response); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|