1616use Symfony \Component \HttpKernel \HttpCache \Esi ;
1717use Symfony \Component \HttpKernel \HttpCache \HttpCache as BaseHttpCache ;
1818use Symfony \Component \HttpKernel \HttpCache \Store ;
19+ use Symfony \Component \HttpKernel \HttpCache \StoreInterface ;
20+ use Symfony \Component \HttpKernel \HttpCache \SurrogateInterface ;
1921use Symfony \Component \HttpKernel \KernelInterface ;
2022
2123/**
@@ -28,22 +30,36 @@ class HttpCache extends BaseHttpCache
2830 protected $ cacheDir ;
2931 protected $ kernel ;
3032
33+ private $ store ;
34+ private $ surrogate ;
35+ private $ options ;
36+
3137 /**
32- * @param string $cacheDir The cache directory (default used if null)
38+ * @param string|StoreInterface $cache The cache directory (default used if null) or the storage instance
3339 */
34- public function __construct (KernelInterface $ kernel , string $ cacheDir = null )
40+ public function __construct (KernelInterface $ kernel , $ cache = null , SurrogateInterface $ surrogate = null , array $ options = null )
3541 {
3642 $ this ->kernel = $ kernel ;
37- $ this ->cacheDir = $ cacheDir ;
43+ $ this ->surrogate = $ surrogate ;
44+ $ this ->options = $ options ?? [];
45+
46+ if ($ cache instanceof StoreInterface) {
47+ $ this ->store = $ cache ;
48+ } elseif (null !== $ cache && !\is_string ($ cache )) {
49+ throw new \TypeError (sprintf ('Argument 2 passed to "%s()" must be a string or a SurrogateInterface, "%s" given. ' , __METHOD__ , get_debug_type ($ cache )));
50+ } else {
51+ $ this ->cacheDir = $ cache ;
52+ }
3853
39- $ isDebug = $ kernel ->isDebug ();
40- $ options = ['debug ' => $ isDebug ];
54+ if (null === $ options && $ kernel ->isDebug ()) {
55+ $ this ->options = ['debug ' => true ];
56+ }
4157
42- if ($ isDebug ) {
43- $ options ['stale_if_error ' ] = 0 ;
58+ if ($ this -> options [ ' debug ' ] ?? false ) {
59+ $ this -> options += ['stale_if_error ' => 0 ] ;
4460 }
4561
46- parent ::__construct ($ kernel , $ this ->createStore (), $ this ->createSurrogate (), array_merge ($ options , $ this ->getOptions ()));
62+ parent ::__construct ($ kernel , $ this ->createStore (), $ this ->createSurrogate (), array_merge ($ this -> options , $ this ->getOptions ()));
4763 }
4864
4965 /**
@@ -69,11 +85,11 @@ protected function getOptions()
6985
7086 protected function createSurrogate ()
7187 {
72- return new Esi ();
88+ return $ this -> surrogate ?? new Esi ();
7389 }
7490
7591 protected function createStore ()
7692 {
77- return new Store ($ this ->cacheDir ?: $ this ->kernel ->getCacheDir ().'/http_cache ' );
93+ return $ this -> store ?? new Store ($ this ->cacheDir ?: $ this ->kernel ->getCacheDir ().'/http_cache ' );
7894 }
7995}
0 commit comments