|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Backpack\Generators\Console\Commands\Views; |
| 4 | + |
| 5 | +use Backpack\CRUD\ViewNamespaces; |
| 6 | +use Illuminate\Support\Str; |
| 7 | + |
| 8 | +class ChipBackpackCommand extends PublishOrCreateViewBackpackCommand |
| 9 | +{ |
| 10 | + /** |
| 11 | + * The console command name. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $name = 'backpack:chip'; |
| 16 | + |
| 17 | + /** |
| 18 | + * The name and signature of the console command. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $signature = 'backpack:chip {name?} {--from=}'; |
| 23 | + |
| 24 | + /** |
| 25 | + * The console command description. |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected $description = 'Generate a Backpack chip'; |
| 30 | + |
| 31 | + /** |
| 32 | + * The type of class being generated. |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + protected $type = 'Chip'; |
| 37 | + |
| 38 | + /** |
| 39 | + * View Namespace. |
| 40 | + * |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + protected $viewNamespace = 'chips'; |
| 44 | + |
| 45 | + /** |
| 46 | + * Stub file name. |
| 47 | + * |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + protected $stub = 'chip.stub'; |
| 51 | + |
| 52 | + /** |
| 53 | + * Execute the console command. |
| 54 | + * |
| 55 | + * @return bool|null |
| 56 | + */ |
| 57 | + public function handle() |
| 58 | + { |
| 59 | + $this->setupChipSourceFile(); |
| 60 | + |
| 61 | + if ($this->sourceFile === false) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + $name = Str::of($this->getNameInput()); |
| 66 | + $path = Str::of($this->getPath($name)); |
| 67 | + $pathRelative = $path->after(base_path())->replace('\\', '/')->trim('/'); |
| 68 | + |
| 69 | + $this->infoBlock("Creating {$name->replace('_', ' ')->title()} {$this->type}"); |
| 70 | + $this->progressBlock("Creating view <fg=blue>{$pathRelative}</>"); |
| 71 | + |
| 72 | + if ($this->alreadyExists($name)) { |
| 73 | + $this->closeProgressBlock('Already existed', 'yellow'); |
| 74 | + |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + $this->makeDirectory($path); |
| 79 | + |
| 80 | + if ($this->sourceFile) { |
| 81 | + $this->files->copy($this->sourceFile, $path); |
| 82 | + } else { |
| 83 | + $this->files->put($path, $this->buildClass($name)); |
| 84 | + } |
| 85 | + |
| 86 | + $this->closeProgressBlock(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Setup the source file for the --from option. |
| 91 | + * This method extends the parent functionality to also look in vendor chip directories. |
| 92 | + */ |
| 93 | + private function setupChipSourceFile() |
| 94 | + { |
| 95 | + if ($this->option('from')) { |
| 96 | + $from = $this->option('from'); |
| 97 | + |
| 98 | + // First, try the standard view namespaces (parent behavior) |
| 99 | + $namespaces = ViewNamespaces::getFor($this->viewNamespace); |
| 100 | + foreach ($namespaces as $namespace) { |
| 101 | + $viewPath = "$namespace.$from"; |
| 102 | + |
| 103 | + if (view()->exists($viewPath)) { |
| 104 | + $this->sourceFile = view($viewPath)->getPath(); |
| 105 | + $this->sourceViewNamespace = $viewPath; |
| 106 | + |
| 107 | + return; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Second, check vendor chip directory |
| 112 | + $vendorChipPath = base_path("vendor/backpack/crud/src/resources/views/crud/chips/{$from}.blade.php"); |
| 113 | + if (file_exists($vendorChipPath)) { |
| 114 | + $this->sourceFile = $vendorChipPath; |
| 115 | + |
| 116 | + // Don't set sourceViewNamespace so it uses the default path logic |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + // Third, try full or relative file paths (parent behavior) |
| 121 | + if (file_exists($from)) { |
| 122 | + $this->sourceFile = realpath($from); |
| 123 | + |
| 124 | + return; |
| 125 | + } |
| 126 | + // remove the first slash to make absolute paths relative in unix systems |
| 127 | + elseif (file_exists(substr($from, 1))) { |
| 128 | + $this->sourceFile = realpath(substr($from, 1)); |
| 129 | + |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + // If nothing found, show error |
| 134 | + $this->errorProgressBlock(); |
| 135 | + $this->note("$this->type '$from' does not exist!", 'red'); |
| 136 | + $this->newLine(); |
| 137 | + |
| 138 | + $this->sourceFile = false; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Get the destination class path. |
| 144 | + * |
| 145 | + * @param string $name |
| 146 | + * @return string |
| 147 | + */ |
| 148 | + protected function getPath($name) |
| 149 | + { |
| 150 | + if ($this->sourceViewNamespace) { |
| 151 | + $themePath = Str::contains($this->sourceViewNamespace, '::') ? |
| 152 | + Str::before($this->sourceViewNamespace, '::') : |
| 153 | + Str::beforeLast($this->sourceViewNamespace, '.'); |
| 154 | + |
| 155 | + $themePath = Str::replace('.', '/', $themePath); |
| 156 | + |
| 157 | + $path = 'views/vendor/'.$themePath.'/'.$this->viewNamespace.'/'.$name.'.blade.php'; |
| 158 | + |
| 159 | + return resource_path($path); |
| 160 | + } |
| 161 | + |
| 162 | + $path = 'views/vendor/backpack/crud/'.$this->viewNamespace.'/'.$name.'.blade.php'; |
| 163 | + |
| 164 | + return resource_path($path); |
| 165 | + } |
| 166 | +} |
0 commit comments