Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #41 from timkelty/fix-template-usage
Browse files Browse the repository at this point in the history
Fix template usage
  • Loading branch information
timkelty authored Oct 15, 2021
2 parents bd6cd34 + d4060bc commit 95b7ec4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/CacheResponse.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php namespace ostark\upper;

use craft\web\Response;
use yii\base\Response;

class CacheResponse
{
/**
* @var \craft\web\Response|\ostark\upper\behaviors\CacheControlBehavior
* @var \yii\base\Response|\ostark\upper\behaviors\CacheControlBehavior
*/
public $response;

Expand All @@ -16,14 +16,26 @@ public function __construct(Response $response)

public function never()
{
if (!$this->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;
}
}
3 changes: 3 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public function init()
if ($this->getSettings()->useLocalTags) {
EventRegistrar::registerFallback();
}

// Register Twig extension
\Craft::$app->getView()->registerTwigExtension(new TwigExtension);
}

// ServiceLocators
Expand Down
2 changes: 1 addition & 1 deletion src/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function getGlobals()
{
return [
'upper' => [
'cache' => \Craft::createObject(CacheResponse::class)
'cache' => \Craft::createObject(CacheResponse::class, [\Craft::$app->getResponse()]),
]
];
}
Expand Down

0 comments on commit 95b7ec4

Please sign in to comment.