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 getConsoleOutput() |
17
|
|
|
{ |
18
|
|
|
return 'Generated migration file: '.$this->command->naming[$this->key]->getRelativeFilePath(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function call() |
22
|
|
|
{ |
23
|
|
|
$this->generateFile(); |
24
|
|
|
$this->addGeneratedFileToIdeStack(); |
25
|
|
|
|
26
|
|
|
if ($this->command->option('migrate')) { |
27
|
|
|
$this->command->call('migrate'); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function buildFileContent() |
32
|
|
|
{ |
33
|
|
|
$this->replaceClassName(); |
34
|
|
|
$this->replaceTableName(); |
35
|
|
|
$this->replaceMigrationFields(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Replace migration fields in stub file. |
40
|
|
|
* |
41
|
|
|
* @return string |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
protected function replaceMigrationFields(): void |
44
|
|
|
{ |
45
|
|
|
$this->fileContent = str_replace('__migration_fields__', $this->generateMigrationFields(), $this->fileContent); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Generates the migration fields from schema |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
protected function generateMigrationFields(): string |
54
|
|
|
{ |
55
|
|
|
$migrationFields = ''; |
56
|
|
|
|
57
|
|
|
foreach ($this->command->schema->getMigrationFields() as $migrationField) { |
58
|
|
|
$migrationFields .= ' $table->' . $migrationField['type'] . '(\'' . $migrationField['name'] . '\')'; |
|
|
|
|
59
|
|
|
unset($migrationField['type'], $migrationField['name']); |
60
|
|
|
|
61
|
|
|
if (!empty($migrationField)) { |
62
|
|
|
foreach ($migrationField as $key => $item) { |
63
|
|
|
$migrationFields .= '->' . $key . '(' . ((!is_bool($item)) ? '\'' . $item . '\'' : '') . ')'; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$migrationFields .= ";" . PHP_EOL; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $migrationFields; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
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.