Installation Add the bundle via Composer:
composer require anja/ghost-hunter-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Anja\GhostHunterBundle\GhostHunterBundle::class => ['all' => true],
];
First Run Execute the CLI command to scan for unused files:
php bin/console ghosthunter:scan
Review the generated report in var/log/ghosthunter_report.log (or configured output path).
Quick Use Case Identify unused Twig templates or JavaScript/CSS assets in a Symfony project before refactoring or cleanup.
Pre-Refactoring Audit
Run ghosthunter:scan before major refactoring to identify safe-to-remove files:
php bin/console ghosthunter:scan --format=json > unused_files.json
CI/CD Integration
Add to a post-deploy or pre-cleanup script to enforce unused file removal:
# .github/workflows/deploy.yml
- name: Scan for unused files
run: php bin/console ghosthunter:scan --fail-on-unused
Custom Rules
Extend the scanner by creating a custom rule (e.g., ignore files in src/Old/):
// config/packages/ghost_hunter.yaml
ghost_hunter:
ignored_paths:
- '%kernel.project_dir%/src/Old/'
assets.json).ghosthunter.scan.complete to process results programmatically:
// src/EventListener/GhostHunterListener.php
public function onScanComplete(ScanCompleteEvent $event) {
$unusedFiles = $event->getUnusedFiles();
// Log or notify team
}
False Positives
{{ asset('dynamic-' ~ id ~ '.js') }}) may flag files as unused.--ignore-dynamic flag or whitelist paths in config.Performance
php bin/console ghosthunter:scan --path=templates,assets
Cache Dependencies
php bin/console cache:clear) before scanning.Asset Build Tools
publicPath misconfiguration may break asset detection.assets.json is up-to-date and publicPath matches your setup.--verbose to debug scanning logic:
php bin/console ghosthunter:scan --verbose
--dry-run to preview changes without modifying files.Custom Scanners
Implement Anja\GhostHunterBundle\Scanner\ScannerInterface to add support for new file types (e.g., PHP includes):
class MyCustomScanner implements ScannerInterface {
public function scan(ScanContext $context) {
// Custom logic here
}
}
Register in services.yaml:
services:
App\Scanner\MyCustomScanner:
tags: [ghosthunter.scanner]
Post-Scan Actions
Use the ghosthunter.scan.complete event to trigger cleanup or notifications:
// config/services.yaml
App\EventListener\PostScanListener:
tags:
- { name: kernel.event_listener, event: ghosthunter.scan.complete, method: onScanComplete }
Configuration Overrides
Override default settings (e.g., report format, thresholds) in config/packages/ghost_hunter.yaml:
ghost_hunter:
report:
format: json
path: '%kernel.project_dir%/var/unused_files.json'
thresholds:
unused_templates: 10 # Fail if >10 unused templates
How can I help you explore Laravel packages today?