Skip to main content
1

Require the package

Run the following command in your project root:
composer require wik/lexer
Composer will install the package and register the bin/lex CLI binary under vendor/bin/lex.
Wik/Lex requires PHP ^8.1. The mbstring extension is recommended for correct multibyte template handling. The igbinary extension is optional — when present it speeds up AST cache serialisation.
2

Create lex.config.json

Place a lex.config.json file at the project root (the same directory as composer.json). This is the only configuration file Lex needs:
{
  "viewPaths":  ["views", "resources/views"],
  "production": false,
  "sandbox":    false
}
All paths in viewPaths may be relative (resolved from the config file’s own directory) or absolute.

Config fields

FieldTypeDefaultDescription
viewPathsstring[]["views"]Directories scanned (in order) for .lex template files
productionboolfalseEnable production mode on startup — skips source-file checks and disables the debugger
sandboxboolfalseEnable secure sandbox mode — restricts function calls and forbids raw echo by default
The cache directory is always placed at {projectRoot}/.lexer/ and is not configurable in lex.config.json. To override the cache location in code, use the fluent ->cache(string $dir) method.
3

Verify the cache directory

Lex creates and manages the .lexer/ directory automatically when you first render a template. You do not need to create it manually. The layout is:
PathContents
.lexer/compiled/Compiled PHP files ({md5}.php)
.lexer/ast/Serialised AST snapshots ({md5}.ast)
.lexer/compiled/index.phpPrecompiled view index (production mode only)
.lexer/view_dependencies.jsonDependency graph used for automatic cache invalidation
Add .lexer/ to your .gitignore — compiled output should not be committed:
echo ".lexer/" >> .gitignore
4

Optional extensions

mbstring

Recommended. Ensures correct multibyte character handling inside template tokenisation. Most PHP installations include it by default.

igbinary

Optional. When installed, Lex automatically switches to igbinary_serialize / igbinary_unserialize for AST snapshot storage, reducing cache file sizes and improving serialisation speed.

The bin/lex CLI

After installation the bin/lex binary is available at vendor/bin/lex. It reads the same lex.config.json that Lexer::fromConfig() uses, so no extra setup is required.
vendor/bin/lex --help
Use the CLI to warm up the compiled template cache before deploying to production — this ensures all templates are pre-compiled and ready to serve from the precompiled index.

Build docs developers (and LLMs) love