PHP code example of deinte / laravel-blade-validator
1. Go to this page and download the library: Download deinte/laravel-blade-validator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Deinte\BladeValidator\Facades\BladeValidator;
// Validate a single file
$result = BladeValidator::validateFile('/path/to/template.blade.php');
// Validate multiple files
$result = BladeValidator::validateFiles([
'/path/to/template1.blade.php',
'/path/to/template2.blade.php',
]);
// Validate a directory
$result = BladeValidator::validateDirectory(resource_path('views'));
// Check results
if (!$result->valid) {
foreach ($result->errors as $error) {
echo "{$error->file}:{$error->line} - [{$error->rule}] {$error->message}\n";
}
}
use Deinte\BladeValidator\BladeValidator;
class MyController
{
public function validateViews(BladeValidator $validator)
{
$result = $validator->validateDirectory(resource_path('views'));
return response()->json($result->toArray());
}
}
namespace App\BladeRules;
use Deinte\BladeValidator\Data\BladeValidationError;
use Deinte\BladeValidator\Rules\BladeRuleInterface;
class NoTodoComments implements BladeRuleInterface
{
public function getName(): string
{
return 'no-todo-comments';
}
public function getDescription(): string
{
return 'Detects TODO comments in Blade templates.';
}
public function getDefaultSeverity(): string
{
return 'warning';
}
public function validate(string $content, string $filePath): array
{
$errors = [];
if (preg_match_all('/\{\{--.*TODO.*--\}\}/i', $content, $matches, PREG_OFFSET_CAPTURE)) {
foreach ($matches[0] as $match) {
$line = substr_count(substr($content, 0, $match[1]), "\n") + 1;
$errors[] = new BladeValidationError(
file: $filePath,
line: $line,
rule: $this->getName(),
message: 'TODO comment found in template.',
severity: $this->getDefaultSeverity(),
);
}
}
return $errors;
}
}
use Deinte\BladeValidator\BladeValidator;
use App\BladeRules\NoTodoComments;
public function boot(BladeValidator $validator)
{
$validator->addRule(new NoTodoComments());
}