From 3de65fc6f7d34922f7abdaa6c85a0285951f0913 Mon Sep 17 00:00:00 2001 From: George Chitechi Date: Thu, 23 Jan 2025 03:09:31 +0300 Subject: [PATCH] Update to Livewire 3 --- composer.json | 5 +- config/config.php | 4 + resources/install/package.json.stub | 4 +- src/Commands/LivewireGeneratorCommand.php | 52 +++++----- src/Commands/LivewireInstall.php | 118 +++++++++++----------- src/stubs/views/form-field.stub | 2 +- src/stubs/views/view.stub | 2 +- 7 files changed, 96 insertions(+), 91 deletions(-) diff --git a/composer.json b/composer.json index c184504..f03a8e4 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "php": "^7|^8", "laravel/framework": ">=9.19", "laravel/ui": "^4", - "livewire/livewire": "^2" + "livewire/livewire": "^3" }, "require-dev": {}, "autoload": { @@ -35,7 +35,6 @@ "scripts": { "test": "vendor/bin/phpunit", "test-coverage": "vendor/bin/phpunit --coverage-html coverage" - }, "config": { "sort-packages": true @@ -50,4 +49,4 @@ } } } -} +} \ No newline at end of file diff --git a/config/config.php b/config/config.php index 0b733ec..72657e5 100644 --- a/config/config.php +++ b/config/config.php @@ -46,4 +46,8 @@ 'namespace' => 'App\Http\Controllers', ], + 'livewire' => [ + 'namespace' => 'App\Livewire', + ], + ]; diff --git a/resources/install/package.json.stub b/resources/install/package.json.stub index eee3b67..38f6c1d 100644 --- a/resources/install/package.json.stub +++ b/resources/install/package.json.stub @@ -13,7 +13,7 @@ "laravel-vite-plugin": "^1.0", "lodash": "^4.17", "postcss": "^8.4", - "sass": "^1.77", - "vite": "^5.3" + "sass": "1.76.0", + "vite": "^6.0" } } \ No newline at end of file diff --git a/src/Commands/LivewireGeneratorCommand.php b/src/Commands/LivewireGeneratorCommand.php index 999bfb5..edf91e4 100644 --- a/src/Commands/LivewireGeneratorCommand.php +++ b/src/Commands/LivewireGeneratorCommand.php @@ -64,12 +64,12 @@ abstract class LivewireGeneratorCommand extends Command * Controller Namespace. * @var string */ - protected $controllerNamespace = 'App\Http\Controllers'; - /** + protected $controllerNamespace = 'App\Http\Controllers'; + /** * Controller Namespace. * @var string */ - protected $livewireNamespace = 'App\Http\Livewire'; + protected $livewireNamespace = 'App\Livewire'; /** * Application Layout @@ -180,14 +180,14 @@ private function _getSpace($no = 1) */ protected function _getMigrationPath($name) { - return base_path("database/migrations/". date('Y-m-d_His') ."_create_". Str::lower(Str::plural($name)) ."_table.php"); - } + return base_path("database/migrations/" . date('Y-m-d_His') . "_create_" . Str::lower(Str::plural($name)) . "_table.php"); + } protected function _getFactoryPath($name) { return base_path("database/factories/{$name}Factory.php"); - } + } - /** + /** * @param $name * @return string */ @@ -274,7 +274,9 @@ protected function getField($title, $column, $type = 'form-field') ]); return str_replace( - array_keys($replace), array_values($replace), $this->getStub("views/{$type}") + array_keys($replace), + array_values($replace), + $this->getStub("views/{$type}") ); } @@ -358,7 +360,7 @@ protected function getFilteredColumns() return array_filter($columns, function ($value) use ($unwanted) { return !in_array($value, $unwanted); }); - } + } /** * Make model attributes/replacements. @@ -421,52 +423,52 @@ protected function modelReplacements() // CSV format return implode(', ', $filterColumns); - }; + }; - $resetfields = function () { + $resetfields = function () { /** @var array $filterColumns Exclude the unwanted columns */ $filterColumns = $this->getFilteredColumns(); // Add quotes to the unwanted columns for fillable array_walk($filterColumns, function (&$value) { - $value = "\n\t\t\$this->". $value . " = null"; + $value = "\n\t\t\$this->" . $value . " = null"; $value .= ";"; }); // CSV format return implode('', $filterColumns); - }; - - $addfields = function () { + }; + + $addfields = function () { /** @var array $filterColumns Exclude the unwanted columns */ $filterColumns = $this->getFilteredColumns(); // Add quotes to the unwanted columns for fillable array_walk($filterColumns, function (&$value) { - $value = "\n\t\t\t'" . $value . "' => \$this-> ". $value; + $value = "\n\t\t\t'" . $value . "' => \$this-> " . $value; }); // CSV format return implode(',', $filterColumns); - }; - - $keyWord = function () { + }; + + $keyWord = function () { /** @var array $filterColumns Exclude the unwanted columns */ $filterColumns = $this->getFilteredColumns(); // Add quotes to the unwanted columns for fillable array_walk($filterColumns, function (&$value) { - $value = "\n\t\t\t\t\t\t->orWhere('" . $value . "', 'LIKE', \$keyWord)"; + $value = "\n\t\t\t\t\t\t->orWhere('" . $value . "', 'LIKE', \$keyWord)"; }); // CSV format return implode('', $filterColumns); - }; + }; - $factoryfields = function () { + $factoryfields = function () { /** @var array $filterColumns Exclude the unwanted columns */ $filterColumns = $this->getFilteredColumns(); @@ -479,15 +481,15 @@ protected function modelReplacements() // CSV format return implode('', $filterColumns); }; - - $editfields = function () { + + $editfields = function () { /** @var array $filterColumns Exclude the unwanted columns */ $filterColumns = $this->getFilteredColumns(); // Add quotes to the unwanted columns for fillable array_walk($filterColumns, function (&$value) { - $value = "\n\t\t\$this->" . $value . " = \$record-> ". $value .";"; + $value = "\n\t\t\$this->" . $value . " = \$record-> " . $value . ";"; }); // CSV format diff --git a/src/Commands/LivewireInstall.php b/src/Commands/LivewireInstall.php index 2791971..67eb258 100644 --- a/src/Commands/LivewireInstall.php +++ b/src/Commands/LivewireInstall.php @@ -9,76 +9,76 @@ class LivewireInstall extends Command { - protected $filesystem; + protected $filesystem; protected $stubDir; protected $argument; private $replaces = []; - + protected $signature = 'crud:install'; protected $description = 'Install Livewire CRUD Generator, compile and publish it\'s assets'; public function handle() { $this->filesystem = new Filesystem; - (new Filesystem)->ensureDirectoryExists(app_path('Http/Livewire')); - (new Filesystem)->ensureDirectoryExists(app_path('Http/Controllers')); - (new Filesystem)->ensureDirectoryExists(app_path('Models')); - (new Filesystem)->ensureDirectoryExists(resource_path('views/livewire')); - - if ($this->confirm('This will delete compiled assets in public folder. It will Re-Compile this. Do you want to proceed?') == 'yes') { - if ($this->confirm('Do you want to scaffold Authentication files? Only skip if you have authentication system on your App') == 'yes') { - Artisan::call('ui:auth', [], $this->getOutput()); - } - $routeFile = base_path('routes/web.php'); - $string = file_get_contents($routeFile); - if (!str_contains($string, '//Route Hooks - Do not delete//')) { - file_put_contents($routeFile, "\n//Route Hooks - Do not delete//", FILE_APPEND); - } - $deleteFiles = [ - 'resources/sass', - 'resources/css', - 'resources/js', - 'public/css', - 'public/js', - 'public/build', - 'public/fonts', - ]; - - foreach ($deleteFiles as $deleteFile) { - if ($this->filesystem->exists($deleteFile)) { - $this->filesystem->delete($deleteFile); - $this->filesystem->deleteDirectory($deleteFile); - $this->warn('Deleted file: ' . $deleteFile . ''); + (new Filesystem)->ensureDirectoryExists(app_path('Livewire')); + (new Filesystem)->ensureDirectoryExists(app_path('Http/Controllers')); + (new Filesystem)->ensureDirectoryExists(app_path('Models')); + (new Filesystem)->ensureDirectoryExists(resource_path('views/livewire')); + + if ($this->confirm('This will delete compiled assets in public folder. It will Re-Compile this. Do you want to proceed?') == 'yes') { + if ($this->confirm('Do you want to scaffold Authentication files? Only skip if you have authentication system on your App') == 'yes') { + Artisan::call('ui:auth', [], $this->getOutput()); } - } - - $this->stubDir = __DIR__ . '/../../resources/install'; - $this->generateFiles(); - - $this->line(''); - $this->warn('Running: npm install && npm run build Please wait...'); - $this->line(''); - - exec('npm install && npm run build'); - - $this->info('Installation Complete, few seconds please, let us optimize your site'); - $this->warn(''); - $this->warn('Removing Dumped node_modules files. Please wait...'); - - tap(new Filesystem, function ($npm) { - $npm->deleteDirectory(base_path('node_modules')); - $npm->delete(base_path('yarn.lock')); - $npm->delete(base_path('webpack.mix.js')); - $npm->delete(base_path('package-lock.json')); - }); - $this->info('node_modules files Removed'); - $this->info(''); - $this->warn('All set, run php artisan crud:generate {table-name} to build your CRUD'); - } - else $this->warn('Installation Aborted, No file was changed'); + $routeFile = base_path('routes/web.php'); + $string = file_get_contents($routeFile); + if (!str_contains($string, '//Route Hooks - Do not delete//')) { + file_put_contents($routeFile, "\n//Route Hooks - Do not delete//", FILE_APPEND); + } + $deleteFiles = [ + 'resources/sass', + 'resources/css', + 'resources/js', + 'public/css', + 'public/js', + 'public/build', + 'public/fonts', + ]; + + foreach ($deleteFiles as $deleteFile) { + if ($this->filesystem->exists($deleteFile)) { + $this->filesystem->delete($deleteFile); + $this->filesystem->deleteDirectory($deleteFile); + $this->warn('Deleted file: ' . $deleteFile . ''); + } + } + + $this->stubDir = __DIR__ . '/../../resources/install'; + $this->generateFiles(); + + $this->line(''); + $this->warn('Running: npm install && npm run build Please wait...'); + $this->line(''); + + exec('npm install && npm run build'); + + $this->info('Installation Complete, few seconds please, let us optimize your site'); + $this->warn(''); + $this->warn('Removing Dumped node_modules files. Please wait...'); + + tap(new Filesystem, function ($npm) { + $npm->deleteDirectory(base_path('node_modules')); + $npm->delete(base_path('yarn.lock')); + $npm->delete(base_path('webpack.mix.js')); + $npm->delete(base_path('package-lock.json')); + }); + $this->info('node_modules files Removed'); + $this->info(''); + $this->warn('All set, run php artisan crud:generate {table-name} to build your CRUD'); + } else + $this->warn('Installation Aborted, No file was changed'); } - - public function generateFiles() + + public function generateFiles() { foreach ($this->filesystem->allFiles($this->stubDir, true) as $file) { $filePath = $this->replace(Str::replaceLast('.stub', '', $file->getRelativePathname())); diff --git a/src/stubs/views/form-field.stub b/src/stubs/views/form-field.stub index 31d50c8..429e7fe 100644 --- a/src/stubs/views/form-field.stub +++ b/src/stubs/views/form-field.stub @@ -1,4 +1,4 @@
- @error('{{column}}') {{ $message }} @enderror + @error('{{column}}') {{ $message }} @enderror
\ No newline at end of file diff --git a/src/stubs/views/view.stub b/src/stubs/views/view.stub index 27f56c5..338bad1 100644 --- a/src/stubs/views/view.stub +++ b/src/stubs/views/view.stub @@ -13,7 +13,7 @@
{{ session('message') }}
@endif
- +
Add {{modelTitle}}s