-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\web\twig\nodes; | ||
|
||
use DateTimeImmutable; | ||
use Twig\Compiler; | ||
use Twig\Node\Node; | ||
|
||
/** | ||
* Class CacheResponseNode | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 4.10.0 | ||
*/ | ||
class CacheResponseNode extends Node | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function compile(Compiler $compiler): void | ||
{ | ||
$durationNum = $this->getAttribute('durationNum'); | ||
|
||
$duration = $durationNum === null ? null : self::durationInSeconds( | ||
$durationNum, | ||
$this->getAttribute('durationUnit'), | ||
); | ||
$line = sprintf( | ||
'\Craft::$app->getResponse()->setCacheHeaders(%s);', | ||
$duration, | ||
); | ||
|
||
$compiler->write("$line\n"); | ||
} | ||
|
||
private static function durationInSeconds($number, $unit): int | ||
{ | ||
if ($unit === 'week') { | ||
if ($number == 1) { | ||
$number = 7; | ||
$unit = 'days'; | ||
} else { | ||
$unit = 'weeks'; | ||
} | ||
} | ||
|
||
$now = new DateTimeImmutable(); | ||
$then = $now->modify("+$number $unit"); | ||
|
||
return $then->getTimestamp() - $now->getTimestamp(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\web\twig\tokenparsers; | ||
|
||
use craft\web\twig\nodes\CacheResponseNode; | ||
use Twig\Token; | ||
use Twig\TokenParser\AbstractTokenParser; | ||
|
||
/** | ||
* Class CacheResponse | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 4.10.0 | ||
*/ | ||
class CacheResponseTokenParser extends AbstractTokenParser | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function parse(Token $token): CacheResponseNode | ||
{ | ||
$lineno = $token->getLine(); | ||
$parser = $this->parser; | ||
$stream = $parser->getStream(); | ||
|
||
$nodes = []; | ||
|
||
$attributes = [ | ||
'durationNum' => null, | ||
'durationUnit' => null, | ||
]; | ||
|
||
if ($stream->test(Token::NAME_TYPE, 'never')) { | ||
$stream->next(); | ||
$attributes['durationNum'] = 0; | ||
} elseif ($stream->test(Token::NAME_TYPE, 'for')) { | ||
$stream->next(); | ||
$attributes['durationNum'] = $stream->expect(Token::NUMBER_TYPE)->getValue(); | ||
$attributes['durationUnit'] = $stream->expect(Token::NAME_TYPE, | ||
[ | ||
'sec', | ||
'secs', | ||
'second', | ||
'seconds', | ||
'min', | ||
'mins', | ||
'minute', | ||
'minutes', | ||
'hour', | ||
'hours', | ||
'day', | ||
'days', | ||
'fortnight', | ||
'fortnights', | ||
'forthnight', | ||
'forthnights', | ||
'month', | ||
'months', | ||
'year', | ||
'years', | ||
'week', | ||
'weeks', | ||
])->getValue(); | ||
} | ||
|
||
$stream->expect(Token::BLOCK_END_TYPE); | ||
|
||
return new CacheResponseNode($nodes, $attributes, $lineno, $this->getTag()); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getTag(): string | ||
{ | ||
return 'cacheResponse'; | ||
} | ||
} |