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