memio/pretty-printer
Memio PrettyPrinter is an opinionated PHP code generator that takes a Model and uses a TemplateEngine to produce “pretty” code. Outputs a string you can save to files, print to console, or render in web pages.
## Getting Started
### **First Steps**
1. **Installation**
```bash
composer require memio/pretty-printer
Register the service provider in config/app.php:
'providers' => [
// ...
Memio\PrettyPrinter\PrettyPrinterServiceProvider::class,
],
Basic Usage
Generate a template for a model (e.g., User) in resources/views/pretty-printer/user.blade.php:
<div class="user-card">
<h2>{{ $model->name }}</h2>
<p>Email: {{ $model->email }}</p>
</div>
First Command Run the generator for a model:
php artisan pretty-printer:generate User
Outputs rendered HTML to storage/app/pretty-printer/user.html.
resources/views/pretty-printer/ (customize per model).config/pretty-printer.php (adjust paths, defaults, or caching).php artisan pretty-printer:generate (CLI entry point).\PrettyPrinter::generate(Model::class) (programmatic use).Dynamic Admin Panel Cards
Generate reusable HTML cards for Eloquent models (e.g., Post, Order) and embed them in a dashboard:
$post = Post::find(1);
echo \PrettyPrinter::generate('Post', $post);
Result: Renders the post.blade.php template with $post data.
Extend a base template (resources/views/pretty-printer/base.blade.php) for shared layouts:
<!-- base.blade.php -->
<!DOCTYPE html>
<html>
<head><title>{{ $title ?? 'Pretty Print' }}</title></head>
<body>
@yield('content')
</body>
</html>
<!-- user.blade.php -->
@extends('pretty-printer::base')
@section('content')
{{ parent() }} <!-- Reuses base content -->
<div class="user-meta">
{{ $model->created_at->diffForHumans() }}
</div>
@endsection
Pass additional data via the with() method:
\PrettyPrinter::generate('User', $user)
->with(['status' => 'Active'])
->render();
Template access: {{ $status }} (merged with $model data).
Use Laravel’s Blade conditionals in templates:
<!-- user.blade.php -->
@if($model->is_admin)
<span class="badge admin">Admin</span>
@endif
Generate JSON responses from templates:
return response()->json([
'html' => \PrettyPrinter::generate('Product', $product)->toHtml(),
]);
Cache rendered output for performance:
// Cache for 1 hour
\PrettyPrinter::generate('Order', $order)
->cache(3600)
->render();
Stored in: storage/framework/cache/pretty-printer/.
Generate templates for multiple models:
php artisan pretty-printer:generate User Post Order
Register Blade directives for reusable logic:
// In a service provider
Blade::directive('avatar', function ($expression) {
return "<?php echo \$model->avatarUrl($expression); ?>";
});
Usage in template:
<img src="{{ avatar('thumb') }}">
New in v3.0.1 Set up a local development environment with Docker:
# Clone the repo (if applicable) or initialize your project
docker-compose up -d
docker-compose exec app bash
make test for PHPStan analysis.make lint for PHP CS Fixer (v2.19.3).make rector for code refactoring.Template Paths
pretty-printer:generate fails if resources/views/pretty-printer/ doesn’t exist.config/pretty-printer.php:
'view_path' => 'custom/path/to/templates',
Model Not Found
generate(Model::class) throws ModelNotFoundException if the model isn’t registered.\PrettyPrinter::generate(\App\Models\User::class, $user);
Caching Conflicts
php artisan cache:clear
Or disable caching for dynamic data:
->cache(0)
Blade Syntax Errors
php artisan view:clear and validate via Tinker:
php artisan tinker
>>> \Blade::renderFile('resources/views/pretty-printer/user.blade.php', ['model' => new App\Models\User]);
Docker-Specific Issues
storage/ in Docker.docker-compose.yml:
volumes:
- ./storage:/var/www/html/storage
make permissions to fix ownership issues.Enable Debug Mode
Set debug to true in config/pretty-printer.php to log template paths and errors.
Inspect Rendered Output
Use ->toHtml() to debug before final rendering:
$html = \PrettyPrinter::generate('User', $user)->toHtml();
dd($html); // Debug output
Check Compiled Views
Compiled Blade templates are stored in bootstrap/cache/. Delete them to force recompilation:
rm -rf bootstrap/cache/*
Leverage Docker Tools
make test
make lint
Custom Storage Override the storage path in config:
'storage_path' => storage_path('app/pretty-printer-custom'),
Event Listeners
Listen for PrettyPrinter.Generated events to post-process output:
// In EventServiceProvider
protected $listen = [
'PrettyPrinter.Generated' => [
\App\Listeners\LogGeneratedTemplate::class,
],
];
Template Overrides Override default templates by publishing assets:
php artisan vendor:publish --tag=pretty-printer-views
Published to: resources/views/vendor/pretty-printer/.
Middleware Add middleware to templates (e.g., auth checks):
\PrettyPrinter::generate('AdminUser', $user)
->middleware(\App\Http\Middleware\CheckAdmin::class);
Docker Extensions
docker-compose.yml.Makefile targets to automate repetitive tasks (e.g., make setup for initial setup).Use for API Responses Combine with Laravel’s API resources for dynamic HTML snippets:
return new PrettyPrinterResource($model);
Localization
Support multiple languages by passing a locale parameter:
\PrettyPrinter::generate('Post', $post)->locale('es');
Template access: {{ __($model->title) }}.
Dark Mode Templates
Create dual templates (e.g., user.blade.php and user-dark.blade.php) and switch via config:
'theme' => env('PRETTY_PRINTER_THEME', 'light'),
Headless CMS Integration
Use to render content from a database (e.g., Page model) without a traditional view layer.
Testing Mock the printer in tests:
$printer = Mockery::mock(\Memio\PrettyPrinter\PrettyPrinter::class);
$printer->shouldReceive('generate')
->with('User', $user
How can I help you explore Laravel packages today?