File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -827,6 +827,14 @@ public function getTest(string $name): ?TwigTest
827827 return $ this ->extensionSet ->getTest ($ name );
828828 }
829829
830+ /**
831+ * @param callable(string): (TokenParserInterface|false) $callable
832+ */
833+ public function registerUndefinedTestCallback (callable $ callable ): void
834+ {
835+ $ this ->extensionSet ->registerUndefinedTestCallback ($ callable );
836+ }
837+
830838 /**
831839 * @return void
832840 */
Original file line number Diff line number Diff line change @@ -59,6 +59,8 @@ final class ExtensionSet
5959 private $ functionCallbacks = [];
6060 /** @var array<callable(string): (TwigFilter|false)> */
6161 private $ filterCallbacks = [];
62+ /** @var array<callable(string): (TwigTest|false)> */
63+ private $ testCallbacks = [];
6264 /** @var array<callable(string): (TokenParserInterface|false)> */
6365 private $ parserCallbacks = [];
6466 private $ lastModified = 0 ;
@@ -410,9 +412,23 @@ public function getTest(string $name): ?TwigTest
410412 }
411413 }
412414
415+ foreach ($ this ->testCallbacks as $ callback ) {
416+ if (false !== $ test = $ callback ($ name )) {
417+ return $ test ;
418+ }
419+ }
420+
413421 return null ;
414422 }
415423
424+ /**
425+ * @param callable(string): (TwigTest|false) $callable
426+ */
427+ public function registerUndefinedTestCallback (callable $ callable ): void
428+ {
429+ $ this ->testCallbacks [] = $ callable ;
430+ }
431+
416432 public function getExpressionParsers (): ExpressionParsers
417433 {
418434 if (!$ this ->initialized ) {
Original file line number Diff line number Diff line change @@ -425,6 +425,22 @@ public function testUndefinedFilterCallback()
425425 $ this ->assertSame ('dynamic ' , $ filter ->getName ());
426426 }
427427
428+ public function testUndefinedTestCallback ()
429+ {
430+ $ twig = new Environment (new ArrayLoader ());
431+ $ twig ->registerUndefinedTestCallback (function (string $ name ) {
432+ if ('dynamic ' === $ name ) {
433+ return new TwigTest ('dynamic ' , function () { return 'dynamic ' ; });
434+ }
435+
436+ return false ;
437+ });
438+
439+ $ this ->assertNull ($ twig ->getTest ('does_not_exist ' ));
440+ $ this ->assertInstanceOf (TwigTest::class, $ test = $ twig ->getTest ('dynamic ' ));
441+ $ this ->assertSame ('dynamic ' , $ test ->getName ());
442+ }
443+
428444 public function testUndefinedTokenParserCallback ()
429445 {
430446 $ twig = new Environment (new ArrayLoader ());
You can’t perform that action at this time.
0 commit comments