Skip to content

Commit 0b50521

Browse files
authored
Merge pull request #192 from Laravel-Backpack/fix-publish-command
Fix publish widget path
2 parents ed513f2 + bdf4c8a commit 0b50521

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

src/Console/Commands/Views/PublishOrCreateViewBackpackCommand.php

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,30 @@ abstract class PublishOrCreateViewBackpackCommand extends GeneratorCommand
1010
{
1111
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
1212

13+
/**
14+
* The source file to copy from.
15+
*/
16+
public ?string $sourceFile = null;
17+
18+
/**
19+
* The source file view namespace.
20+
*/
21+
public ?string $sourceViewNamespace = null;
22+
23+
/**
24+
* Stub file name.
25+
*
26+
* @var string
27+
*/
28+
protected $stub = '';
29+
30+
/**
31+
* View Namespace.
32+
*
33+
* @var string
34+
*/
35+
protected $viewNamespace = '';
36+
1337
/**
1438
* Get the stub file for the generator.
1539
*
@@ -27,6 +51,12 @@ protected function getStub()
2751
*/
2852
public function handle()
2953
{
54+
$this->setupSourceFile();
55+
56+
if ($this->sourceFile === false) {
57+
return false;
58+
}
59+
3060
$name = Str::of($this->getNameInput());
3161
$path = Str::of($this->getPath($name));
3262
$pathRelative = $path->after(base_path())->replace('\\', '/')->trim('/');
@@ -40,45 +70,49 @@ public function handle()
4070
return false;
4171
}
4272

43-
$source = null;
73+
$this->makeDirectory($path);
74+
75+
if ($this->sourceFile) {
76+
$this->files->copy($this->sourceFile, $path);
77+
} else {
78+
$this->files->put($path, $this->buildClass($name));
79+
}
80+
81+
$this->closeProgressBlock();
82+
}
83+
84+
private function setupSourceFile()
85+
{
4486
if ($this->option('from')) {
4587
$from = $this->option('from');
4688
$namespaces = ViewNamespaces::getFor($this->viewNamespace);
4789
foreach ($namespaces as $namespace) {
4890
$viewPath = "$namespace.$from";
91+
4992
if (view()->exists($viewPath)) {
50-
$source = view($viewPath)->getPath();
93+
$this->sourceFile = view($viewPath)->getPath();
94+
$this->sourceViewNamespace = $viewPath;
5195
break;
5296
}
5397
}
5498

5599
// full or relative file path may be provided
56100
if (file_exists($from)) {
57-
$source = realpath($from);
101+
$this->sourceFile = realpath($from);
58102
}
59103
// remove the first slash to make absolute paths relative in unix systems
60104
elseif (file_exists(substr($from, 1))) {
61-
$source = realpath(substr($from, 1));
105+
$this->sourceFile = realpath(substr($from, 1));
62106
}
63107

64-
if (! $source) {
108+
if (! $this->sourceFile) {
65109
$this->errorProgressBlock();
66110
$this->note("$this->type '$from' does not exist!", 'red');
67111
$this->newLine();
68112

69-
return false;
113+
$this->sourceFile = false;
70114
}
71115
}
72-
73-
$this->makeDirectory($path);
74-
75-
if ($source) {
76-
$this->files->copy($source, $path);
77-
} else {
78-
$this->files->put($path, $this->buildClass($name));
79-
}
80-
81-
$this->closeProgressBlock();
82116
}
83117

84118
/**

src/Console/Commands/Views/WidgetBackpackCommand.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Backpack\Generators\Console\Commands\Views;
44

5+
use Illuminate\Support\Str;
6+
57
class WidgetBackpackCommand extends PublishOrCreateViewBackpackCommand
68
{
79
/**
@@ -54,6 +56,20 @@ class WidgetBackpackCommand extends PublishOrCreateViewBackpackCommand
5456
*/
5557
protected function getPath($name)
5658
{
57-
return resource_path("views/vendor/backpack/base/{$this->viewNamespace}/$name.blade.php");
59+
if ($this->sourceViewNamespace) {
60+
$themePath = Str::contains($this->sourceViewNamespace, '::') ?
61+
Str::before($this->sourceViewNamespace, '::') :
62+
Str::beforeLast($this->sourceViewNamespace, '.');
63+
64+
$themePath = Str::replace('.', '/', $themePath);
65+
66+
$path = 'views/vendor/'.$themePath.'/'.$this->viewNamespace.'/'.$name.'.blade.php';
67+
68+
return resource_path($path);
69+
}
70+
71+
$path = 'views/vendor/backpack/ui/'.$this->viewNamespace.'/'.$name.'.blade.php';
72+
73+
return resource_path($path);
5874
}
5975
}

0 commit comments

Comments
 (0)