jakub-onderka/php-console-highlighter
Abandoned package for syntax-highlighting PHP code in the terminal using ANSI colors. Provides a Highlighter that outputs highlighted whole-file or snippet content for CLI display. Suggested alternative: php-parallel-lint/PHP-Console-Highlighter.
Installation:
composer require jakub-onderka/php-console-highlighter
Ensure your composer.json includes the package under require.
Basic Usage:
use JakubOnderka\PhpConsoleHighlighter\Highlighter;
use JakubOnderka\PhpConsoleColor\ConsoleColor;
$highlighter = new Highlighter(new ConsoleColor());
$highlighted = $highlighter->getWholeFile(file_get_contents('path/to/file.php'));
echo $highlighted;
First Use Case: Highlight a single PHP file in a CLI script (e.g., for debugging or documentation generation):
$filePath = 'app/Services/UserService.php';
echo $highlighter->getWholeFile(file_get_contents($filePath));
Syntax Highlighting in CLI Tools:
Integrate into custom CLI commands (e.g., php artisan extensions) to display code snippets with syntax highlighting:
// In a custom Artisan command
$this->info($highlighter->getWholeFile($this->getFileContent()));
Dynamic Code Display:
Use in Laravel’s telescope:commands or debugbar extensions to show highlighted code blocks in logs or debug panels.
API Responses: Highlight code snippets in JSON API responses (e.g., for error messages or documentation endpoints):
return response()->json([
'error' => 'Invalid query',
'example' => $highlighter->highlight('SELECT * FROM users WHERE id = ?;')
]);
Service Provider Bootstrapping: Bind the highlighter to the container for global access:
// In AppServiceProvider
$this->app->singleton(Highlighter::class, function ($app) {
return new Highlighter(new ConsoleColor());
});
Then inject via constructor:
public function __construct(private Highlighter $highlighter) {}
Partial Highlighting: Highlight specific code segments (e.g., from a string or file range):
$code = '<?php class User { public $name; }';
echo $highlighter->highlight($code);
@verbatim + highlighted code).Artisan::command() to output highlighted code for help or inspect subcommands.$this->failures[] = $highlighter->highlight($failedTestCode);
Deprecated Package:
php-parallel-lint/PHP-Console-Highlighter.Tokenizer Dependency:
ext-tokenizer extension. Verify with:
php -m | grep tokenizer
nikic/php-parser if tokenizer is unavailable.Performance:
vendor/ or node_modules/) may cause memory issues. Stream content or limit file sizes:
$highlighter->highlight(file_get_contents($filePath, false, null, 0, 1024 * 100)); // Read first 100KB
Color Compatibility:
xterm, iTerm2, or enable ANSI support in CMD:
reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 1
Invalid Code:
try {
echo $highlighter->highlight($invalidCode);
} catch (\Exception $e) {
echo "Error highlighting code: " . $e->getMessage();
}
highlight_string() as a fallback for broken code.Custom Themes:
ConsoleColor to modify syntax colors (e.g., for dark terminals):
$customColor = new ConsoleColor();
$customColor->setStyle('keyword', 'bold yellow');
$highlighter = new Highlighter($customColor);
Custom Highlighters:
JakubOnderka\PhpConsoleHighlighter\HighlighterInterface for custom logic (e.g., language-specific highlighting).File Caching:
$cacheKey = 'highlighted_' . md5($filePath);
$highlighted = Cache::remember($cacheKey, now()->addHours(1), function () use ($highlighter, $filePath) {
return $highlighter->getWholeFile(file_get_contents($filePath));
});
Web Integration:
spatie/array-to-html + custom ANSI-to-CSS mapping).How can I help you explore Laravel packages today?