Skip to content

Commit

Permalink
Update to Livewire 3
Browse files Browse the repository at this point in the history
  • Loading branch information
georgechitechi committed Jan 23, 2025
1 parent 747d867 commit 3de65fc
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 91 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": "^7|^8",
"laravel/framework": ">=9.19",
"laravel/ui": "^4",
"livewire/livewire": "^2"
"livewire/livewire": "^3"
},
"require-dev": {},
"autoload": {
Expand All @@ -35,7 +35,6 @@
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"

},
"config": {
"sort-packages": true
Expand All @@ -50,4 +49,4 @@
}
}
}
}
}
4 changes: 4 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@
'namespace' => 'App\Http\Controllers',
],

'livewire' => [
'namespace' => 'App\Livewire',
],

];
4 changes: 2 additions & 2 deletions resources/install/package.json.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
52 changes: 27 additions & 25 deletions src/Commands/LivewireGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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}")
);
}

Expand Down Expand Up @@ -358,7 +360,7 @@ protected function getFilteredColumns()
return array_filter($columns, function ($value) use ($unwanted) {
return !in_array($value, $unwanted);
});
}
}

/**
* Make model attributes/replacements.
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
118 changes: 59 additions & 59 deletions src/Commands/LivewireInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -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: <info>' . $deleteFile . '</info>');
(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: <info>npm install && npm run build</info> 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 <info>php artisan crud:generate {table-name}</info> 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: <info>' . $deleteFile . '</info>');
}
}

$this->stubDir = __DIR__ . '/../../resources/install';
$this->generateFiles();

$this->line('');
$this->warn('Running: <info>npm install && npm run build</info> 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 <info>php artisan crud:generate {table-name}</info> 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()));
Expand Down
2 changes: 1 addition & 1 deletion src/stubs/views/form-field.stub
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="form-group">
<label for="{{column}}"></label>
<input wire:model="{{column}}" type="{{type}}" class="form-control" id="{{column}}" placeholder="{{title}}">@error('{{column}}') <span class="error text-danger">{{ $message }}</span> @enderror
<input wire:model.live="{{column}}" type="{{type}}" class="form-control" id="{{column}}" placeholder="{{title}}">@error('{{column}}') <span class="error text-danger">{{ $message }}</span> @enderror
</div>
2 changes: 1 addition & 1 deletion src/stubs/views/view.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<div wire:poll.4s class="btn btn-sm btn-success" style="margin-top:0px; margin-bottom:0px;"> {{ session('message') }} </div>
@endif
<div>
<input wire:model='keyWord' type="text" class="form-control" name="search" id="search" placeholder="Search {{modelTitle}}s">
<input wire:model.live='keyWord' type="text" class="form-control" name="search" id="search" placeholder="Search {{modelTitle}}s">
</div>
<div class="btn btn-sm btn-info" data-bs-toggle="modal" data-bs-target="#createDataModal">
<i class="bi-plus-lg"></i> Add {{modelTitle}}s
Expand Down

0 comments on commit 3de65fc

Please sign in to comment.