@@ -32,6 +32,11 @@ class TagHandler
3232 */
3333 private $ tagsHeader ;
3434
35+ /**
36+ * @var array
37+ */
38+ private $ tags = array ();
39+
3540 /**
3641 * Constructor
3742 *
@@ -49,6 +54,40 @@ public function __construct(CacheInvalidator $invalidator, $tagsHeader = 'X-Cach
4954 $ this ->tagsHeader = $ tagsHeader ;
5055 }
5156
57+ /**
58+ * Get the HTTP header name that will hold cache tags.
59+ *
60+ * @return string
61+ */
62+ public function getTagsHeaderName ()
63+ {
64+ return $ this ->tagsHeader ;
65+ }
66+
67+ /**
68+ * Get the value for the HTTP tag header.
69+ *
70+ * This concatenates all tags and ensures correct encoding.
71+ *
72+ * @return string
73+ */
74+ public function getTagsHeaderValue ()
75+ {
76+ return implode (', ' , array_unique ($ this ->escapeTags ($ this ->tags )));
77+ }
78+
79+ /**
80+ * Add tags to be sent.
81+ *
82+ * This must be called before any response is sent to the client.
83+ *
84+ * @param array $tags List of tags to add.
85+ */
86+ public function addTags (array $ tags )
87+ {
88+ $ this ->tags = array_merge ($ this ->tags , $ tags );
89+ }
90+
5291 /**
5392 * Invalidate cache entries that contain any of the specified tags in their
5493 * tag header.
@@ -59,10 +98,26 @@ public function __construct(CacheInvalidator $invalidator, $tagsHeader = 'X-Cach
5998 */
6099 public function invalidateTags (array $ tags )
61100 {
62- $ tagExpression = sprintf ('(%s)(,.+)?$ ' , implode ('| ' , array_map ('preg_quote ' , $ tags )));
101+ $ tagExpression = sprintf ('(%s)(,.+)?$ ' , implode ('| ' , array_map ('preg_quote ' , $ this -> escapeTags ( $ tags) )));
63102 $ headers = array ($ this ->tagsHeader => $ tagExpression );
64103 $ this ->invalidator ->invalidate ($ headers );
65104
66105 return $ this ;
67106 }
107+
108+ /**
109+ * Make sure that the tags are valid.
110+ *
111+ * @param array $tags The tags to escape.
112+ *
113+ * @return array Sane tags.
114+ */
115+ protected function escapeTags (array $ tags )
116+ {
117+ array_walk ($ tags , function (&$ tag ) {
118+ $ tag = str_replace (array (', ' , "\n" ), array ('_ ' , '_ ' ), $ tag );
119+ });
120+
121+ return $ tags ;
122+ }
68123}
0 commit comments