forked from pimcore/pimcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare for twig >=3.9.0 (pimcore#17444)
* Prepare twig pimcore cache extension based on nodes * Apply php-cs-fixer changes * Small fixes * Fix sonar cloud issues * Apply php-cs-fixer changes * Avoid duplication * Docs + deprecations * Apply php-cs-fixer changes * Deprcate pimcore_placeholder captureStart()/captureEnd() and update docs * Fix typo Co-authored-by: Jacob Dreesen <[email protected]> * Fix docs * Deprecate pimcore_head_script captureStart()/captureEnd() * Apply php-cs-fixer changes * Improve type cast * Apply php-cs-fixer changes * Deprecate pimcore_head_style captureStart()/captureEnd() * Simplify placeholder docs * Simplify placeholder docs * Deprecations * Docs * Docs * Docs * Code style * Improve deprecations * Implement feedback * Apply php-cs-fixer changes --------- Co-authored-by: markus-moser <[email protected]> Co-authored-by: Jacob Dreesen <[email protected]> Co-authored-by: mattamon <[email protected]>
- Loading branch information
1 parent
df3a88c
commit 123b8db
Showing
13 changed files
with
468 additions
and
28 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
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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Twig\Extension; | ||
|
||
use Pimcore\Cache; | ||
use Pimcore\Tool; | ||
use Pimcore\Twig\TokenParser\CacheParser; | ||
use Twig\Extension\AbstractExtension; | ||
use function is_null; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class CacheTagExtension extends AbstractExtension | ||
{ | ||
private const CACHE_KEY_PREFIX = 'pimcore_twigcache_'; | ||
|
||
public function getTokenParsers(): array | ||
{ | ||
return [ | ||
new CacheParser(), | ||
]; | ||
} | ||
|
||
public function getContentFromCache(string $key, bool $force): string|bool | ||
{ | ||
|
||
if ($this->isCacheEnabled($force)) { | ||
return Cache::load(self::CACHE_KEY_PREFIX . $key); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function startBuffering(): void | ||
{ | ||
ob_start(); | ||
} | ||
|
||
public function endBuffering(string $key, array $tags, ?int $ttl, bool $force): string | ||
{ | ||
$content = ob_get_contents(); | ||
ob_end_clean(); | ||
|
||
if ($this->isCacheEnabled($force)) { | ||
$tags[] = 'in_template'; | ||
if (is_null($ttl)) { | ||
$tags[] = 'output'; | ||
} | ||
$tags = array_unique($tags); | ||
Cache::save($content, self::CACHE_KEY_PREFIX . $key, $tags, $ttl, 996, true); | ||
} | ||
|
||
return $content; | ||
} | ||
|
||
private function isCacheEnabled(bool $force): bool | ||
{ | ||
return !Tool::isFrontendRequestByAdmin() || $force; | ||
} | ||
} |
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
Oops, something went wrong.