14
14
use Psr \Cache \InvalidArgumentException ;
15
15
use Symfony \Component \Cache \Adapter \FilesystemAdapter ;
16
16
use Symfony \Component \Cache \Adapter \TagAwareAdapter ;
17
+ use Symfony \Component \Cache \Adapter \TagAwareAdapterInterface ;
17
18
use Symfony \Component \HttpFoundation \HeaderBag ;
18
19
use Symfony \Component \HttpFoundation \Request ;
19
20
use Symfony \Component \HttpFoundation \Response ;
@@ -39,7 +40,7 @@ class TaggableStore implements StoreInterface
39
40
private $ purgeTagsHeader ;
40
41
41
42
/**
42
- * @var TagAwareAdapter
43
+ * @var TagAwareAdapterInterface
43
44
*/
44
45
private $ cache ;
45
46
@@ -63,15 +64,47 @@ public function __construct($cacheDir, $purgeTagsHeader = PurgeTagsListener::DEF
63
64
}
64
65
65
66
$ this ->purgeTagsHeader = $ purgeTagsHeader ;
66
- $ this ->cache = new TagAwareAdapter (new FilesystemAdapter ('fos-http-cache ' , 0 , $ cacheDir ));
67
67
68
- if (SemaphoreStore::isSupported (false )) {
69
- $ store = new SemaphoreStore ();
70
- } else {
71
- $ store = new FlockStore ($ cacheDir );
72
- }
68
+ $ this ->setCache (new TagAwareAdapter (new FilesystemAdapter ('fos-http-cache ' , 0 , $ cacheDir )));
69
+ $ this ->setLockFactory (new Factory ($ this ->getBestLocalLockStore ($ cacheDir )));
70
+ }
73
71
74
- $ this ->lockFactory = new Factory ($ store );
72
+ /**
73
+ * Use this method if you want to use a different cache implementation than
74
+ * the one created in the constructor. Note that there are very good reasons
75
+ * that the local adapters are used by default and you do not inject them in
76
+ * the constructor but via setter injection instead. This is to protect you
77
+ * as a developer! Only use this method if you're really sure your cache
78
+ * implementation meets the needs of Symfony's HttpCache.
79
+ *
80
+ * @param TagAwareAdapterInterface $cache
81
+ *
82
+ * @return TaggableStore
83
+ */
84
+ public function setCache (TagAwareAdapterInterface $ cache )
85
+ {
86
+ $ this ->cache = $ cache ;
87
+
88
+ return $ this ;
89
+ }
90
+
91
+ /**
92
+ * Use this method if you want to use a different lock implementation than
93
+ * the one created in the constructor. Note that there are very good reasons
94
+ * that the local adapters are used by default and you do not inject them in
95
+ * the constructor but via setter injection instead. This is to protect you
96
+ * as a developer! Only use this method if you're really sure your lock
97
+ * implementation meets the needs of Symfony's HttpCache.
98
+ *
99
+ * @param Factory $lockFactory
100
+ *
101
+ * @return TaggableStore
102
+ */
103
+ public function setLockFactory (Factory $ lockFactory )
104
+ {
105
+ $ this ->lockFactory = $ lockFactory ;
106
+
107
+ return $ this ;
75
108
}
76
109
77
110
/**
@@ -392,4 +425,20 @@ private function restoreResponse(array $cacheData)
392
425
$ cacheData ['headers ' ]
393
426
);
394
427
}
428
+
429
+ /**
430
+ * @param string $cache
80C5
Dir
431
+ *
432
+ * @return \Symfony\Component\Lock\StoreInterface
433
+ */
434
+ private function getBestLocalLockStore ($ cacheDir )
435
+ {
436
+ if (SemaphoreStore::isSupported (false )) {
437
+ $ store = new SemaphoreStore ();
438
+ } else {
439
+ $ store = new FlockStore ($ cacheDir );
440
+ }
441
+
442
+ return $ store ;
443
+ }
395
444
}
0 commit comments