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