-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Latte: runtime helpers moved to Runtime class
- Loading branch information
Showing
7 changed files
with
165 additions
and
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Latte (https://latte.nette.org) | ||
* Copyright (c) 2008 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nette\Bridges\CacheLatte; | ||
|
||
use Latte; | ||
use Nette; | ||
use Nette\Caching\Cache; | ||
use Nette\Caching\OutputHelper; | ||
|
||
|
||
/** | ||
* Runtime helpers for Latte v3. | ||
* @internal | ||
*/ | ||
class Runtime | ||
{ | ||
/** @var array<int, OutputHelper|\stdClass> */ | ||
private array $stack = []; | ||
|
||
|
||
public function __construct( | ||
private Nette\Caching\Storage $storage, | ||
) { | ||
} | ||
|
||
|
||
public function initialize(Latte\Runtime\Template $template): void | ||
{ | ||
if ($this->stack) { | ||
$file = (new \ReflectionClass($template))->getFileName(); | ||
if (@is_file($file)) { // @ - may trigger error | ||
end($this->stack)->dependencies[Cache::Files][] = $file; | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Starts the output cache. Returns true if buffering was started. | ||
*/ | ||
public function createCache(string $key, ?array $args = null): bool | ||
{ | ||
if ($args) { | ||
if (array_key_exists('if', $args) && !$args['if']) { | ||
$this->stack[] = new \stdClass; | ||
return true; | ||
} | ||
|
||
$key = array_merge([$key], array_intersect_key($args, range(0, count($args)))); | ||
} | ||
|
||
if ($this->stack) { | ||
end($this->stack)->dependencies[Cache::Items][] = $key; | ||
} | ||
|
||
$cache = new Cache($this->storage, 'Nette.Templating.Cache'); | ||
if ($helper = $cache->capture($key)) { | ||
$this->stack[] = $helper; | ||
|
||
if (isset($args['dependencies'])) { | ||
$args += $args['dependencies'](); | ||
} | ||
|
||
$helper->dependencies[Cache::Tags] = $args['tags'] ?? null; | ||
$helper->dependencies[Cache::Expire] = $args['expiration'] ?? $args['expire'] ?? '+ 7 days'; | ||
} | ||
|
||
return (bool) $helper; | ||
} | ||
|
||
|
||
/** | ||
* Ends the output cache. | ||
*/ | ||
public function end(): void | ||
{ | ||
$helper = array_pop($this->stack); | ||
if ($helper instanceof OutputHelper) { | ||
$helper->end(); | ||
} | ||
} | ||
|
||
|
||
public function rollback(): void | ||
{ | ||
$helper = array_pop($this->stack); | ||
if ($helper instanceof OutputHelper) { | ||
$helper->rollback(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 | ||
|
||
declare(strict_types=1); | ||
|
||
use Nette\Bridges\CacheLatte\Runtime; | ||
use Nette\Caching\Cache; | ||
use Nette\Caching\Storages\DevNullStorage; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
if (version_compare(Latte\Engine::VERSION, '3', '<')) { | ||
Tester\Environment::skip('Test for Latte 3'); | ||
} | ||
|
||
|
||
test('', function () { | ||
$runtime = new Runtime(new DevNullStorage); | ||
$dp = [Cache::Tags => ['rum', 'cola']]; | ||
Assert::true($runtime->createCache('test', $dp)); | ||
$stack = Assert::with($runtime, fn() => $this->stack); | ||
$runtime->end(); | ||
Assert::same($dp + [Cache::Expire => '+ 7 days'], $stack[0]->dependencies); | ||
}); | ||
|
||
test('', function () { | ||
$runtime = new Runtime(new DevNullStorage); | ||
$dp = [Cache::Tags => ['rum', 'cola']]; | ||
$dpFallback = fn() => $dp; | ||
Assert::true($runtime->createCache('test', ['dependencies' => $dpFallback])); | ||
$stack = Assert::with($runtime, fn() => $this->stack); | ||
$runtime->end(); | ||
Assert::same($dp + [Cache::Expire => '+ 7 days'], $stack[0]->dependencies); | ||
}); | ||
|
||
test('', function () { | ||
$runtime = new Runtime(new DevNullStorage); | ||
$dp = [ | ||
Cache::Tags => ['rum', 'cola'], | ||
Cache::Expire => '+ 1 days', | ||
]; | ||
$dpFallback = fn() => $dp; | ||
Assert::true($runtime->createCache('test', ['dependencies' => $dpFallback])); | ||
$stack = Assert::with($runtime, fn() => $this->stack); | ||
$runtime->end(); | ||
Assert::same($dp, $stack[0]->dependencies); | ||
}); |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
<?php | ||
%A% | ||
if (Nette\Bridges\CacheLatte\Nodes\CacheNode::createCache($this->global->cacheStorage, '%[\w]+%', $this->global->cacheStack)) /* line %d% */ | ||
if ($this->global->cache->createCache('%a%')) /* line %d% */ | ||
try { | ||
echo ' '; | ||
echo LR\Filters::escapeHtmlText(($this->filters->lower)($title)) /* line %d% */; | ||
echo "\n"; | ||
|
||
Nette\Bridges\CacheLatte\Nodes\CacheNode::endCache($this->global->cacheStack) /* line %d% */; | ||
$this->global->cache->end() /* line %d% */; | ||
} catch (\Throwable $ʟ_e) { | ||
Nette\Bridges\CacheLatte\Nodes\CacheNode::rollback($this->global->cacheStack); | ||
$this->global->cache->rollback(); | ||
throw $ʟ_e; | ||
} | ||
%A% |
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