|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Gerard van Helden <[email protected]> |
|
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Zicht\Bundle\FrameworkExtraBundle\Helper; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Provides a global registry for page "annotations", which can be used for meta data such as meta tags, title or |
|
10
|
|
|
* OpenGraph data. This can be utilized using the Twig {% annotate %} and {% annotations %} {% endannotations %} |
|
11
|
|
|
* constructs. |
|
12
|
|
|
*/ |
|
13
|
|
|
class AnnotationRegistry |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* The annotations |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $annotations = array(); |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->annotations = array(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Add an annotation. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $name |
|
36
|
|
|
* @param mixed $value |
|
37
|
|
|
* @param int $priority |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
|
|
public function addAnnotation($name, $value, $priority = 0) |
|
41
|
|
|
{ |
|
42
|
|
|
$annotation = $this->getAnnotation($name); |
|
43
|
|
|
$new_annotation = array('name' => $name, 'value' => $value, 'priority' => $priority); |
|
44
|
|
|
if (!empty($annotation)) { |
|
45
|
|
|
if ($priority > $annotation['value']['priority']) { |
|
46
|
|
|
$this->setAnnotation($annotation['key'], $new_annotation); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
} else { |
|
49
|
|
|
$this->annotations[]= $new_annotation; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get annotation |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $name |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getAnnotation($name) |
|
60
|
|
|
{ |
|
61
|
|
|
$match = array(); |
|
62
|
|
|
foreach ($this->getAnnotations() as $key => $annotation) { |
|
63
|
|
|
if ($annotation['name'] == $name) { |
|
64
|
|
|
$match['key'] = $key; |
|
65
|
|
|
$match['value'] = $annotation; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
return $match; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Set annotation |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $key |
|
75
|
|
|
* @param string $annotation |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setAnnotation($key, $annotation) |
|
78
|
|
|
{ |
|
79
|
|
|
if (array_key_exists($key, $this->annotations)) { |
|
80
|
|
|
$this->annotations[$key] = $annotation; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the annotations. |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getAnnotations() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->annotations; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Add multiple annotations, or an annotated object that implements the getPublicAnnotations() method. |
|
97
|
|
|
* |
|
98
|
|
|
* @param mixed $values |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
public function addAnnotations($values) |
|
102
|
|
|
{ |
|
103
|
|
|
// TODO interface |
|
104
|
|
|
if (method_exists($values, 'getPublicAnnotations')) { |
|
105
|
|
|
$this->addAnnotations($values->getPublicAnnotations()); |
|
106
|
|
|
} else { |
|
107
|
|
|
foreach ($values as $name => $value) { |
|
108
|
|
|
$this->addAnnotation($name, $value); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|