diff --git a/src/CacheResponse.php b/src/CacheResponse.php index 5e626ee..71d3309 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(): bool + { + return $this->response instanceof \craft\web\Response; + } } diff --git a/src/Plugin.php b/src/Plugin.php index 07739be..fed6e5d 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -73,6 +73,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..d27fb58 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -15,7 +15,7 @@ public function getGlobals() { return [ 'upper' => [ - 'cache' => \Craft::createObject(CacheResponse::class) + 'cache' => \Craft::createObject(CacheResponse::class, [\Craft::$app->getResponse()]), ] ]; }