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