11<?php
22
3+ /*
4+ * This file is part of php-cache organization.
5+ *
6+ * (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
7+ *
8+ * This source file is subject to the MIT license that is bundled
9+ * with this source code in the file LICENSE.
10+ */
11+
312namespace Cache \Taggable ;
413
514use Cache \Taggable \TaggableItemInterface ;
918use Psr \Cache \CacheItemPoolInterface ;
1019
1120/**
12- *
13- */
21+ * @internal
22+ *
23+ * An adapter for non-taggable cache items, to be used with the cache pool
24+ * adapter.
25+ *
26+ * This adapter stores tags along with the cached value, by storing wrapping
27+ * the item in an array structure containing both.
28+ *
29+ * @author Magnus Nordlander <magnus@fervo.se>
30+ */
1431class TaggablePSR6ItemAdapter implements TaggableItemInterface
1532{
33+ /**
34+ * @type boolean
35+ */
36+ private $ initialized = false ;
37+
38+ /**
39+ * @type CacheItemInterface
40+ */
1641 private $ cacheItem ;
42+
43+ /**
44+ * @type array<string>
45+ */
1746 private $ tags = [];
1847
48+ /**
49+ * @param CacheItemInterface $cacheItem
50+ */
1951 private function __construct (CacheItemInterface $ cacheItem )
2052 {
2153 $ this ->cacheItem = $ cacheItem ;
22- if ($ this ->cacheItem ->isHit ()) {
23- $ rawItem = $ this ->cacheItem ->get ();
24-
25- if (is_array ($ rawItem ) && isset ($ rawItem ['tags ' ])) {
26- $ this ->tags = $ rawItem ['tags ' ];
27- }
28- }
2954 }
3055
31- public static function makeTaggable (CacheItemInterface $ cacheItem ) // @TODO naming?
56+ /**
57+ * @param CacheItemInterface $cacheItem
58+ * @return TaggableItemInterface
59+ */
60+ public static function makeTaggable (CacheItemInterface $ cacheItem )
3261 {
3362 return new self ($ cacheItem );
3463 }
3564
65+ /**
66+ * @return CacheItemInterface
67+ */
3668 public function unwrap ()
3769 {
3870 return $ this ->cacheItem ;
@@ -73,6 +105,8 @@ public function isHit()
73105 */
74106 public function set ($ value )
75107 {
108+ $ this ->initializeTags ();
109+
76110 $ this ->cacheItem ->set ([
77111 'value ' => $ value ,
78112 'tags ' => $ this ->tags ,
@@ -86,6 +120,7 @@ public function set($value)
86120 */
87121 public function getTags ()
88122 {
123+ $ this ->initializeTags ();
89124 return $ this ->tags ;
90125 }
91126
@@ -94,6 +129,7 @@ public function getTags()
94129 */
95130 public function setTags (array $ tags )
96131 {
132+ $ this ->initialized = true ;
97133 $ this ->tags = $ tags ;
98134 $ this ->updateTags ();
99135
@@ -105,6 +141,7 @@ public function setTags(array $tags)
105141 */
106142 public function addTag ($ tag )
107143 {
144+ $ this ->initializeTags ();
108145 $ this ->tags [] = $ tag ;
109146 $ this ->updateTags ();
110147
@@ -136,4 +173,19 @@ private function updateTags()
136173 'tags ' => $ this ->tags ,
137174 ]);
138175 }
176+
177+ private function initializeTags ()
178+ {
179+ if (!$ this ->initialized ) {
180+ if ($ this ->cacheItem ->isHit ()) {
181+ $ rawItem = $ this ->cacheItem ->get ();
182+
183+ if (is_array ($ rawItem ) && isset ($ rawItem ['tags ' ])) {
184+ $ this ->tags = $ rawItem ['tags ' ];
185+ }
186+ }
187+
188+ $ this ->initialized = true ;
189+ }
190+ }
139191}
0 commit comments