-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14969 from craftcms/feature/cache-response-tag
`expires` tag
- Loading branch information
Showing
9 changed files
with
234 additions
and
38 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
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
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,47 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\web\twig\nodes; | ||
|
||
use craft\helpers\DateTimeHelper; | ||
use Twig\Compiler; | ||
use Twig\Node\Node; | ||
|
||
/** | ||
* Class ExpiresNode | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 4.10.0 | ||
*/ | ||
class ExpiresNode extends Node | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function compile(Compiler $compiler): void | ||
{ | ||
$expiration = $this->hasNode('expiration') ? $this->getNode('expiration') : null; | ||
|
||
if ($expiration) { | ||
$compiler | ||
->write('$expiration = ') | ||
->subcompile($expiration) | ||
->raw(";\n") | ||
->write('$duration = \craft\helpers\DateTimeHelper::toDateTime($expiration)->getTimestamp() - time();'); | ||
} else { | ||
$duration = DateTimeHelper::relativeTimeToSeconds( | ||
$this->getAttribute('durationNum'), | ||
$this->getAttribute('durationUnit'), | ||
); | ||
$compiler->write("\$duration = $duration;\n"); | ||
} | ||
|
||
$compiler | ||
->write('\Craft::$app->getResponse()->setCacheHeaders($duration);') | ||
->raw("\n"); | ||
} | ||
} |
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,60 @@ | ||
<?php | ||
/** | ||
* @link https://craftcms.com/ | ||
* @copyright Copyright (c) Pixel & Tonic, Inc. | ||
* @license https://craftcms.github.io/license/ | ||
*/ | ||
|
||
namespace craft\web\twig\tokenparsers; | ||
|
||
use craft\helpers\DateTimeHelper; | ||
use craft\web\twig\nodes\ExpiresNode; | ||
use Twig\Token; | ||
use Twig\TokenParser\AbstractTokenParser; | ||
|
||
/** | ||
* Class ExpiresTokenParser | ||
* | ||
* @author Pixel & Tonic, Inc. <[email protected]> | ||
* @since 4.10.0 | ||
*/ | ||
class ExpiresTokenParser extends AbstractTokenParser | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function parse(Token $token): ExpiresNode | ||
{ | ||
$lineno = $token->getLine(); | ||
$parser = $this->parser; | ||
$stream = $parser->getStream(); | ||
|
||
$nodes = []; | ||
|
||
$attributes = [ | ||
'durationNum' => 0, | ||
'durationUnit' => 'seconds', | ||
]; | ||
|
||
if ($stream->test(Token::OPERATOR_TYPE, 'in')) { | ||
$stream->next(); | ||
$attributes['durationNum'] = $stream->expect(Token::NUMBER_TYPE)->getValue(); | ||
$attributes['durationUnit'] = $stream->expect(Token::NAME_TYPE, DateTimeHelper::RELATIVE_TIME_UNITS)->getValue(); | ||
} elseif ($stream->test(Token::NAME_TYPE, 'on')) { | ||
$stream->next(); | ||
$nodes['expiration'] = $parser->getExpressionParser()->parseExpression(); | ||
} | ||
|
||
$stream->expect(Token::BLOCK_END_TYPE); | ||
|
||
return new ExpiresNode($nodes, $attributes, $lineno, $this->getTag()); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getTag(): string | ||
{ | ||
return 'expires'; | ||
} | ||
} |
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