Skip to content

Commit b126a5b

Browse files
pxpmtabacituStyleCIBot
authored
Add support for Backpack v7, remove support for v6 (#210)
Co-authored-by: Cristian Tabacitu <[email protected]> Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Cristian Tăbăcitu <[email protected]>
1 parent 2795434 commit b126a5b

File tree

4 files changed

+198
-1
lines changed

4 files changed

+198
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
}
2626
],
2727
"require": {
28-
"backpack/crud": "^6.0"
28+
"backpack/crud": "^7.0"
2929
},
3030
"autoload": {
3131
"psr-4": {
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
}

src/Console/stubs/chip.stub

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
@php
2+
// ----------------------------------
3+
// Create any variables you need here
4+
// ----------------------------------
5+
// If you're using this chip as a column inside ListOperation, or as a widget inside ShowOperation,
6+
// you will most likely have the $entry variable.
7+
@endphp
8+
9+
@include('crud::chips.general', [
10+
'heading' => [
11+
'content' => $entry->name,
12+
'href' => backpack_url('example/'.$entry->id.'/show'),
13+
],
14+
'image' => [
15+
'content' => asset($entry->avatar->url),
16+
],
17+
'details' => [
18+
[
19+
'icon' => 'la la-shopping-cart',
20+
'content' => $entry->invoices->count(). ' purchases',
21+
'title' => 'Number of purchases: '.$entry->invoices->count(),
22+
],
23+
[
24+
'icon' => 'la la-calendar',
25+
'content' => $entry->created_at->format('F j, Y'),
26+
'title' => 'Sign up date: '.$last_purchase,
27+
]
28+
]
29+
])

src/GeneratorsServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Backpack\Generators\Console\Commands\RequestBackpackCommand;
1919
use Backpack\Generators\Console\Commands\ViewBackpackCommand;
2020
use Backpack\Generators\Console\Commands\Views\ButtonBackpackCommand;
21+
use Backpack\Generators\Console\Commands\Views\ChipBackpackCommand;
2122
use Backpack\Generators\Console\Commands\Views\ColumnBackpackCommand;
2223
use Backpack\Generators\Console\Commands\Views\FieldBackpackCommand;
2324
use Backpack\Generators\Console\Commands\Views\FilterBackpackCommand;
@@ -29,6 +30,7 @@ class GeneratorsServiceProvider extends ServiceProvider
2930
protected $commands = [
3031
BuildBackpackCommand::class,
3132
ButtonBackpackCommand::class,
33+
ChipBackpackCommand::class,
3234
ColumnBackpackCommand::class,
3335
ConfigBackpackCommand::class,
3436
CrudModelBackpackCommand::class,

0 commit comments

Comments
 (0)