2121use function explode ;
2222use function implode ;
2323use function is_array ;
24- use function is_string ;
2524use function sprintf ;
2625use function strtoupper ;
26+ use function trim ;
2727
2828/**
2929 * @psalm-type Props = array{
3232 * uriTag: UriTag,
3333 * saver: ResourceStorageSaver,
3434 * roProvider:ProviderInterface<TagAwareAdapterInterface>,
35- * etagProvider: ProviderInterface<TagAwareAdapterInterface>
35+ * etagProvider: ProviderInterface<TagAwareAdapterInterface>,
36+ * serverContext: ServerContextInterface
3637 * }
3738 */
3839final class ResourceStorage implements ResourceStorageInterface
@@ -64,6 +65,7 @@ public function __construct(
6465 private PurgerInterface $ purger ,
6566 private UriTagInterface $ uriTag ,
6667 private ResourceStorageSaver $ saver ,
68+ private ServerContextInterface $ serverContext ,
6769 #[Set(TagAwareAdapterInterface::class, ResourceObjectPool::class)]
6870 ProviderInterface $ roPoolProvider ,
6971 #[Set(TagAwareAdapterInterface::class, EtagPool::class)]
@@ -239,19 +241,26 @@ private function evaluateBody(mixed $body): mixed
239241
240242 private function getUriKey (AbstractUri $ uri , string $ type ): string
241243 {
242- return $ type . ($ this ->uriTag )($ uri ) . (isset ( $ _SERVER [ 'X_VARY ' ] ) ? $ this ->getVary () : '' );
244+ return $ type . ($ this ->uriTag )($ uri ) . ($ this -> serverContext -> has ( 'X_VARY ' ) ? $ this ->getVary () : '' );
243245 }
244246
245247 private function getVary (): string
246248 {
247- $ xvary = $ _SERVER ['X_VARY ' ];
248- /** @psalm-suppress RedundantCast */
249- $ varys = explode (', ' , (string ) $ xvary ); // @phpstan-ignore-line
249+ $ xvary = $ this ->serverContext ->get ('X_VARY ' );
250+ assert ($ xvary !== null , 'getVary() is only called when X_VARY exists ' );
251+
252+ $ varys = explode (', ' , $ xvary );
250253 $ varyString = '' ;
251254 foreach ($ varys as $ vary ) {
255+ $ vary = trim ($ vary );
256+ if ($ vary === '' ) {
257+ continue ;
258+ }
259+
252260 $ phpVaryKey = sprintf ('X_%s ' , strtoupper ($ vary ));
253- if (isset ($ _SERVER [$ phpVaryKey ]) && is_string ($ _SERVER [$ phpVaryKey ])) {
254- $ varyString .= $ _SERVER [$ phpVaryKey ];
261+ $ value = $ this ->serverContext ->get ($ phpVaryKey );
262+ if ($ value !== null ) {
263+ $ varyString .= $ value ;
255264 }
256265 }
257266
@@ -279,6 +288,7 @@ public function __serialize(): array
279288 'saver ' => $ this ->saver ,
280289 'roProvider ' => $ this ->roPoolProvider ,
281290 'etagProvider ' => $ this ->etagPoolProvider ,
291+ 'serverContext ' => $ this ->serverContext ,
282292 ];
283293 }
284294
@@ -293,6 +303,7 @@ public function __unserialize(array $data): void
293303 $ this ->purger = $ data ['purger ' ];
294304 $ this ->uriTag = $ data ['uriTag ' ];
295305 $ this ->saver = $ data ['saver ' ];
306+ $ this ->serverContext = $ data ['serverContext ' ];
296307 $ this ->initializePools ($ data ['roProvider ' ], $ data ['etagProvider ' ]);
297308 }
298309}
0 commit comments