From 0047e1809b10a6c77f3466e0200b64e50a783f6f Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 24 Jul 2020 09:20:20 -0400 Subject: [PATCH 1/6] Fix template usage --- src/Plugin.php | 6 ++++++ src/TwigExtension.php | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Plugin.php b/src/Plugin.php index 07739be..1a0f8e7 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -61,6 +61,9 @@ public function init() 'tagCollection' => TagCollection::class ]); + // Register Twig extension + Craft::$app->getView()->registerTwigExtension(new TwigExtension); + // Attach Behaviors \Craft::$app->getResponse()->attachBehavior('cache-control', CacheControlBehavior::class); \Craft::$app->getResponse()->attachBehavior('tag-header', TagHeaderBehavior::class); @@ -73,6 +76,9 @@ public function init() if ($this->getSettings()->useLocalTags) { EventRegistrar::registerFallback(); } + + // Register Twig extension + \Craft::$app->getView()->registerTwigExtension(new TwigExtension()); } // ServiceLocators diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 4ada220..288fe40 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -15,7 +15,7 @@ public function getGlobals() { return [ 'upper' => [ - 'cache' => \Craft::createObject(CacheResponse::class) + 'cache' => new CacheResponse(\Craft::$app->getResponse()) ] ]; } From bb308bcde07bfe22628b10630cb02ec42a0702c5 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 31 Jul 2020 09:31:38 -0400 Subject: [PATCH 2/6] Namespace --- src/Plugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin.php b/src/Plugin.php index 1a0f8e7..d5eb4f3 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -62,7 +62,7 @@ public function init() ]); // Register Twig extension - Craft::$app->getView()->registerTwigExtension(new TwigExtension); + \Craft::$app->getView()->registerTwigExtension(new TwigExtension); // Attach Behaviors \Craft::$app->getResponse()->attachBehavior('cache-control', CacheControlBehavior::class); From 740fc56d6a0521d77cc46083bd8014e3ceb948ba Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 31 Jul 2020 09:50:53 -0400 Subject: [PATCH 3/6] Whoops --- src/Plugin.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index d5eb4f3..fed6e5d 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -61,9 +61,6 @@ public function init() 'tagCollection' => TagCollection::class ]); - // Register Twig extension - \Craft::$app->getView()->registerTwigExtension(new TwigExtension); - // Attach Behaviors \Craft::$app->getResponse()->attachBehavior('cache-control', CacheControlBehavior::class); \Craft::$app->getResponse()->attachBehavior('tag-header', TagHeaderBehavior::class); @@ -78,7 +75,7 @@ public function init() } // Register Twig extension - \Craft::$app->getView()->registerTwigExtension(new TwigExtension()); + \Craft::$app->getView()->registerTwigExtension(new TwigExtension); } // ServiceLocators From 86d48f5f2d7295c92cec9487ba32d562a47723c7 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Tue, 11 Aug 2020 13:05:15 -0400 Subject: [PATCH 4/6] Fix console requests --- src/CacheResponse.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/CacheResponse.php b/src/CacheResponse.php index 5e626ee..efc26b3 100644 --- a/src/CacheResponse.php +++ b/src/CacheResponse.php @@ -1,11 +1,11 @@ isWebResponse()) { + return; + } + $this->response->addCacheControlDirective('private'); $this->response->addCacheControlDirective('no-cache'); } public function for(string $time) { + if (!$this->isWebResponse()) { + return; + } + $seconds = strtotime($time) - time(); $this->response->setSharedMaxAge($seconds); } + public function isWebResponse() + { + return $this->response instanceof \craft\web\Response; + } } From 8888448b7e33db64a9f8478af4c97849b57c2b18 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 16 Apr 2021 10:40:54 -0400 Subject: [PATCH 5/6] Use createObject for DI --- src/TwigExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 288fe40..d27fb58 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -15,7 +15,7 @@ public function getGlobals() { return [ 'upper' => [ - 'cache' => new CacheResponse(\Craft::$app->getResponse()) + 'cache' => \Craft::createObject(CacheResponse::class, [\Craft::$app->getResponse()]), ] ]; } From d4060bc9e5940749ac06ae741517e5dc557b47ad Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 15 Oct 2021 09:29:03 -0400 Subject: [PATCH 6/6] Return type --- src/CacheResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CacheResponse.php b/src/CacheResponse.php index efc26b3..71d3309 100644 --- a/src/CacheResponse.php +++ b/src/CacheResponse.php @@ -34,7 +34,7 @@ public function for(string $time) $this->response->setSharedMaxAge($seconds); } - public function isWebResponse() + public function isWebResponse(): bool { return $this->response instanceof \craft\web\Response; }