Issues (157)

src/Blocks/Migrations.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace SoliDry\Blocks;
4
5
use Illuminate\Database\Migrations\Migration;
6
use Illuminate\Database\Schema\Blueprint;
7
use Illuminate\Support\Facades\Schema;
8
use SoliDry\Controllers\BaseCommand;
9
use SoliDry\Extension\BaseFormRequest;
10
use SoliDry\Helpers\Classes;
11
use SoliDry\Helpers\Console;
12
use SoliDry\Helpers\MethodOptions;
13
use SoliDry\Helpers\MigrationsHelper;
14
use SoliDry\Types\CustomsInterface;
15
use SoliDry\Types\ModelsInterface;
16
use SoliDry\Types\PhpInterface;
17
18
class Migrations extends MigrationsAbstract
19
{
20
    use EntitiesTrait;
0 ignored issues
show
The trait SoliDry\Blocks\EntitiesTrait requires the property $types which is not provided by SoliDry\Blocks\Migrations.
Loading history...
21
22
    /** @var BaseCommand $generator */
23
    protected $generator;
24
    protected $sourceCode = '';
25
26
    private $className = '';
27
    private $tableName = '';
28
29
    public function __construct($generator)
30
    {
31
        $this->generator = $generator;
32
        $this->className = Classes::getClassName($this->generator->objectName);
33
        $this->tableName = MigrationsHelper::getTableName($this->generator->objectName);
34
    }
35
36
    public function setCodeState($generator)
37
    {
38
        $this->generator = $generator;
39
    }
40
41
    /**
42
     * @uses \SoliDry\Blocks\EntitiesTrait::reset
43
     */
44
    public function create()
45
    {
46
        $migrationName = ModelsInterface::MIGRATION_CREATE . PhpInterface::UNDERSCORE .
47
                         $this->tableName .
48
                         PhpInterface::UNDERSCORE . ModelsInterface::MIGRATION_TABLE;
49
        $attrKey = $this->generator->objectName . CustomsInterface::CUSTOM_TYPES_ATTRIBUTES;
50
51
        $isFileNonExistent = FileManager::migrationNotExists($this->generator, $migrationName);
52
        $isAdding = ($this->generator->isMerge === true && empty($this->generator->diffTypes[$attrKey]) === false);
53
54
        if($isFileNonExistent === true)
55
        {
56
            $this->setContent();
57
        } else if ($isAdding === true) {
58
            $migrationName = str_replace(ModelsInterface::MIGRATION_TABLE_PTTRN, $this->tableName,
59
                ModelsInterface::MIGRATION_ADD_COLUMN);
60
            // file exists and it is merge op - add columns/indices for this table
61
            $columnName = key($this->generator->diffTypes[$attrKey]);
62
            $migrationName = str_replace(ModelsInterface::MIGRATION_COLUMN_PTTRN, $columnName, $migrationName);
63
            $this->resetContent($this->generator->diffTypes[$attrKey], $columnName);
64
        }
65
66
        if ($isFileNonExistent === true || $isAdding === true) {
67
            $this->createMigrationFile($migrationName);
68
        }
69
    }
70
71
    /**
72
     *  Creates pivot table for ManyToMany relations if needed
73
     */
74
    public function createPivot()
75
    {
76
        $formRequestEntity = $this->getFormRequestEntity($this->generator->version, $this->className);
77
        /** @var BaseFormRequest $formRequest **/
78
        $formRequest       = new $formRequestEntity();
79
        if(method_exists($formRequest, ModelsInterface::MODEL_METHOD_RELATIONS))
80
        {
81
            $relations = $formRequest->relations();
82
            foreach($relations as $relationEntity)
83
            {
84
                $entityFile = $this->generator->formatEntitiesPath()
85
                              . PhpInterface::SLASH .
86
                              $this->generator->objectName .
87
                              ucfirst($relationEntity) .
88
                              PhpInterface::PHP_EXT;
89
                if(file_exists($entityFile))
90
                {
91
                    $this->setPivotContent($relationEntity);
92
93
                    $migrationMask = date(self::PATTERN_TIME) . (self::$counter + 1);
94
                    $migrationName = ModelsInterface::MIGRATION_CREATE . PhpInterface::UNDERSCORE
95
                                     . $this->tableName
96
                                     . PhpInterface::UNDERSCORE .
97
                                     MigrationsHelper::getTableName($relationEntity) .
98
                                     PhpInterface::UNDERSCORE . ModelsInterface::MIGRATION_TABLE;
99
100
                    if(FileManager::migrationNotExists($this->generator, $migrationName))
101
                    {
102
                        $file = $this->generator->formatMigrationsPath() . $migrationMask
103
                                . PhpInterface::UNDERSCORE . $migrationName . PhpInterface::PHP_EXT;
104
                        // if migration file with the same name ocasionally exists we do not override it
105
                        $isCreated = FileManager::createFile($file, $this->sourceCode);
106
                        if($isCreated)
107
                        {
108
                            Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN);
109
                        }
110
                    }
111
                }
112
            }
113
        }
114
    }
