Skip to content

Commit

Permalink
Remove name unicity constraint, add phpdoc, refactor time computation
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Dec 10, 2023
1 parent 3d86571 commit 6c5f9c6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
14 changes: 1 addition & 13 deletions src/Extension/AttributeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getLastModified(): int

foreach ($this->objectsOrClasses as $objectOrClass) {
$r = new \ReflectionClass($objectOrClass);
if (is_file($r->getFileName()) && ($extensionTime = filemtime($r->getFileName())) > $lastModified) {
if (is_file($r->getFileName()) && $lastModified < $extensionTime = filemtime($r->getFileName())) {
$lastModified = $extensionTime;
}
}
Expand Down Expand Up @@ -99,10 +99,6 @@ private function initFromAttributes()
$attribute = $attribute->newInstance();

$name = $attribute->name;
if (isset($filters[$name])) {
throw new \LogicException(sprintf('Multiple definitions of the "%s" filter.', $name));
}

$parameters = $method->getParameters();
$needsEnvironment = isset($parameters[0]) && Environment::class === $parameters[0]->getType()?->getName();
$firstParam = $needsEnvironment ? 1 : 0;
Expand All @@ -128,10 +124,6 @@ private function initFromAttributes()
$attribute = $attribute->newInstance();

$name = $attribute->name;
if (isset($functions[$name])) {
throw new \LogicException(sprintf('Multiple definitions of the "%s" function.', $name));
}

$parameters = $method->getParameters();
$needsEnvironment = isset($parameters[0]) && Environment::class === $parameters[0]->getType()?->getName();
$firstParam = $needsEnvironment ? 1 : 0;
Expand All @@ -155,10 +147,6 @@ private function initFromAttributes()
$attribute = $attribute->newInstance();

$name = $attribute->name;
if (isset($tests[$name])) {
throw new \LogicException(sprintf('Multiple definitions of the "%s" test.', $name));
}

$parameters = $method->getParameters();
$isVariadic = isset($parameters[$firstParam]) && end($parameters)->isVariadic();

Expand Down
7 changes: 7 additions & 0 deletions src/Extension/WithLastModified.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

namespace Twig\Extension;

/**
* Freshness of templates use the last modification date of each extension class.
* Implement this interface to provide a different last modification date.
*/
interface WithLastModified
{
/**
* @return int A UNIX timestamp
*/
public function getLastModified(): int;
}
4 changes: 2 additions & 2 deletions src/ExtensionSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ public function getLastModified(): int

foreach ($this->extensions as $extension) {
$r = new \ReflectionObject($extension);
if (is_file($r->getFileName()) && ($extensionTime = filemtime($r->getFileName())) > $this->lastModified) {
if (is_file($r->getFileName()) && $this->lastModified < $extensionTime = filemtime($r->getFileName())) {
$this->lastModified = $extensionTime;
}
if ($extension instanceof WithLastModified && ($extensionTime = $extension->getLastModified()) > $this->lastModified) {
if ($extension instanceof WithLastModified && $this->lastModified < $extensionTime = $extension->getLastModified()) {
$this->lastModified = $extensionTime;
}
}
Expand Down

0 comments on commit 6c5f9c6

Please sign in to comment.