@@ -260,12 +260,19 @@ private function maybeHandleCacheEventAsEndOfSpan(Events\CacheEvent $event): boo
260260 private function getSessionKey (): ?string
261261 {
262262 try {
263- /** @var Session $request */
264- $ request = $ this ->container ()->make ('session.store ' );
265-
266- return $ request ->getId ();
263+ /** @var Session $sessionStore */
264+ $ sessionStore = $ this ->container ()->make ('session.store ' );
265+
266+ // It is safe for us to get the session ID here without checking if the session is started
267+ // because getting the session ID does not start the session. In addition we need the ID before
268+ // the session is started because the cache will retrieve the session ID from the cache before the session
269+ // is considered started. So if we wait for the session to be started, we will not be able to replace the
270+ // session key in the cache operation that is being executed to retrieve the session data from the cache.
271+ return $ sessionStore ->getId ();
267272 } catch (\Exception $ e ) {
268273 // We can assume the session store is not available here so there is no session key to retrieve
274+ // We capture a generic exception to avoid breaking the application because some code paths can
275+ // result in an exception other than the expected `Illuminate\Contracts\Container\BindingResolutionException`
269276 return null ;
270277 }
271278 }
@@ -283,14 +290,14 @@ private function replaceSessionKey(string $value): string
283290 *
284291 * @param string[] $values
285292 *
286- * @return string []
293+ * @return mixed []
287294 */
288295 private function replaceSessionKeys (array $ values ): array
289296 {
290297 $ sessionKey = $ this ->getSessionKey ();
291298
292299 return array_map (static function ($ value ) use ($ sessionKey ) {
293- return $ value === $ sessionKey ? '{sessionKey} ' : $ value ;
300+ return is_string ( $ value ) && $ value === $ sessionKey ? '{sessionKey} ' : $ value ;
294301 }, $ values );
295302 }
296303
0 commit comments