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