115
116
    /**
117
     *  Sets the content of migration
118
     */
119
    private function setContent()
120
    {
121
        $this->setTag();
122
        $this->setUse(Schema::class);
123
        $this->setUse(Blueprint::class);
124
        $migrationClass = Migration::class;
125
        $this->setUse($migrationClass, false, true);
126
        // migrate up
127
        $this->startClass(
128
            ucfirst(ModelsInterface::MIGRATION_CREATE) . $this->className
129
            . ucfirst(ModelsInterface::MIGRATION_TABLE), Classes::getName($migrationClass)
130
        );
131
        $methodOptions = new MethodOptions();
132
        $methodOptions->setName(ModelsInterface::MIGRATION_METHOD_UP);
133
        $this->startMethod($methodOptions);
134
        $this->openSchema($this->tableName);
135
        $this->setRows();
136
        $this->closeSchema();
137
        $this->endMethod();
138
        // migrate down
139
        $methodOptions->setName(ModelsInterface::MIGRATION_METHOD_DOWN);
140
        $this->startMethod($methodOptions);
141
        $this->createSchema(ModelsInterface::MIGRATION_METHOD_DROP, $this->tableName);
142
        $this->endMethod();
143
        $this->endClass();
144
    }
145
146
    /**
147
     * @param array  $attrs         columns
148
     * @param string $columnName    1st column
149
     */
150
    private function resetContent(array $attrs, string $columnName)
151
    {
152
        $this->setTag();
153
        $this->setUse(Schema::class);
154
        $this->setUse(Blueprint::class);
155
        $migrationClass = Migration::class;
156
        $this->setUse($migrationClass, false, true);
157
        $className = str_replace(ModelsInterface::MIGRATION_COLUMN_PTTRN, Classes::getClassName($columnName),
158
            ModelsInterface::MIGRATION_ADD_COLUMN_CLASS);
159
        $className = str_replace(ModelsInterface::MIGRATION_TABLE_PTTRN, $this->className, $className);
160
        $this->startClass($className, Classes::getName($migrationClass));
161
        // migrate up
162
        $methodOptions = new MethodOptions();
163
        $methodOptions->setName(ModelsInterface::MIGRATION_METHOD_UP);
164
        $this->startMethod($methodOptions);
165
        $this->openSchema($this->tableName, ModelsInterface::MIGRATION_TABLE);
166
        $this->setAddRows($attrs);
167
        $this->closeSchema();
168
        $this->endMethod();
169
        // migrate down
170
        $methodOptions = new MethodOptions();
171
        $methodOptions->setName(ModelsInterface::MIGRATION_METHOD_DOWN);
172
        $this->startMethod($methodOptions);
173
        $this->openSchema($this->tableName, ModelsInterface::MIGRATION_TABLE);
174
        $this->dropRows($attrs);
175
        $this->closeSchema();
176
        $this->endMethod();
177
        $this->endClass();
178
    }
179
180
    /**
181
     * Sets the content of pivot ManyToMany migration
182
     * @param string $relationEntity
183
     */
184
    private function setPivotContent(string $relationEntity)
185
    {
186
        $this->setTag();
187
188
        $this->setUse(Schema::class);
189
        $this->setUse(Blueprint::class);
190
        $migrationClass = Migration::class;
191
        $this->setUse($migrationClass, false, true);
192
        // migrate up
193
        $this->startClass(
194
            ucfirst(ModelsInterface::MIGRATION_CREATE) .
195
            Classes::getClassName($this->generator->objectName) .
196
            Classes::getClassName($relationEntity) .
197
            ucfirst(ModelsInterface::MIGRATION_TABLE), Classes::getName($migrationClass)
198
        );
199
        $methodOptions = new MethodOptions();
200
        $methodOptions->setName(ModelsInterface::MIGRATION_METHOD_UP);
201
        $this->startMethod($methodOptions);
202
        // make first entity lc + underscore
203
        $table = MigrationsHelper::getTableName($this->generator->objectName);
204
        // make 2nd entity lc + underscore
205
        $relatedTable   = MigrationsHelper::getTableName($relationEntity);
206
        $combinedTables = $table . PhpInterface::UNDERSCORE . $relatedTable;
207
        // migrate up
208
        $this->openSchema($combinedTables);
209
        $this->setPivotRows($relationEntity);
210
        $this->closeSchema();
211
        $this->endMethod();
212
        // migrate down
213
        $methodOptions->setName(ModelsInterface::MIGRATION_METHOD_DOWN);
214
        $this->startMethod($methodOptions);
215
        $this->createSchema(ModelsInterface::MIGRATION_METHOD_DROP, $combinedTables);
216
        $this->endMethod();
217
        $this->endClass();
218
    }
219
}