Installation Add the package via Composer:
composer require drjele/symfony-printer
No Laravel-specific setup is required since this is a Symfony utility package, but it can be used in Laravel via Symfony’s components or as a standalone utility.
First Use Case
Import the Printer class and use it to format output:
use DrJele\Printer\Printer;
$printer = new Printer();
$output = $printer->print('Hello, Laravel!');
echo $output; // Renders formatted output (e.g., colored, styled)
Where to Look First
src/Printer.php for core methods like print(), error(), success(), etc.Console/Style/StyleInterface for CLI output (this package may wrap similar functionality).CLI Output Styling
Use Printer for colored, formatted console output in Laravel Artisan commands:
use DrJele\Printer\Printer;
class MyCommand extends Command {
protected function handle() {
$printer = new Printer();
$printer->success('Task completed!');
$printer->error('Failed to process: ' . $e->getMessage());
}
}
Logging Integration Extend Laravel’s logging to include formatted messages:
use DrJele\Printer\Printer;
use Illuminate\Support\Facades\Log;
Log::info((new Printer())->info('User logged in: ' . $user->name));
API Response Formatting Format API responses with structured output (e.g., for debugging):
return response()->json([
'data' => $data,
'debug' => (new Printer())->dump($data, 'Data structure')
]);
success(), warning(), error()) for uniform CLI output across commands.dump() or table() methods for structured variable inspection in Tinker or logs.Printer in unit tests to isolate output logic:
$printer = Mockery::mock(Printer::class);
$printer->shouldReceive('success')->once();
Illuminate\Support\Facades\Console, combine with Symfony’s Style interface for advanced features.Printer to add project-specific styles (e.g., custom colors for warnings).Printer to Laravel’s container for easy access:
$this->app->singleton(Printer::class, function () {
return new Printer();
});
Console components. If using in Laravel, ensure compatibility (e.g., avoid Symfony-specific features like Symfony\Component\Console\Output\OutputInterface directly).flush() or disable buffering:
$printer->print('Message')->flush();
Log facade methods or Blade directives).dump() may not throw exceptions. Check return values or use try-catch:
try {
$printer->dump($variable);
} catch (\Exception $e) {
Log::error('Dump failed: ' . $e->getMessage());
}
NO_COLOR env var).config/printer.php exists. Customize via constructor or DI.DrJele\Printer\Printer to add methods:
class MyPrinter extends Printer {
public function customOutput($message) {
return $this->print($message)->withStyle('bg=blue;fg=white');
}
}
success()) before rendering.Printer output in event listeners:
event(new \App\Events\UserRegistered($user));
// In listener:
$printer->success('User registered: ' . $user->email);
How can I help you explore Laravel packages today?