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\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
|
16
|
|
|
class CacheKeyBuilder |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
const IDENTIFIER_SEPARATOR = '|'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const IDENTIFIER_PREFIX = ':'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EtagHasherInterface |
30
|
|
|
*/ |
31
|
|
|
protected $etag_hasher; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param EtagHasherInterface $etag_hasher |
35
|
|
|
*/ |
36
|
7 |
|
public function __construct(EtagHasherInterface $etag_hasher) |
37
|
|
|
{ |
38
|
7 |
|
$this->etag_hasher = $etag_hasher; |
39
|
7 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param object $entity |
43
|
|
|
* @param EntityManagerInterface $em |
44
|
|
|
* |
45
|
|
|
* @return string|null |
46
|
|
|
*/ |
47
|
3 |
|
public function getEntityAlias($entity, EntityManagerInterface $em) |
48
|
|
|
{ |
49
|
3 |
|
$class = get_class($entity); |
50
|
|
|
|
51
|
3 |
|
foreach ($em->getConfiguration()->getEntityNamespaces() as $ns_alias => $ns) { |
52
|
3 |
|
if (strpos($class, $ns) === 0) { |
53
|
2 |
|
return $ns_alias.':'.ltrim(str_replace($ns, '', $class), '\\'); |
54
|
|
|
} |
55
|
3 |
|
} |
56
|
|
|
|
57
|
1 |
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param object $entity |
62
|
|
|
* @param EntityManagerInterface $em |
63
|
|
|
* |
64
|
|
|
* @return string|null |
65
|
|
|
*/ |
66
|
3 |
|
public function getEntityIdentifier($entity, EntityManagerInterface $em) |
67
|
|
|
{ |
68
|
3 |
|
$ids = $em->getClassMetadata(get_class($entity))->getIdentifierValues($entity); |
69
|
|
|
|
70
|
3 |
|
return $ids ? self::IDENTIFIER_PREFIX.implode(self::IDENTIFIER_SEPARATOR, $ids) : null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Request $request |
75
|
|
|
* @param Response $response |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
1 |
|
public function getEtag(Request $request, Response $response) |
80
|
|
|
{ |
81
|
1 |
|
return $this->etag_hasher->hash($request, $response); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|