Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
2.33% |
1 / 43 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
GrowTreeCommand | |
2.33% |
1 / 43 |
|
25.00% |
1 / 4 |
103.18 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
handle | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
writeClosureMigration | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
getMigrationContents | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Baril\Bonsai\Console; |
4 | |
5 | use Baril\Bonsai\Migrations\MigrationCreator; |
6 | use Illuminate\Database\Console\Migrations\MigrateMakeCommand; |
7 | use Illuminate\Database\Eloquent\Model; |
8 | use Illuminate\Support\Composer; |
9 | use Illuminate\Support\Str; |
10 | |
11 | class GrowTreeCommand extends MigrateMakeCommand |
12 | { |
13 | protected $signature = 'bonsai:grow {model : The model class.} |
14 | {--name= : The name of the migration.} |
15 | {--path= : The location where the migration file should be created.} |
16 | {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths.} |
17 | {--migrate : Migrate the database and fill the table after the migration file has been created.}'; |
18 | protected $description = 'Create the migration file for a closure table, and optionally run the migration'; |
19 | |
20 | public function __construct(MigrationCreator $creator, Composer $composer) |
21 | { |
22 | parent::__construct($creator, $composer); |
23 | } |
24 | |
25 | public function handle() |
26 | { |
27 | $model = $this->input->getArgument('model'); |
28 | if ( |
29 | !class_exists($model) |
30 | || !is_subclass_of($model, Model::class) |
31 | || !method_exists($model, 'getClosureTable') |
32 | ) { |
33 | $this->error('{model} must be a valid model class and use the BelongsToTree trait!'); |
34 | return; |
35 | } |
36 | |
37 | $this->writeClosureMigration($model); |
38 | $this->composer->dumpAutoloads(); |
39 | |
40 | if ($this->input->hasOption('migrate') && $this->option('migrate')) { |
41 | $this->call('migrate'); |
42 | $this->call('bonsai:fix', ['model' => $model]); |
43 | } |
44 | } |
45 | |
46 | protected function writeClosureMigration($model) |
47 | { |
48 | // Retrieve all informations about the tree: |
49 | $instance = new $model(); |
50 | $table = $instance->getTable(); |
51 | $closureTable = $instance->getClosureTable(); |
52 | $keyName = $instance->getKeyName(); |
53 | |
54 | // Get the name for the migration file: |
55 | $name = $this->input->getOption('name') ?: 'create_' . $closureTable . '_table'; |
56 | $name = Str::snake(trim($name)); |
57 | $className = Str::studly($name); |
58 | |
59 | // Generate the content of the migration file: |
60 | $contents = $this->getMigrationContents($className, $table, $closureTable, $keyName); |
61 | |
62 | // Generate the file: |
63 | $file = $this->creator->create( |
64 | $name, |
65 | $this->getMigrationPath(), |
66 | $closureTable, |
67 | true |
68 | ); |
69 | file_put_contents($file, $contents); |
70 | |
71 | // Output information: |
72 | $file = pathinfo($file, PATHINFO_FILENAME); |
73 | $this->line("<info>Created Migration:</info> {$file}"); |
74 | } |
75 | |
76 | protected function getMigrationContents($className, $table, $closureTable, $keyName) |
77 | { |
78 | $contents = file_get_contents(__DIR__ . '/../Migrations/stubs/grow_tree.stub'); |
79 | $contents = str_replace([ |
80 | 'class CreateExampleTreeTable', |
81 | '$mainTableName = "example"', |
82 | '$closureTableName = "example_tree"', |
83 | '$mainTableKey = "id"', |
84 | ], [ |
85 | 'class ' . $className, |
86 | '$mainTableName = "' . $table . '"', |
87 | '$closureTableName = "' . $closureTable . '"', |
88 | '$mainTableKey = "' . $keyName . '"', |
89 | ], $contents); |
90 | $contents = preg_replace('/\;[\s]*\/\/.*\n/U', ";\n", $contents); |
91 | return $contents; |
92 | } |
93 | } |