1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
|
7
|
|
|
class CrudOverwrite extends Command |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The console command name. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $name = 'backpack:overwrite'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The name and signature of the console command. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $signature = 'backpack:overwrite |
22
|
|
|
{type : what are you overwriting? field/column/button} |
23
|
|
|
{name : name of the field}'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The console command description. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = 'Publishes a built in field/column/button for modification'; |
31
|
|
|
|
32
|
|
|
public $packageDir = 'vendor/backpack/crud/src/resources/views/'; |
33
|
|
|
public $appDir = 'resources/views/vendor/backpack/crud/'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a new command instance. |
37
|
|
|
* |
38
|
|
|
* @return void |
|
|
|
|
39
|
|
|
*/ |
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
parent::__construct(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Execute the console command. |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public function handle() |
51
|
|
|
{ |
52
|
|
|
$this->type = strtolower($this->argument('type')); |
|
|
|
|
53
|
|
|
$this->name = strtolower($this->argument('name')); |
54
|
|
|
|
55
|
|
|
switch ($this->type) { |
56
|
|
|
case 'field': |
57
|
|
|
case 'fields': |
58
|
|
|
return $this->publishField(); |
59
|
|
|
break; |
|
|
|
|
60
|
|
|
case 'button': |
61
|
|
|
case 'buttons': |
62
|
|
|
return $this->publishButton(); |
63
|
|
|
break; |
|
|
|
|
64
|
|
|
case 'column': |
65
|
|
|
case 'columns': |
66
|
|
|
return $this->publishColumn(); |
67
|
|
|
break; |
|
|
|
|
68
|
|
|
default: |
69
|
|
|
return $this->error('Please pick from field, column or button'); |
70
|
|
|
break; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function processPublish($file, $label) |
75
|
|
|
{ |
76
|
|
|
$sourceFile = $this->packageDir.$file.'.blade.php'; |
77
|
|
|
$copiedFile = $this->appDir.$file.'.blade.php'; |
78
|
|
|
|
79
|
|
|
if (! file_exists($sourceFile)) { |
80
|
|
|
return $this->error( |
81
|
|
|
'Cannot find source '.$label.' at ' |
82
|
|
|
.$sourceFile. |
83
|
|
|
' - make sure you\'ve picked a real '.$label.' type' |
84
|
|
|
); |
85
|
|
|
} else { |
86
|
|
|
$canCopy = true; |
87
|
|
|
|
88
|
|
|
if (file_exists($copiedFile)) { |
89
|
|
|
$canCopy = $this->confirm( |
90
|
|
|
'File already exists at ' |
91
|
|
|
.$copiedFile. |
92
|
|
|
' - do you want to overwrite it?' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($canCopy) { |
97
|
|
|
$path = pathinfo($copiedFile); |
98
|
|
|
|
99
|
|
|
if (! file_exists($path['dirname'])) { |
100
|
|
|
mkdir($path['dirname'], 0755, true); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (copy($sourceFile, $copiedFile)) { |
104
|
|
|
$this->info('Copied to '.$copiedFile); |
105
|
|
|
} else { |
106
|
|
|
return $this->error( |
107
|
|
|
'Failed to copy ' |
108
|
|
|
.$sourceFile. |
109
|
|
|
' to ' |
110
|
|
|
.$copiedFile. |
111
|
|
|
' for unknown reason' |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function publishField() |
119
|
|
|
{ |
120
|
|
|
return $this->processPublish('fields/'.$this->name, 'field'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function publishColumn() |
124
|
|
|
{ |
125
|
|
|
return $this->processPublish('columns/'.$this->name, 'column'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function publishButton() |
129
|
|
|
{ |
130
|
|
|
return $this->processPublish('buttons/'.$this->name, 'button'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.