2121use Psr \EventDispatcher \EventDispatcherInterface ;
2222use Psr \SimpleCache \CacheException ;
2323use Psr \SimpleCache \CacheInterface ;
24+ use Ramsey \Uuid \UuidFactory ;
25+ use Ramsey \Uuid \UuidFactoryInterface ;
2426
2527class CacheBuilder implements ICacheBuilder {
2628
@@ -74,12 +76,23 @@ class CacheBuilder implements ICacheBuilder {
7476 */
7577 private $ lazyDispatcher ;
7678
79+ /**
80+ * @var UuidFactoryInterface
81+ */
82+ private $ uuidFactory ;
83+
84+ /**
85+ * @var string|null
86+ */
87+ private $ sessionId = null ;
88+
7789 /**
7890 * Creates an instance with a simple build/cache validators that check results for null
7991 *
8092 * @note it is intended that references in this instance are maintained when the instance is cloned
93+ * @param UuidFactoryInterface|null $uuidFactory - an optional uuid generation override to control automatic session ids
8194 */
82- public function __construct () {
95+ public function __construct (? UuidFactoryInterface $ uuidFactory = null ) {
8396 $ this ->buildValidator = function ($ result ) : bool {
8497 return $ result !== null ;
8598 };
@@ -99,59 +112,64 @@ public function dispatch(object $event) : object {
99112 }
100113 };
101114 };
115+ $ this ->uuidFactory = $ uuidFactory ?? new UuidFactory ();
116+ }
117+
118+ public function __clone () {
119+ $ this ->sessionId = null ;
102120 }
103121
104122 public function get () {
105123 if ($ this ->getCache () !== null ) {
106- $ this ->dispatch (new Event (Event::CACHE_GET_START ));
124+ $ this ->dispatch ($ this -> newEvent (Event::CACHE_GET_START ));
107125 $ key = $ this ->getCacheKey ();
108126 try {
109127 $ result = $ key !== null ? $ this ->cache ->get ($ key ) : null ;
110128 } catch (CacheException $ e ) {
111- $ this ->dispatch (( new Event (Event ::CACHE_GET_ERROR ) )->withCacheException ($ e ));
129+ $ this ->dispatch ($ this -> newEvent ( Event::CACHE_GET_ERROR )->withCacheException ($ e ));
112130 $ result = null ;
113131 }
114132 $ cacheValidator = $ this ->cacheValidator ;
115133 if ($ cacheValidator ($ result ) === true ) {
116- $ this ->dispatch (new Event (Event::CACHE_GET_HIT ));
134+ $ this ->dispatch ($ this -> newEvent (Event::CACHE_GET_HIT ));
117135 return $ result ;
118136 }
119- $ this ->dispatch (new Event (Event::CACHE_GET_MISS ));
137+ $ this ->dispatch ($ this -> newEvent (Event::CACHE_GET_MISS ));
120138 }
121139 if ($ this ->builder === null ) {
122140 return null ;
123141 }
124- $ this ->dispatch (new Event (Event::BUILD_START ));
142+ $ this ->dispatch ($ this -> newEvent (Event::BUILD_START ));
125143 $ builder = $ this ->builder ;
126144 try {
127145 $ result = $ builder ();
128146 } catch (Exception $ e ) {
129- $ this ->dispatch (( new Event (Event ::BUILD_ERROR ) )->withBuildException ($ e ));
147+ $ this ->dispatch ($ this -> newEvent ( Event::BUILD_ERROR )->withBuildException ($ e ));
130148 $ result = null ;
131149 }
132150 $ buildValidator = $ this ->buildValidator ;
133151 if ($ buildValidator ($ result ) === true ) {
134- $ this ->dispatch (new Event (Event::BUILD_SUCCESS ));
152+ $ this ->dispatch ($ this -> newEvent (Event::BUILD_SUCCESS ));
135153
136154 // the cache key used for setting a value may be different than the key used to get a value if upstream
137155 // ...dependencies and state have changed - to be safe, we regenerate the key
138156 $ this ->isCacheKeyStale = true ;
139157 $ key = $ this ->getCacheKey ();
140158 if ($ this ->getCache () !== null && $ key !== null ) {
141159 $ cacheLifespanBuilder = $ this ->cacheLifespanBuilder ;
142- $ this ->dispatch (new Event (Event::CACHE_SET_START ));
160+ $ this ->dispatch ($ this -> newEvent (Event::CACHE_SET_START ));
143161 try {
144162 if ($ this ->cache ->set ($ key , $ result , $ cacheLifespanBuilder ($ result ))) {
145- $ this ->dispatch (new Event (Event::CACHE_SET_SUCCESS ));
163+ $ this ->dispatch ($ this -> newEvent (Event::CACHE_SET_SUCCESS ));
146164 } else {
147- $ this ->dispatch (new Event (Event::CACHE_SET_FAIL ));
165+ $ this ->dispatch ($ this -> newEvent (Event::CACHE_SET_FAIL ));
148166 }
149167 } catch (CacheException $ e ) {
150- $ this ->dispatch (( new Event (Event ::CACHE_SET_ERROR ) )->withCacheException ($ e ));
168+ $ this ->dispatch ($ this -> newEvent ( Event::CACHE_SET_ERROR )->withCacheException ($ e ));
151169 }
152170 }
153171 } else {
154- $ this ->dispatch (new Event (Event::BUILD_FAIL ));
172+ $ this ->dispatch ($ this -> newEvent (Event::BUILD_FAIL ));
155173 }
156174 return $ result ;
157175 }
@@ -169,6 +187,13 @@ public function getCacheKey() : ?string {
169187 return $ this ->cacheKey ;
170188 }
171189
190+ public function getSessionId () : string {
191+ if ($ this ->sessionId === null ) {
192+ $ this ->sessionId = $ this ->uuidFactory ->uuid4 ()->toString ();
193+ }
194+ return $ this ->sessionId ;
195+ }
196+
172197 public function withBuildValidator (Closure $ validator ) : ICacheBuilder {
173198 $ instance = clone $ this ;
174199 $ instance ->buildValidator = $ validator ;
@@ -213,6 +238,12 @@ public function withLazyEventDispatcher(Closure $dispatcher) : ICacheBuilder {
213238 return $ instance ;
214239 }
215240
241+ public function withSessionId (string $ sessionId ) : ICacheBuilder {
242+ $ instance = clone $ this ;
243+ $ instance ->sessionId = $ sessionId ;
244+ return $ instance ;
245+ }
246+
216247 /**
217248 * @param Event $event
218249 */
@@ -226,4 +257,12 @@ private function dispatch(Event $event) : void {
226257 }
227258 $ this ->dispatcher ->dispatch ($ event );
228259 }
260+
261+ /**
262+ * @param string $message
263+ * @return Event
264+ */
265+ private function newEvent (string $ message ) : Event {
266+ return new Event ($ message , $ this ->getSessionId ());
267+ }
229268}
0 commit comments