Skip to content

Commit 1a773bf

Browse files
authored
Merge pull request #155 from Laravel-Backpack/create-publish-views
Fixes on create/publish views commands
2 parents da74e3b + 0697b83 commit 1a773bf

File tree

6 files changed

+223
-173
lines changed

6 files changed

+223
-173
lines changed

src/Console/Commands/ButtonBackpackCommand.php

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Backpack\Generators\Console\Commands;
44

5+
use Backpack\CRUD\ViewNamespaces;
56
use Illuminate\Console\GeneratorCommand;
67
use Illuminate\Support\Str;
78

89
class ButtonBackpackCommand extends GeneratorCommand
910
{
1011
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
12+
1113
/**
1214
* The console command name.
1315
*
@@ -20,7 +22,7 @@ class ButtonBackpackCommand extends GeneratorCommand
2022
*
2123
* @var string
2224
*/
23-
protected $signature = 'backpack:button {name}';
25+
protected $signature = 'backpack:button {name} {--from=}';
2426

2527
/**
2628
* The console command description.
@@ -37,51 +39,77 @@ class ButtonBackpackCommand extends GeneratorCommand
3739
protected $type = 'Button';
3840

3941
/**
40-
* Get the stub file for the generator.
42+
* View Namespace.
4143
*
42-
* @return string
44+
* @var string
4345
*/
44-
protected function getStub()
45-
{
46-
return __DIR__.'/../stubs/button.stub';
47-
}
46+
protected $viewNamespace = 'buttons';
4847

4948
/**
50-
* Alias for the fire method.
49+
* Get the stub file for the generator.
5150
*
52-
* In Laravel 5.5 the fire() method has been renamed to handle().
53-
* This alias provides support for both Laravel 5.4 and 5.5.
51+
* @return string
5452
*/
55-
public function handle()
53+
protected function getStub()
5654
{
57-
$this->fire();
55+
return __DIR__.'/../stubs/button.stub';
5856
}
5957

6058
/**
6159
* Execute the console command.
6260
*
6361
* @return bool|null
6462
*/
65-
public function fire()
63+
public function handle()
6664
{
6765
$name = Str::of($this->getNameInput());
68-
$path = $this->getPath($name);
66+
$path = Str::of($this->getPath($name));
67+
$pathRelative = $path->after(base_path())->replace('\\', '/')->trim('/');
6968

70-
if ($this->alreadyExists($this->getNameInput())) {
71-
$this->error($this->type.' already existed!');
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');
7274

7375
return false;
7476
}
7577

76-
$this->infoBlock("Creating {$name->replace('_', ' ')->title()} {$this->type}");
77-
$this->progressBlock("Creating view <fg=blue>resources/views/vendor/backpack/crud/buttons/${name}.blade.php</>");
78+
$source = null;
79+
if ($this->option('from')) {
80+
$from = $this->option('from');
81+
$namespaces = ViewNamespaces::getFor($this->viewNamespace);
82+
foreach ($namespaces as $namespace) {
83+
$viewPath = "$namespace.$from";
84+
if (view()->exists($viewPath)) {
85+
$source = view($viewPath)->getPath();
86+
break;
87+
}
88+
}
89+
90+
// full file path may be provided
91+
if (file_exists($from)) {
92+
$source = $from;
93+
}
94+
95+
if (! $source) {
96+
$this->errorProgressBlock();
97+
$this->note("$this->type '$from' does not exist!", 'red');
98+
$this->newLine();
99+
100+
return false;
101+
}
102+
}
78103

79104
$this->makeDirectory($path);
80-
$this->files->put($path, $this->buildClass($name));
105+
106+
if ($source) {
107+
$this->files->copy($source, $path);
108+
} else {
109+
$this->files->put($path, $this->buildClass($name));
110+
}
81111

82112
$this->closeProgressBlock();
83-
$this->newLine();
84-
$this->info($this->type.' created successfully.');
85113
}
86114

87115
/**
@@ -119,16 +147,4 @@ protected function buildClass($name)
119147

120148
return $stub;
121149
}
122-
123-
/**
124-
* Get the console command options.
125-
*
126-
* @return array
127-
*/
128-
protected function getOptions()
129-
{
130-
return [
131-
132-
];
133-
}
134150
}

