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
|
|
|
$this->creator = $creator; |
52
|
|
|
$this->composer = $composer; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Execute the console command. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function fire() |
61
|
|
|
{ |
62
|
|
|
// create the translate model |
63
|
|
|
$this->call('make:model', [ |
64
|
|
|
'name' => $this->getModelName(trim($this->input->getArgument('name'))), |
65
|
|
|
]); |
66
|
|
|
// It's possible for the developer to specify the tables to modify in this |
67
|
|
|
// schema operation. The developer may also specify if this table needs |
68
|
|
|
// to be freshly created so we can create the appropriate migrations. |
69
|
|
|
$name = $this->getMigrationName(trim($this->input->getArgument('name'))); |
70
|
|
|
$table = $this->getTableName(trim($this->input->getArgument('name'))); |
71
|
|
|
$create = true; |
72
|
|
|
// Now we are ready to write the migration out to disk. Once we've written |
73
|
|
|
// the migration out, we will dump-autoload for the entire framework to |
74
|
|
|
// make sure that the migrations are registered by the class loaders. |
75
|
|
|
$this->writeMigration($name, $table, $create); |
76
|
|
|
$this->composer->dumpAutoloads(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the translate table name. |
81
|
|
|
* |
82
|
|
|
* @param string $table |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
protected function getTableName($table) |
86
|
|
|
{ |
87
|
|
|
$translation_suffix = Str::snake(Str::plural(App::make('config')->get('translatable.translation_suffix', 'Translation'))); |
88
|
|
|
|
89
|
|
|
return Str::snake(Str::singular(class_basename($table)).'_'.$translation_suffix); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the translate model class name. |
94
|
|
|
* |
95
|
|
|
* @param $table |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
protected function getModelName($table) |
99
|
|
|
{ |
100
|
|
|
$config = App::make('config'); |
101
|
|
|
$suffix = $config->get('translatable.translation_suffix', 'Translation'); |
102
|
|
|
if ($config->get('translatable.translation_models_namespace')) { |
103
|
|
|
$namespace = $config->get('translatable.translation_models_namespace').'\\'; |
104
|
|
|
} else { |
105
|
|
|
$namespace = ''; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $namespace.Str::studly(Str::singular(class_basename($table))).$suffix; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the translate migration file name. |
113
|
|
|
* |
114
|
|
|
* @param string $name |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
protected function getMigrationName($name) |
118
|
|
|
{ |
119
|
|
|
return 'create_'.$this->getTableName($name).'_table'; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Write the migration file to disk. |
124
|
|
|
* |
125
|
|
|
* @param string $name |
126
|
|
|
* @param string $table |
127
|
|
|
* @param bool $create |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
protected function writeMigration($name, $table, $create) |
131
|
|
|
{ |
132
|
|
|
$file = pathinfo($this->creator->create($name, $this->getMigrationPath(), $table, $create), PATHINFO_FILENAME); |
133
|
|
|
$this->line("<info>Created Migration:</info> {$file}"); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get migration path (either specified by '--path' option or default location). |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
protected function getMigrationPath() |
142
|
|
|
{ |
143
|
|
|
if (! is_null($targetPath = $this->input->getOption('path'))) { |
144
|
|
|
return $this->laravel->basePath().'/'.$targetPath; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return parent::getMigrationPath(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.