1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webfactor\Laravel\Generators\Services; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
6
|
|
|
use Webfactor\Laravel\Generators\Contracts\ServiceAbstract; |
7
|
|
|
use Webfactor\Laravel\Generators\Contracts\ServiceInterface; |
8
|
|
|
use Webfactor\Laravel\Generators\Traits\CanGenerateFile; |
9
|
|
|
|
10
|
|
|
class MigrationService extends ServiceAbstract implements ServiceInterface |
11
|
|
|
{ |
12
|
|
|
use CanGenerateFile; |
13
|
|
|
|
14
|
|
|
protected $key = 'migration'; |
15
|
|
|
|
16
|
|
|
public function call() |
17
|
|
|
{ |
18
|
|
|
$this->generateFile(); |
19
|
|
|
$this->addGeneratedFileToIdeStack(); |
20
|
|
|
|
21
|
|
|
if ($this->command->option('migrate')) { |
22
|
|
|
$this->command->call('migrate'); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function buildFileContent() |
27
|
|
|
{ |
28
|
|
|
$this->replaceClassName(); |
29
|
|
|
$this->replaceTableName(); |
30
|
|
|
$this->replaceMigrationFields(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Replace migration fields in stub file. |
35
|
|
|
* |
36
|
|
|
* @return string |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
protected function replaceMigrationFields(): void |
39
|
|
|
{ |
40
|
|
|
$this->fileContent = str_replace('__migration_fields__', $this->generateMigrationFields(), $this->fileContent); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Generates the migration fields from schema |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
protected function generateMigrationFields(): string |
49
|
|
|
{ |
50
|
|
|
$migrationFields = ''; |
51
|
|
|
|
52
|
|
|
foreach ($this->command->schema->getMigrationFields() as $migrationField) { |
53
|
|
|
$migrationFields .= '$table->' . $migrationField['type'] . '(\'' . $migrationField['name'] . '\')'; |
54
|
|
|
unset($migrationField['type'], $migrationField['name']); |
55
|
|
|
|
56
|
|
|
if (!empty($migrationField)) { |
57
|
|
|
foreach ($migrationField as $key => $item) { |
58
|
|
|
$migrationFields .= '->' . $key . '(' . ((!is_bool($item)) ? '\'' . $item . '\'' : '') . ')'; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$migrationFields .= ";" . PHP_EOL; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $migrationFields; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.