Skip to content

fix: improve templator component variable extraction and cache invalidation#505

Merged
SonyPradana merged 9 commits into
masterfrom
fix/templator-component-directive
Apr 26, 2026
Merged

fix: improve templator component variable extraction and cache invalidation#505
SonyPradana merged 9 commits into
masterfrom
fix/templator-component-directive

Conversation

@SonyPradana

Copy link
Copy Markdown
Owner
Q A
Is bugfix? Yes
New feature? No
Breaks BC? No
Fixed issues #501

This PR improves the Templator component by addressing several issues reported in #501:

  • Variable Extraction: Added support for {{ $var }} and {!! $var !!} syntax inside component templates by extracting passed parameters into the component scope.
  • Cache Invalidation: Implemented dependency tracking for templates. Changes in components or included files now correctly trigger a recompilation of the parent template.
  • Enhanced Parameter Parsing: Improved the regex for {% component %} to support commas within string literals and varied whitespace around named parameters.
  • PHP Block Protection: Updated NameTemplator to ignore content within tags, preventing double parsing of variable syntax.
  • Multiline Comments: Updated CommentTemplator with the /s modifier and moved it to the beginning of the compilation chain to ensure comments are handled first.
  • Custom Exceptions: Added RequiredVariableNotFound and YieldSectionNotFound for better error diagnosis.

…dation

- Add support for variable extraction in components using {{  }} syntax
- Implement dependency-aware cache invalidation to recompile templates when components change
- Enhance parameter parsing to support commas in strings and flexible whitespace
- Protect existing PHP blocks from being parsed by NameTemplator
- Support multiline comments and ensure they are processed first
- Introduce custom exceptions YieldSectionNotFound and RequiredVariableNotFound
- Add .dep file generation to track template dependencies (includes,
components, etc.)
- Implement automatic cache invalidation when any dependency is modified
- Add `isFresh()` method to Templator for efficient stale cache
detection
- Introduce `$use_dep` parameter to `render()` and `compile()` for
optional validation
- Refactor `ViewCommand` to handle .dep files in `view:clear` and
improve `watch` efficiency
- Fix typos and logic in `Templator::prependDependency`
- Update tests to ensure .dep files are properly managed and cleaned up
- Fix PHPStan issues and exclude internal vendor directories from
analysis
@SonyPradana

Copy link
Copy Markdown
Owner Author

PR Documentation: Smart Dependency Tracking for Templator

Overview

This update introduces robust dependency tracking to solve the long-standing issue where changes to included files (headers, footers, components) don't trigger cache updates. The core philosophy: "Performance First, Smart Features as Opt-In." By default, the library remains as fast as raw PHP with intelligent invalidation available on demand.


Key Features

1. Dependency Mapping (.dep files)

Every compiled template now generates a companion .dep file:

  • [hash].php — Compiled PHP code
  • [hash].dep — Serialized map of all dependencies (including nested)

2. Intelligent Cache Invalidation

The new isFresh() method validates cache by checking modification timestamps of every file in the .dep map. If any component is newer than the cache, automatic re-compile triggers.

3. Refined API

Method Behavior
render() Checks cache; uses $use_dep (default: false) to validate nested dependencies
compile() Forces full re-compile; always generates .dep map

Usage Strategies

Development Mode

// Option A: Reactive (Smart Check)
$templator->render('home', $data, cache: true, use_dep: true);

// Option B: Proactive (CLI Watcher)
// Terminal: php cli view:watch
$templator->render('home', $data, cache: true, use_dep: false);

Production Mode

// 1. Deployment: php cli view:cache
// 2. Runtime:
$templator->render('home', $data, cache: true, use_dep: false);

CLI Enhancements

  • view:cache — Builds .dep maps for all templates
  • view:clear — Removes both .php and corresponding .dep files
  • view:watch — Uses .dep maps to auto-update parent templates when components change

Performance

  • With .dep check: ~0.09ms latency
  • Without .dep check: 0.00ms overhead (raw PHP speed)

The .dep system uses serialized arrays and native filemtime() checks—significantly faster than regex re-parsing.


Quality Standards

✅ DRY principles (logic in isFresh() and prependDependency())
✅ Typo fixes (perentparent, has_depethhas_depth)
✅ Type safety validated with PHPStan Level 6 (zero errors)

Comment thread src/System/Integrate/Console/ViewCommand.php Outdated
@SonyPradana SonyPradana merged commit 2c8a33e into master Apr 26, 2026
12 of 13 checks passed
@SonyPradana SonyPradana deleted the fix/templator-component-directive branch April 26, 2026 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants