Documentation

Class: Compiler

Full name: Clarity\Engine\Compiler

Compile Clarity template sources into PHP classes.

Traits in Clarity\Engine\Compiler\ implement the compiler stages. The pipeline resolves inheritance and includes, tokenizes the source, generates PHP, then adds the class metadata and source map.

Each template becomes one class with its render method and freshness/error mapping metadata. Engine-bound PHP variables use the __c_ prefix to avoid collisions with template variables. Source maps use the packed SourceMap format to keep generated code compact.

Metadata uses line comments to avoid retaining docblocks in OPcache; the constructor and render method retain theirs.

Rendering owns an output buffer. On failure, nested buffers are drained back to the level it opened without closing caller buffers. No finally block is used because success closes the buffer, and a template may close it early.

Public Constants

  • COMPILER_VERSION = 36
  • INTERNAL_PREFIX = '__c_'

Public methods

__construct() ·

public function __construct(): mixed

Return value

  • Type: mixed

default() ·

public static function default(): static

Return value

  • Type: static

setRegistry() ·

public function setRegistry(Clarity\Engine\Registry $registry): static

Parameters

Name Type Default Description
$registry Registry -

Return value

  • Type: static

setDebugMode() ·

public function setDebugMode(bool $debug): static

Parameters

Name Type Default Description
$debug bool -

Return value

  • Type: static

setPolicy() ·

public function setPolicy(Clarity\Engine\Policy $policy): static

Set the policy shared by the compiler and tokenizer.

Parameters

Name Type Default Description
$policy Policy -

Return value

  • Type: static

compile() ·

public function compile(string $templateName, Clarity\Template\TemplateLoader $loader): Clarity\Engine\CompiledTemplate

Compile a template and return a CompiledTemplate value object.

Parameters

Name Type Default Description
$templateName string - Logical template name (e.g. 'home', 'admin::dashboard').
$loader TemplateLoader - Loader used to fetch source for this template and its
dependencies (extends parents, includes).

Return value

Throws


registerVar() ·

public function registerVar(string $name, int|null $tplLine = null): mixed

Register a local variable in the compile-time context.

This is the extension point a custom directive uses to bind a variable it emits itself. The name is a PHP local that nothing writes back into the scope array, like a loop variable. It is also recorded as a dynamic binding, so a vars() snapshot inside the directive's scope includes it. If the same name is also stored in $__c_va, the snapshot shows the $__c_va entry.

Parameters

Name Type Default Description
$name string - The name of the variable to register.
$tplLine int|null null Line of the directive that requested the
registration, when known. It is only used to
point the error at the offending directive
rather than at a bare variable name.

Return value

  • Type: mixed

unregisterVar() ·

public function unregisterVar(string $name): mixed

Unregister a local variable from the compile-time context.

Parameters

Name Type Default Description
$name string - The name of the variable to unregister.

Return value

  • Type: mixed

getVars() ·

public function getVars(): array

Get the currently registered local variables.

Return value

  • Type: array
  • Description: Map of local variable names to their PHP representations.

Back to the Index