1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dimsav\Translatable\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Support\Composer; |
7
|
|
|
use Illuminate\Support\Facades\App; |
8
|
|
|
use Illuminate\Database\Console\Migrations\BaseCommand; |
9
|
|
|
|
10
|
|
|
class TranslateTableCommand extends BaseCommand |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The console command signature. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'make:translate {name : The name of the translate table.} |
18
|
|
|
{--path= : The location where the migration file should be created.}'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The console command description. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $description = 'Create a new translate migration file'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The migration creator instance. |
29
|
|
|
* |
30
|
|
|
* @var \Illuminate\Database\Migrations\MigrationCreator |
31
|
|
|
*/ |
32
|
|
|
protected $creator; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The Composer instance. |
36
|
|
|
* |
37
|
|
|
* @var \Illuminate\Support\Composer |
38
|
|
|
*/ |
39
|
|
|
protected $composer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a new migration install command instance. |
43
|
|
|
* |
44
|
|
|
* @param \Dimsav\Translatable\Console\MigrationCreator $creator |
45
|
|
|
* @param \Illuminate\Support\Composer $composer |
46
|
|
|
* @return void |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function __construct(MigrationCreator $creator, Composer $composer) |
49
|
|
|
{ |
50
|
|
|
parent::__construct(); |
51
|
|
|
|
52
|
|
|
$this->creator = $creator; |
53
|
|
|
$this->composer = $composer; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Execute the console command. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function fire() |
62
|
|
|
{ |
63
|
|
|
// create the translate model |
64
|
|
|
$this->call('make:model', [ |
65
|
|
|
'name' => $this->getModelName(trim($this->input->getArgument('name'))), |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
// It's possible for the developer to specify the tables to modify in this |
69
|
|
|
// schema operation. The developer may also specify if this table needs |
70
|
|
|
// to be freshly created so we can create the appropriate migrations. |
71
|
|
|
$name = $this->getMigrationName(trim($this->input->getArgument('name'))); |
72
|
|
|
|
73
|
|
|
$table = $this->getTableName(trim($this->input->getArgument('name'))); |
74
|
|
|
$create = true; |
75
|
|
|
|
76
|
|
|
// Now we are ready to write the migration out to disk. Once we've written |
77
|
|
|
// the migration out, we will dump-autoload for the entire framework to |
78
|
|
|
// make sure that the migrations are registered by the class loaders. |
79
|
|
|
$this->writeMigration($name, $table, $create); |
80
|
|
|
|
81
|
|
|
$this->composer->dumpAutoloads(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the translate table name. |
86
|
|
|
* |
87
|
|
|
* @param string $table |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
protected function getTableName($table) |
91
|
|
|
{ |
92
|
|
|
$translation_suffix = Str::snake(Str::plural(App::make('config')->get('translatable.translation_suffix', 'Translation'))); |
93
|
|
|
|
94
|
|
|
return Str::snake(Str::singular(class_basename($table)).'_'.$translation_suffix); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the translate model class name. |
99
|
|
|
* |
100
|
|
|
* @param $table |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
protected function getModelName($table) |
104
|
|
|
{ |
105
|
|
|
$config = App::make('config'); |
106
|
|
|
|
107
|
|
|
$suffix = $config->get('translatable.translation_suffix', 'Translation'); |
108
|
|
|
|
109
|
|
|
if ($config->get('translatable.translation_models_namespace')) { |
110
|
|
|
$namespace = $config->get('translatable.translation_models_namespace').'\\'; |
111
|
|
|
} else { |
112
|
|
|
$namespace = ''; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $namespace.Str::studly(Str::singular(class_basename($table))).$suffix; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the translate migration file name. |
120
|
|
|
* |
121
|
|
|
* @param string $name |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
protected function getMigrationName($name) |
125
|
|
|
{ |
126
|
|
|
return 'create_'.$this->getTableName($name).'_table'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Write the migration file to disk. |
131
|
|
|
* |
132
|
|
|
* @param string $name |
133
|
|
|
* @param string $table |
134
|
|
|
* @param bool $create |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
protected function writeMigration($name, $table, $create) |
138
|
|
|
{ |
139
|
|
|
$file = pathinfo($this->creator->create($name, $this->getMigrationPath(), $table, $create), PATHINFO_FILENAME); |
140
|
|
|
|
141
|
|
|
$this->line("<info>Created Migration:</info> {$file}"); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get migration path (either specified by '--path' option or default location). |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
protected function getMigrationPath() |
150
|
|
|
{ |
151
|
|
|
if (! is_null($targetPath = $this->input->getOption('path'))) { |
152
|
|
|
return $this->laravel->basePath().'/'.$targetPath; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return parent::getMigrationPath(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
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.