22
33namespace DNADesign \HTTPCacheControl ;
44
5+ use SilverStripe \Control \Controller ;
56use SilverStripe \Control \Middleware \HTTPCacheControlMiddleware ;
6- use SilverStripe \Core \Extension ;
77use SilverStripe \Core \Config \Config ;
8+ use SilverStripe \Core \Extension ;
89
910/**
1011 * This extension uses the field data "max-age" and sets the max age.
1112 * This will be applied to the Controller::init function for the controller
1213 * you add this extension to.
14+ *
15+ * @extends Extension<Controller>
1316 */
1417class ControllerExtension extends Extension
1518{
16-
1719 /**
18- * @var int $cacheAge Max-age seconds to cache for.
20+ * Max-age seconds to cache for.
1921 */
20- private static $ cacheAge_default = 120 ;
22+ private static int $ cacheAge_default = 120 ;
2123
2224 /**
2325 * Called by ContentController::init();
2426 */
25- public function onBeforeInit ()
27+ public function onBeforeInit (): void
2628 {
2729 $ cacheControl = HTTPCacheControlMiddleware::singleton ();
2830
2931 if ($ this ->getDisableCache ()) {
30- $ cacheControl ->disableCache ($ force = true );
32+ $ cacheControl ->disableCache (true );
3133 } else {
3234 $ cacheControl
3335 ->enableCache ($ this ->getForceCache ())
@@ -36,52 +38,52 @@ public function onBeforeInit()
3638 }
3739 }
3840
39- public function getDisableCache ()
41+ public function getDisableCache (): bool
4042 {
41- if ($ this ->owner ->Config ()->get ('http_cache_disable ' )) {
43+ if ($ this ->getOwner () ->Config ()->get ('http_cache_disable ' )) {
4244 return true ;
4345 }
4446
45- if ($ this ->owner -> failover ->Config ()->get ('http_cache_disable ' )) {
47+ if ($ this ->getOwner ()-> getFailover () ->Config ()->get ('http_cache_disable ' )) {
4648 return true ;
4749 }
4850
49- if ($ this ->owner -> failover ->MaxAge == '0 ' ) {
51+ if ($ this ->getOwner ()-> getFailover () ->MaxAge == '0 ' ) {
5052 return true ;
5153 }
54+
55+ return false ;
5256 }
5357
54- public function getForceCache ()
58+ public function getForceCache (): bool
5559 {
56- if ($ this ->owner ->failover ->Config ()->get ('http_cache_force ' )) {
57- return true ;
58- }
60+ return $ this ->getOwner ()->getFailover ()->Config ()->get ('http_cache_force ' ) === true ;
5961 }
6062
61- public function getCacheAge ()
63+ public function getCacheAge (): int
6264 {
63- if ($ this ->owner ->Config ()->get ('http_cache_disable ' )) {
65+ if ($ this ->getOwner () ->Config ()->get ('http_cache_disable ' )) {
6466 return 0 ;
6567 }
6668
6769 /* http_cache_disable can be used on subclasses to override the behaviour */
68- if ($ this ->owner -> failover ->Config ()->get ('http_cache_disable ' )) {
70+ if ($ this ->getOwner ()-> getFailover () ->Config ()->get ('http_cache_disable ' )) {
6971 return 0 ;
7072 }
7173
7274 //any page with forms in its elements shouldnt be cached
73- if ($ this ->owner -> failover ->hasExtension ('DNADesign\Elemental\Extensions\ElementalPageExtension ' )) {
74- $ area = $ this ->owner ->ElementalArea ();
75+ if ($ this ->getOwner ()-> getFailover () ->hasExtension ('DNADesign\Elemental\Extensions\ElementalPageExtension ' )) {
76+ $ area = $ this ->getOwner () ->ElementalArea (); // @phpstan-ignore-line method.notFound
7577 $ elements = $ area ->Elements ();
7678
7779 foreach ($ elements as $ element ) {
78- /* http_cache_disable can be used on Elements to override the behaviour
79- this is particularly useful for elements containing forms which
80- contain a security ID specific to the user
80+ // http_cache_disable can be used on Elements to override the behaviour
81+ // this is particularly useful for elements containing forms which
82+ // contain a security ID specific to the user
83+
84+ // Another way to approach this and still get decent caching on the page
85+ // is to lazy load those elements after the initial page request
8186
82- Another way to approach this and still get decent caching on the page
83- is to lazy load those elements after the initial page request
84- */
8587 if ($ element ->Config ()->get ('http_cache_disable ' )) {
8688 return 0 ;
8789 } elseif ($ element ->ClassName == 'DNADesign\ElementalVirtual\Model\ElementVirtual ' ) {
@@ -92,9 +94,9 @@ public function getCacheAge()
9294 }
9395 }
9496
95- if ($ this ->owner -> failover ->MaxAge != '' && $ this ->owner -> failover ->MaxAge != '0 ' ) {
96- return (int ) ($ this ->owner -> failover ->MaxAge * 60 );
97+ if ($ this ->getOwner ()-> getFailover () ->MaxAge != '' && $ this ->getOwner ()-> getFailover () ->MaxAge != '0 ' ) {
98+ return (int ) ($ this ->getOwner ()-> getFailover () ->MaxAge * 60 );
9799 }
98- return (int ) Config::inst ()->get (' DNADesign\HTTPCacheControl\ControllerExtension ' , 'cacheAge_default ' );
100+ return (int ) Config::inst ()->get (self ::class , 'cacheAge_default ' );
99101 }
100102}
0 commit comments