Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Console Highlighter Laravel Package

php-parallel-lint/php-console-highlighter

Highlight PHP source code in the terminal with readable, colorized syntax. php-console-highlighter works with ConsoleColor and can highlight whole files or snippets—ideal for CLI tools, linters, and test output to quickly spot errors and context.

View on GitHub
Deep Wiki
Context7

Getting Started

This package provides terminal syntax highlighting for PHP code—primarily useful for CLI tools and output formatting. Start by requiring it via Composer:

composer require php-parallel-lint/php-console-highlighter

Usage is minimal: instantiate PHP_Parallel_Lint\ConsoleHighlighter, then call highlight() with PHP source code. It integrates directly with STDOUT (or any stream) for colored output. A common first use case is enhancing CLI tool output (e.g., in artisan commands, custom linting scripts, or IDE integrations) to make PHP snippets easier to read.

Note: As of v1.0.0, the namespace has changed from JakubOnderka\ConsoleHighlighter to PHP_Parallel_Lint\ConsoleHighlighter. Update imports accordingly.

Implementation Patterns

  • Direct CLI output: Pass a resource (e.g., STDOUT) to the constructor for immediate colored rendering:
    use PHP_Parallel_Lint\ConsoleHighlighter;
    
    $highlighter = new ConsoleHighlighter(STDOUT);
    $highlighter->highlight("<?php echo 'Hello';");
    
  • String output: Use highlightString() to capture highlighted code as an ANSI-formatted string (e.g., for logging or embedding in multi-step CLI output):
    $output = $highlighter->highlightString($code);
    fwrite(STDERR, $output);
    
  • Integration with Laravel artisan commands: In a custom Command class, inject or instantiate the highlighter and use it inside handle() to output structured PHP snippets (e.g., showing generated boilerplate, scaffolding templates, or config diffs) with clear syntax cues. Ensure your command checks for ANSI support ($this->output->isVerbose() or OutputInterface::OUTPUT_RAW).
  • Lightweight syntax checker companion: Pair with tools like php-parallel-lint/parallel-lint—highlight diffs or problematic code blocks after linting failures.

Gotchas and Tips

  • Namespace breaking change: The top-level namespace is now PHP_Parallel_Lint (not JakubOnderka). Replace use JakubOnderka\ConsoleHighlighter; with use PHP_Parallel_Lint\ConsoleHighlighter; to avoid fatal errors.
  • ANSI compatibility: Output won’t render correctly on Windows terminals without symfony/polyfill-ansi or enabling Windows 10 virtual terminal support. Check !_windows() or wrap with stream_isatty(STDOUT).
  • Improved stability & bug fixes: v1.0.0 resolves several line-numbering and PHP 8.0+ tokenization bugs (e.g., readonly properties, match expressions) and restores PHP 5.3 compatibility—making syntax handling more robust across legacy and modern code.
  • Color disabled? Respect NO_COLOR environment variable or --no-ansi flag in Laravel by checking OutputInterface::isVerbose() or environment state before highlighting.
  • Extending highlighting: Custom lexing still isn’t officially supported—but you can fork and extend the Lexer class if needed for internal tooling.
  • Dependency update: The package now uses php-console-color ^1.0.1, improving cross-platform color handling and reducing edge-case rendering issues.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport