diff --git a/composer.json b/composer.json index d67a6fd..f8c37e3 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ } ], "require": { - "backpack/crud": "^6.0" + "backpack/crud": "^7.0.0-alpha" }, "autoload": { "psr-4": { diff --git a/src/Console/Commands/Views/ChipBackpackCommand.php b/src/Console/Commands/Views/ChipBackpackCommand.php new file mode 100644 index 0000000..16d673b --- /dev/null +++ b/src/Console/Commands/Views/ChipBackpackCommand.php @@ -0,0 +1,166 @@ +setupChipSourceFile(); + + if ($this->sourceFile === false) { + return false; + } + + $name = Str::of($this->getNameInput()); + $path = Str::of($this->getPath($name)); + $pathRelative = $path->after(base_path())->replace('\\', '/')->trim('/'); + + $this->infoBlock("Creating {$name->replace('_', ' ')->title()} {$this->type}"); + $this->progressBlock("Creating view {$pathRelative}"); + + if ($this->alreadyExists($name)) { + $this->closeProgressBlock('Already existed', 'yellow'); + + return false; + } + + $this->makeDirectory($path); + + if ($this->sourceFile) { + $this->files->copy($this->sourceFile, $path); + } else { + $this->files->put($path, $this->buildClass($name)); + } + + $this->closeProgressBlock(); + } + + /** + * Setup the source file for the --from option. + * This method extends the parent functionality to also look in vendor chip directories. + */ + private function setupChipSourceFile() + { + if ($this->option('from')) { + $from = $this->option('from'); + + // First, try the standard view namespaces (parent behavior) + $namespaces = ViewNamespaces::getFor($this->viewNamespace); + foreach ($namespaces as $namespace) { + $viewPath = "$namespace.$from"; + + if (view()->exists($viewPath)) { + $this->sourceFile = view($viewPath)->getPath(); + $this->sourceViewNamespace = $viewPath; + + return; + } + } + + // Second, check vendor chip directory + $vendorChipPath = base_path("vendor/backpack/crud/src/resources/views/crud/chips/{$from}.blade.php"); + if (file_exists($vendorChipPath)) { + $this->sourceFile = $vendorChipPath; + + // Don't set sourceViewNamespace so it uses the default path logic + return; + } + + // Third, try full or relative file paths (parent behavior) + if (file_exists($from)) { + $this->sourceFile = realpath($from); + + return; + } + // remove the first slash to make absolute paths relative in unix systems + elseif (file_exists(substr($from, 1))) { + $this->sourceFile = realpath(substr($from, 1)); + + return; + } + + // If nothing found, show error + $this->errorProgressBlock(); + $this->note("$this->type '$from' does not exist!", 'red'); + $this->newLine(); + + $this->sourceFile = false; + } + } + + /** + * Get the destination class path. + * + * @param string $name + * @return string + */ + protected function getPath($name) + { + if ($this->sourceViewNamespace) { + $themePath = Str::contains($this->sourceViewNamespace, '::') ? + Str::before($this->sourceViewNamespace, '::') : + Str::beforeLast($this->sourceViewNamespace, '.'); + + $themePath = Str::replace('.', '/', $themePath); + + $path = 'views/vendor/'.$themePath.'/'.$this->viewNamespace.'/'.$name.'.blade.php'; + + return resource_path($path); + } + + $path = 'views/vendor/backpack/crud/'.$this->viewNamespace.'/'.$name.'.blade.php'; + + return resource_path($path); + } +} diff --git a/src/Console/stubs/chip.stub b/src/Console/stubs/chip.stub new file mode 100644 index 0000000..78a832e --- /dev/null +++ b/src/Console/stubs/chip.stub @@ -0,0 +1,29 @@ +@php + // ---------------------------------- + // Create any variables you need here + // ---------------------------------- + // If you're using this chip as a column inside ListOperation, or as a widget inside ShowOperation, + // you will most likely have the $entry variable. +@endphp + +@include('crud::chips.general', [ + 'heading' => [ + 'content' => $entry->name, + 'href' => backpack_url('example/'.$entry->id.'/show'), + ], + 'image' => [ + 'content' => asset($entry->avatar->url), + ], + 'details' => [ + [ + 'icon' => 'la la-shopping-cart', + 'content' => $entry->invoices->count(). ' purchases', + 'title' => 'Number of purchases: '.$entry->invoices->count(), + ], + [ + 'icon' => 'la la-calendar', + 'content' => $entry->created_at->format('F j, Y'), + 'title' => 'Sign up date: '.$last_purchase, + ] + ] +]) diff --git a/src/GeneratorsServiceProvider.php b/src/GeneratorsServiceProvider.php index 833daf5..1b891b9 100644 --- a/src/GeneratorsServiceProvider.php +++ b/src/GeneratorsServiceProvider.php @@ -18,6 +18,7 @@ use Backpack\Generators\Console\Commands\RequestBackpackCommand; use Backpack\Generators\Console\Commands\ViewBackpackCommand; use Backpack\Generators\Console\Commands\Views\ButtonBackpackCommand; +use Backpack\Generators\Console\Commands\Views\ChipBackpackCommand; use Backpack\Generators\Console\Commands\Views\ColumnBackpackCommand; use Backpack\Generators\Console\Commands\Views\FieldBackpackCommand; use Backpack\Generators\Console\Commands\Views\FilterBackpackCommand; @@ -29,6 +30,7 @@ class GeneratorsServiceProvider extends ServiceProvider protected $commands = [ BuildBackpackCommand::class, ButtonBackpackCommand::class, + ChipBackpackCommand::class, ColumnBackpackCommand::class, ConfigBackpackCommand::class, CrudModelBackpackCommand::class,