s1k3/livewire-v4-patch
Dev-only Laravel tool to convert Livewire v4 class components into Model-Focused Components (MFC). Provides an Artisan command to convert single components or whole folders, with options to keep originals, generate JS/CSS, exclude directories, and disable Pint formatting.
Installation:
composer require --dev s1k3/livewire-v4-patch
php artisan vendor:publish --provider="LivewireV4\LivewireV4PatchServiceProvider"
config/livewire-v4-patch.php exists in your project.First Use Case: Convert a single class component to MFC:
php artisan convert-class-to:mfc app/Livewire/MyComponent.php
resources/views/components/my-component.blade.php (or your configured mfc_component_path).Quick Check:
php artisan list to see the new command.Batch Conversion: Use the command in a loop for all class components:
find app/Livewire -name "*.php" | xargs -I {} php artisan convert-class-to:mfc {}
excluded_directories in config (e.g., ['tests', 'stubs']).Integration with Livewire v4:
use Livewire\Component; with use Livewire\Component; (if needed) in generated MFCs.mount() to public function mount() if using dependency injection.Asset Management:
create_js/create_css in config to auto-generate:
'create_js' => true,
'create_css' => true,
resources/js/components/ and resources/css/components/ (adjust paths if needed).Partial Conversions:
--partial flag to convert only specific methods/views:
php artisan convert-class-to:mfc MyComponent.php --partial
Testing:
php artisan livewire:discover to validate MFC registration.Namespace Conflicts:
resources/views/components/{kebab-case}.blade.php.user-profile vs. user_profile).State Management:
public $property becomes public $property; in MFCs.public $reactiveProp) may need manual adjustment if using Alpine.js or custom logic.Asset Paths:
href="{{ asset('css/app.css') }}") in class components break in MFCs.@vite() or @asset() directives in Blade templates.Method Overrides:
render() methods are converted to Blade views.render() logic (e.g., PDF generation) won’t work—move to a separate method.Dependency Injection:
public function __construct(Foo $foo)) may not convert cleanly.mount() for dependencies in MFCs:
public function mount(Foo $foo) { ... }
Dry Run:
--dry-run to preview changes without writing files:
php artisan convert-class-to:mfc MyComponent.php --dry-run
Log Output:
php artisan convert-class-to:mfc MyComponent.php --verbose
Config Validation:
class_component_path and mfc_component_path are correct.'class_component_path' => base_path('app/Http/Livewire'),
Custom Templates:
php artisan vendor:publish --tag="livewire-v4-patch-templates"
resources/views/vendor/livewire-v4-patch/mfc.blade.php.Post-Conversion Hooks:
LivewireV4Patch::afterConversion(function ($componentName) {
// Example: Auto-register the MFC in a config file
});
Emoji Support:
emoji: true for fun but avoid in production:
'emoji' => env('APP_ENV') === 'local',
Global CSS:
global_css: true to output CSS in a single file (e.g., resources/css/app.css).Batch Processing:
find app/Livewire -name "*.php" | head -n 20 | xargs -I {} php artisan convert-class-to:mfc {}
Exclude Tests:
'excluded_directories' => ['tests', 'stubs', 'Livewire/Testing'],
Backup First:
app/Livewire directory before bulk conversions:
cp -r app/Livewire app/Livewire.backup
How can I help you explore Laravel packages today?