Install the Package
composer require a9f/fractor-fluid --dev
Ensure it’s added to your devDependencies (as it’s a tooling package).
Register the Processor
In your Fractor configuration (typically config/fractor.php), add the FluidFileProcessor to the $processors array:
'processors' => [
\A9F\FractorFluid\Processor\FluidFileProcessor::class,
],
First Use Case: Basic Fluid Template Analysis
Run Fractor with the --analyze flag to scan .html or .fluid files (common Fluid extensions):
php artisan fractor:analyze app/Resources/Views/
The processor will parse Fluid syntax (e.g., {namespace}, {layout}, {section}) and flag issues like:
namespace directives.{section} blocks.{foo} vs. {bar.baz}).Define Custom Rules
Create a rule class implementing FluidFractorRule:
use A9F\FractorFluid\Contract\FluidFractorRule;
class CustomFluidRule implements FluidFractorRule {
public function check(string $content): array {
$errors = [];
if (strpos($content, '{invalid_syntax}') !== false) {
$errors[] = 'Invalid Fluid syntax detected.';
}
return $errors;
}
}
Tag and Register the Rule
Bind the rule in a service provider (e.g., AppServiceProvider):
public function register() {
$this->app->tag([CustomFluidRule::class], 'fractor.fluid_rule');
}
Trigger Analysis in CI/CD
Add a script to your composer.json:
"scripts": {
"test:fluid": "php artisan fractor:analyze resources/views --format=json > fluid-report.json"
}
Use the output to gate deployments (e.g., fail if errors exist).
Laravel Blade Compatibility:
If using Blade + Fluid hybrid templates, exclude Blade files from analysis by filtering file extensions in FluidFileProcessor.
protected function shouldProcess(string $path): bool {
return str_ends_with($path, ['.fluid', '.html']);
}
Dynamic Rule Loading:
Load rules from a config file (e.g., config/fractor/fluid_rules.php) for maintainability:
'rules' => [
\App\Rules\DeprecatedFluidTagRule::class,
\App\Rules\SectionNamingConventionRule::class,
],
Pre-commit Hooks:
Use husky or pre-commit to run Fractor on Fluid files before commits:
# .husky/pre-commit
php artisan fractor:analyze resources/views --format=compact
False Positives in Complex Templates
<!--{...}-->) or escaped braces (\{...}) may trigger false errors.FluidFileProcessor to skip commented/escaped blocks:
protected function extractFluidBlocks(string $content): array {
// Add logic to ignore comments/escaped braces
return preg_match_all('/\{\{.*?\}\}/s', $content, $matches);
}
Performance with Large Projects
// In FluidFileProcessor
protected function getFilesToProcess(): array {
return array_diff(scandir($this->path), ['.git', 'node_modules']);
}
Rule Conflicts
namespace and "invalid root block").FluidFractorRule:
public function getPriority(): int {
return 10; // Lower = higher priority
}
Enable Verbose Output:
Run with --verbose to see raw Fluid parsing:
php artisan fractor:analyze --verbose
Inspect Processor Logic:
Override FluidFileProcessor methods like process() to log intermediate steps:
public function process(string $content): string {
\Log::debug('Processing: ', ['content' => $content]);
return parent::process($content);
}
Custom Fluid Dialects
Extend FluidFileProcessor to support non-standard Fluid syntax (e.g., TYPO3-specific tags):
protected function getFluidPattern(): string {
return '/\{\{.*?\}\}/s'; // Override regex for custom syntax
}
Post-Processing Hooks
Add callbacks after rule checks via the afterCheck event:
// In a service provider
\Event::listen('fractor.fluid.after_check', function ($content, $errors) {
if (count($errors)) {
\Log::warning('Fluid errors found:', $errors);
}
});
Visual Studio Code Integration
Use the fractor-report.json output to create a VSCode problem matcher for inline errors:
// .vscode/settings.json
"problemMatcher": [
{
"pattern": {
"regexp": "^(.+?):(\\d+):\\d+:\\s+(warning|error)\\s+(.*)$",
"file": 1,
"line": 2,
"severity": 3,
"message": 4
}
}
]
How can I help you explore Laravel packages today?