src/Console/Commands/ColumnBackpackCommand.php

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
class ColumnBackpackCommand extends GeneratorCommand
1010
{
1111
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
12+
1213
/**
1314
* The console command name.
1415
*
@@ -38,73 +39,77 @@ class ColumnBackpackCommand extends GeneratorCommand
3839
protected $type = 'Column';
3940

4041
/**
41-
* Get the stub file for the generator.
42+
* View Namespace.
4243
*
43-
* @return string
44+
* @var string
4445
*/
45-
protected function getStub()
46-
{
47-
return __DIR__.'/../stubs/column.stub';
48-
}
46+
protected $viewNamespace = 'columns';
4947

5048
/**
51-
* Alias for the fire method.
49+
* Get the stub file for the generator.
5250
*
53-
* In Laravel 5.5 the fire() method has been renamed to handle().
54-
* This alias provides support for both Laravel 5.4 and 5.5.
51+
* @return string
5552
*/
56-
public function handle()
53+
protected function getStub()
5754
{
58-
$this->fire();
55+
return __DIR__.'/../stubs/column.stub';
5956
}
6057

6158
/**
6259
* Execute the console command.
6360
*
6461
* @return bool|null
6562
*/
66-
public function fire()
63+
public function handle()
6764
{
6865
$name = Str::of($this->getNameInput());
69-
$path = $this->getPath($name);
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}</>");
7071

71-
if ($this->alreadyExists($this->getNameInput())) {
72-
$this->error("Error : $this->type $name already existed!");
72+
if ($this->alreadyExists($name)) {
73+
$this->closeProgressBlock('Already existed', 'yellow');
7374

7475
return false;
7576
}
7677

77-
$src = null;
78+
$source = null;
7879
if ($this->option('from')) {
79-
$column = Str::of($this->option('from'));
80-
$arr = ViewNamespaces::getFor('columns');
81-
foreach ($arr as $key => $value) {
82-
$viewPath = $value.'.'.$column;
80+
$from = $this->option('from');
81+
$namespaces = ViewNamespaces::getFor($this->viewNamespace);
82+
foreach ($namespaces as $namespace) {
83+
$viewPath = "$namespace.$from";
8384
if (view()->exists($viewPath)) {
84-
$src = view($viewPath)->getPath();
85+
$source = view($viewPath)->getPath();
8586
break;
8687
}
8788
}
88-
if ($src == null) {
89-
$this->error("Error : $this->type $column does not exist!");
89+
90+
// full file path may be provided
91+
if (file_exists($from)) {
92+
$source = $from;
93+
}
94+
95+
if (! $source) {
96+
$this->errorProgressBlock();
97+
$this->note("$this->type '$from' does not exist!", 'red');
98+
$this->newLine();
9099

91100
return false;
92101
}
93102
}
94103

95-
$this->infoBlock("Creating {$name->replace('_', ' ')->title()} {$this->type}");
96-
$this->progressBlock("Creating view <fg=blue>resources/views/vendor/backpack/crud/columns/{$name->snake('_')}.blade.php</>");
97-
98104
$this->makeDirectory($path);
99-
if ($src != null) {
100-
$this->files->copy($src, $path);
105+
106+
if ($source) {
107+
$this->files->copy($source, $path);
101108
} else {
102109
$this->files->put($path, $this->buildClass($name));
103110
}
104111

105112
$this->closeProgressBlock();
106-
$this->newLine();
107-
$this->info($this->type.' created successfully.');
108113
}
109114

110115
/**
@@ -145,12 +150,15 @@ protected function buildClass($name)
145150
}
146151

147152
/**
148-
* Get the console command options.
153+
* Get the desired class name from the input.
149154
*
150-
* @return array
155+
* @return string
151156
*/
152-
protected function getOptions()
157+
protected function getNameInput()
153158
{
154-
return [];
159+
return Str::of($this->argument('name'))
160+
->trim()
161+
->snake('_')
162+
->value;
155163
}
156164
}

0 commit comments

Comments
 (0)