-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathModelBackpackCommand.php
More file actions
111 lines (93 loc) · 2.5 KB
/
ModelBackpackCommand.php
File metadata and controls
111 lines (93 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace Backpack\Generators\Console\Commands;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
class ModelBackpackCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'backpack:model';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'backpack:model {name} {--softdelete}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a backpack templated model';
/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Model';
/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
if ($this->option('softdelete')) {
return __DIR__.'/../stubs/model-softdelete.stub';
}
return __DIR__.'/../stubs/model.stub';
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace;
}
/**
* Replace the table name for the given stub.
*
* @param string $stub
* @param string $name
* @return string
*/
protected function replaceTable(&$stub, $name)
{
$name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_');
$table = Str::snake(Str::plural($name));
$stub = str_replace('DummyTable', $table, $stub);
return $this;
}
/**
* Replace Model on MongoDB.
*
* @param string $stub
* @param string $name
* @return string
*/
protected function replaceDriverDB(&$stub)
{
$driver = \Schema::getConnection()->getConfig('driver');
if ($driver === 'mongodb') {
$stub = str_replace(\Illuminate\Database\Eloquent\Model::class, \Jenssegers\Mongodb\Eloquent\Model::class, $stub);
}
return $this;
}
/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$stub = $this->files->get($this->getStub());
return $this->replaceNamespace($stub, $name)->replaceTable($stub, $name)->replaceDriverDB($stub)->replaceClass($stub, $name);
}
}