1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dimsav\Translatable\Console; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Illuminate\Support\Facades\App; |
8
|
|
|
|
9
|
|
|
class MigrationCreator extends \Illuminate\Database\Migrations\MigrationCreator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Get the migration stub file. |
13
|
|
|
* |
14
|
|
|
* @param string $table |
15
|
|
|
* @param bool $create |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
protected function getStub($table, $create) |
19
|
|
|
{ |
20
|
|
|
return $this->files->get($this->stubPath().'/translate.stub'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Populate the place-holders in the migration stub. |
25
|
|
|
* |
26
|
|
|
* @param string $name |
27
|
|
|
* @param string $stub |
28
|
|
|
* @param string $table |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
protected function populateStub($name, $stub, $table) |
32
|
|
|
{ |
33
|
|
|
$stub = str_replace('DummyClass', $this->getClassName($name), $stub); |
34
|
|
|
|
35
|
|
|
// Here we will replace the table place-holders with the table specified by |
36
|
|
|
// the developer, which is useful for quickly creating a tables creation |
37
|
|
|
// or update migration from the console instead of typing it manually. |
38
|
|
|
if (! is_null($table)) { |
39
|
|
|
$stub = str_replace('DummyTable', $table, $stub); |
40
|
|
|
|
41
|
|
|
$referencesTable = Str::plural(str_replace('_'.$this->getTranslationSuffix(), '', $table)); |
42
|
|
|
|
43
|
|
|
$stub = str_replace('DummyReferencesTable', $referencesTable, $stub); |
44
|
|
|
|
45
|
|
|
$forignKey = Str::singular(str_replace('_'.$this->getTranslationSuffix(), '', $table)).'_id'; |
46
|
|
|
|
47
|
|
|
$stub = str_replace('DummyForeign', $forignKey, $stub); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $stub; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Defines the default 'Translation' class suffix. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
protected function getTranslationSuffix() |
59
|
|
|
{ |
60
|
|
|
return Str::snake(Str::plural(App::make('config')->get('translatable.translation_suffix', 'translation'))); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the class name of a migration name. |
65
|
|
|
* |
66
|
|
|
* @param string $name |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
protected function getClassName($name) |
70
|
|
|
{ |
71
|
|
|
return Str::studly($name); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the full path to the migration. |
76
|
|
|
* |
77
|
|
|
* @param string $name |
78
|
|
|
* @param string $path |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
protected function getPath($name, $path) |
82
|
|
|
{ |
83
|
|
|
return $path.'/'.$this->getDatePrefix().'_'.$name.'.php'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Fire the registered post create hooks. |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
protected function firePostCreateHooks() |
92
|
|
|
{ |
93
|
|
|
foreach ($this->postCreate as $callback) { |
94
|
|
|
call_user_func($callback); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Register a post migration create hook. |
100
|
|
|
* |
101
|
|
|
* @param \Closure $callback |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function afterCreate(Closure $callback) |
105
|
|
|
{ |
106
|
|
|
$this->postCreate[] = $callback; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the path to the stubs. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function stubPath() |
115
|
|
|
{ |
116
|
|
|
return __DIR__.'/stubs'; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|