Require the package
Run the following command in your project root: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.Create lex.config.json
Place a All paths in
lex.config.json file at the project root (the same directory as composer.json). This is the only configuration file Lex needs:viewPaths may be relative (resolved from the config file’s own directory) or absolute.Config fields
| Field | Type | Default | Description |
|---|---|---|---|
viewPaths | string[] | ["views"] | Directories scanned (in order) for .lex template files |
production | bool | false | Enable production mode on startup — skips source-file checks and disables the debugger |
sandbox | bool | false | Enable 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.Verify the cache directory
Lex creates and manages the
Add
.lexer/ directory automatically when you first render a template. You do not need to create it manually. The layout is:| Path | Contents |
|---|---|
.lexer/compiled/ | Compiled PHP files ({md5}.php) |
.lexer/ast/ | Serialised AST snapshots ({md5}.ast) |
.lexer/compiled/index.php | Precompiled view index (production mode only) |
.lexer/view_dependencies.json | Dependency graph used for automatic cache invalidation |
.lexer/ to your .gitignore — compiled output should not be committed: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.