Skip to content

Commit

Permalink
Add LastModifiedExtensionInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 29, 2024
1 parent 3a94244 commit 36ddfc4
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Extension/AbstractExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Twig\Extension;

abstract class AbstractExtension implements ExtensionInterface
abstract class AbstractExtension implements LastModifiedExtensionInterface
{
public function getTokenParsers()
{
Expand Down
2 changes: 0 additions & 2 deletions src/Extension/ExtensionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
/**
* Interface implemented by extension classes.
*
* @method int getLastModified() Returns the last modification time of the extension for cache invalidation.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface ExtensionInterface
Expand Down
23 changes: 23 additions & 0 deletions src/Extension/LastModifiedExtensionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Twig\Extension;

interface LastModifiedExtensionInterface extends ExtensionInterface
{
/**
* Returns the last modification time of the extension for cache invalidation.
*
* This timestamp should be the last time the source code of the extension class
* and all its dependencies were modified.
*/
public function getLastModified(): int;
}
8 changes: 3 additions & 5 deletions src/ExtensionSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Twig\Error\RuntimeError;
use Twig\Extension\ExtensionInterface;
use Twig\Extension\GlobalsInterface;
use Twig\Extension\LastModifiedExtensionInterface;
use Twig\Extension\StagingExtension;
use Twig\Node\Expression\Binary\AbstractBinary;
use Twig\Node\Expression\Unary\AbstractUnary;
Expand Down Expand Up @@ -118,13 +119,10 @@ public function getLastModified(): int

$lastModified = 0;
foreach ($this->extensions as $extension) {
$r = new \ReflectionObject($extension);

if ($r->hasMethod('getLastModified')) {
if ($extension instanceof LastModifiedExtensionInterface) {
$lastModified = max($extension->getLastModified(), $lastModified);
} else {
trigger_deprecation('twig/twig', '3.18', 'Not implementing "%s::getLastModified()" in "%s" is deprecated.', ExtensionInterface::class, $extension::class);

$r = new \ReflectionObject($extension);
if (is_file($r->getFileName())) {
$lastModified = max(filemtime($r->getFileName()), $lastModified);
}
Expand Down
5 changes: 0 additions & 5 deletions tests/CustomExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,4 @@ public function getOperators(): array
{
return $this->operators;
}

public function getLastModified(): int
{
return 0;
}
}
2 changes: 1 addition & 1 deletion tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public function testAddExtension()

public function testAddMockExtension()
{
$extension = $this->createMock(AbstractExtension::class);
$extension = $this->createMock(ExtensionInterface::class);
$loader = new ArrayLoader(['page' => 'hey']);

$twig = new Environment($loader);
Expand Down

0 comments on commit 36ddfc4

Please sign in to comment.