sclable/xml-lint
Command-line tool to lint XML files and validate them against referenced XSD schemas. Works on single files or entire directories (optionally recursive), with verbose output, exclusions, and the ability to skip schema validation. Install via Composer and run vendor/bin/xmllint.
Installation Add the package via Composer (ensure PHP 8.1+ for compatibility):
composer require sclable/xml-lint
Publish the config (if needed):
php artisan vendor:publish --provider="Sclable\XmlLint\XmlLintServiceProvider" --tag="config"
First Command Run a basic XML lint check on a file:
php artisan xml:lint path/to/your/file.xml
For a directory of XML files:
php artisan xml:lint path/to/xml/directory --recursive
Quick Validation Validate against a custom XSD schema:
php artisan xml:lint path/to/file.xml --schema=path/to/schema.xsd
CI/CD Pipeline
Add to phpunit.xml or .github/workflows/ for pre-commit/pre-push checks:
<php>
<env name="XML_LINT" value="1"/>
</php>
Trigger via:
php artisan xml:lint storage/xml --recursive --fail-on-error
Laravel Artisan Command Extend the base command for custom logic (PHP 8.4+ compatible):
use Sclable\XmlLint\Commands\LintXmlCommand;
class CustomXmlLintCommand extends LintXmlCommand {
protected $signature = 'xml:custom-lint {path} {--schema=} {--recursive}';
public function handle(): void {
$this->lint($this->argument('path'), [
'schema' => $this->option('schema'),
'recursive' => $this->option('recursive'),
'fail_on_error' => true,
]);
}
}
Event-Driven Validation
Hook into file uploads (e.g., file.uploaded event) to validate XML attachments:
use Sclable\XmlLint\Facades\XmlLint;
Event::listen('file.uploaded', function ($file) {
if ($file->getClientOriginalExtension() === 'xml') {
XmlLint::lint($file->getRealPath())->validate();
}
});
API Response Validation Validate XML payloads in API routes (PHP 8.4+ features like named arguments supported):
use Sclable\XmlLint\Facades\XmlLint;
Route::post('/api/validate', function (Request $request) {
$xml = $request->xmlPayload; // Assume XML payload
$result = XmlLint::lint($xml)->validate();
return response()->json($result->toArray());
});
PHP Version Compatibility
composer require php:^8.1
Schema Validation Failures
php artisan xml:lint --schema-only path/to/schema.xsd
Recursive Directory Traversal
--max-depth or exclude directories via .xml-lintignore.Memory Limits
memory_limit..env or process files in chunks:
php artisan xml:lint --chunk-size=500 path/to/large.xml
php artisan xml:lint --verbose path/to/file.xml
XmlLint::setErrorHandler(fn ($error) => {
Log::error("XML Lint Error: " . $error->getMessage());
throw new \RuntimeException($error->getMessage());
});
Custom Rules Extend the validator to add domain-specific rules (PHP 8.4+ compatible):
use Sclable\XmlLint\Contracts\XmlValidator;
class CustomXmlValidator implements XmlValidator {
public function validate(string $xml, ?string $schema = null): array {
// Add custom logic (e.g., check for required tags)
return parent::validate($xml, $schema);
}
}
Register in config/xml-lint.php:
'validators' => [
'custom' => \App\Validators\CustomXmlValidator::class,
],
Pre/Post-Processing Use hooks to transform XML before/after linting (PHP 8.4+ arrow functions):
XmlLint::preProcess(fn ($xml) => str_replace('<root>', '<custom-root>', $xml));
Integration with Laravel Filesystem
Use Storage::disk('xml')->lint() for cloud storage validation:
use Sclable\XmlLint\Facades\XmlLint;
$result = XmlLint::lint(storage_path('app/xml/file.xml'));
How can I help you explore Laravel packages today?