stillat/blade-parser
Parse, analyze, and transform Laravel Blade templates with a robust PHP parser. stillat/blade-parser provides an AST, tokenization, and utilities to inspect directives, components, and expressions—ideal for linters, formatters, editors, and automated refactoring tools.
composer require stillat/blade-parseruse stillat\BladeParser\Parser;
$parser = new Parser();
$document = $parser->parseString('@extends('layouts.app') @section('content') Hello {{ $name }} @endsection');
$document->getNodes() — each node has a type (e.g., @extends, @section, {{ }}) and metadata (content, position).examples/ in the repo for basic traversal and transformation use cases.@php blocks, detect unused sections).@yield to @section + @show, or normalize spacing around directives).@include paths) for dependency graphs.@auth sugar) since they’re treated as raw tokens in the AST — useful for auditing non-core Blade extensions.Workflow Tip: Always parse from disk (not rendered output!) using
$parser->parseFile('resources/views/dashboard.blade.php')to get accurate source mapping.
@if passes parsing). Implement your own structural validation.@class, @props) may parse inconsistently — verify via $node->rawContent if AST node is unexpected.@endphp, @endsection) are preserved as-is; use $node->getSourceContext()->getLine() to correlate with original line numbers.$document->getSource() for exact round-tripping, or the SourceTransformer class for surgical edits.TokenStream or implement custom NodeVisitor for deep static analysis (e.g., detect Blade expressions with unresolved variables via {{ $var->method() }}).How can I help you explore Laravel packages today?