-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make AsTwigExtension extension first party instead of relying on the …
…extension to be registered
- Loading branch information
Showing
6 changed files
with
57 additions
and
79 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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
namespace Twig\Extension; | ||
|
||
use Twig\Attribute\AsTwigExtension; | ||
use Twig\Attribute\AsTwigFilter; | ||
use Twig\Attribute\AsTwigFunction; | ||
use Twig\Attribute\AsTwigTest; | ||
|
@@ -23,36 +24,25 @@ | |
* Define Twig filters, functions, and tests with PHP attributes. | ||
* | ||
* @author Jérôme Tamarelle <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class AttributeExtension extends AbstractExtension implements ModificationAwareInterface | ||
final class AttributeExtension extends AbstractExtension | ||
{ | ||
private array $classes; | ||
private array $filters; | ||
private array $functions; | ||
private array $tests; | ||
|
||
public function __construct( | ||
/** | ||
* A list of objects or class names defining filters, functions, and tests using PHP attributes. | ||
* When passing a class name, it must be available in runtimes. | ||
* | ||
* @var iterable<object|class-string> | ||
*/ | ||
private iterable $objectsOrClasses, | ||
) { | ||
} | ||
|
||
public function getLastModified(): int | ||
/** | ||
* A list of objects or class names defining filters, functions, and tests using PHP attributes. | ||
* When passing a class name, it must be available in runtimes. | ||
* | ||
* @param class-string[] | ||
*/ | ||
public function __construct(array $classes) | ||
{ | ||
$lastModified = 0; | ||
|
||
foreach ($this->objectsOrClasses as $objectOrClass) { | ||
$r = new \ReflectionClass($objectOrClass); | ||
if (is_file($r->getFileName()) && $lastModified < $extensionTime = filemtime($r->getFileName())) { | ||
$lastModified = $extensionTime; | ||
} | ||
} | ||
|
||
return $lastModified; | ||
$this->classes = $classes; | ||
} | ||
|
||
public function getFilters(): array | ||
|
@@ -86,13 +76,18 @@ private function initFromAttributes() | |
{ | ||
$filters = $functions = $tests = []; | ||
|
||
foreach ($this->objectsOrClasses as $objectOrClass) { | ||
foreach ($this->classes as $objectOrClass) { | ||
try { | ||
$reflectionClass = new \ReflectionClass($objectOrClass); | ||
} catch (\ReflectionException $e) { | ||
throw new \LogicException(sprintf('"%s" class requires a list of objects or class name, "%s" given.', __CLASS__, get_debug_type($objectOrClass)), 0, $e); | ||
} | ||
|
||
$attributes = $reflectionClass->getAttributes(AsTwigExtension::class); | ||
if (!$attributes) { | ||
throw new \LogicException(sprintf('Extension class "%s" must have the attribute "%s" in order to use attributes', is_string($objectOrClass) ? $objectOrClass : get_debug_type($objectOrClass), AsTwigExtension::class)); | ||
} | ||
|
||
foreach ($reflectionClass->getMethods() as $method) { | ||
// Filters | ||
foreach ($method->getAttributes(AsTwigFilter::class) as $attribute) { | ||
|
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
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