Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Add PreLexerInterface #4215

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Environment
private $autoReload;
private $cache;
private $lexer;
/** @var array<PreLexerInterface> */
private $preLexers = [];
private $parser;
private $compiler;
/** @var array<string, mixed> */
Expand Down Expand Up @@ -488,6 +490,11 @@ public function resolveTemplate($names): TemplateWrapper
throw new LoaderError(\sprintf('Unable to find one of the following templates: "%s".', implode('", "', $names)));
}

public function addPreLexer(PreLexerInterface $preLexer): void
{
$this->preLexers[] = $preLexer;
}

public function setLexer(Lexer $lexer)
{
$this->lexer = $lexer;
Expand All @@ -502,6 +509,10 @@ public function tokenize(Source $source): TokenStream
$this->lexer = new Lexer($this);
}

foreach ($this->preLexers as $preLexer) {
$source = $preLexer->preLex($source);
}

return $this->lexer->tokenize($source);
}

Expand Down
8 changes: 8 additions & 0 deletions src/PreLexerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Twig;

interface PreLexerInterface
{
public function preLex(Source $source): Source;
}