| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Mtolhuys\LaravelSchematics\Actions\Migration\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Facades\Artisan; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Facades\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | trait CreatesMigrations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         $autoMigrate, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         $filename, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $this->autoMigrate = config('schematics.auto-migrate'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->path = database_path('migrations'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function __destruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         if ($this->autoMigrate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 Artisan::call('migrate'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             } catch (\Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 Cache::put('schematics-exception', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                     'title' => get_class($e), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                     'message' => $e->getMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 ], 1440); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected function getLocalKey($request): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return empty($request['method']['localKey']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ? 'id' : $request['method']['localKey']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     protected function getForeignKey($request): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return empty($request['method']['foreignKey']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ? strtolower( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 Str::snake( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     substr(strrchr($request['target'], "\\"), 1) . '_id' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             : $request['method']['foreignKey']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected function getFields($fields): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return array_map(static function ($field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $field['name'] => empty($field['type']) ? 'string|max:255' : $field['type'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         }, $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     protected function setUpChange($field) | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         if (isset($field['from'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             $field['type'] = "renameColumn|from:{$field['from']}|required"; | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             $field['type'] .= '|change'; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         return $field; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     protected function setDownChange($field) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if (isset($field['from'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $field['type'] = "renameColumn|from:{$field['to']}|required"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $field['name'] = $field['from']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             if (isset($field['to'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $field['name'] = $field['to']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $field['type'] = $this->parseColumnType($field['columnType']) . '|change'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return $field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * Parsing column type to migration rule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param $columnType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     protected function parseColumnType($columnType): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $max = (int)preg_replace('/\D/', '', $columnType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $type = str_replace(' ', '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             preg_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 '/[^a-zA-Z]+/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 explode(' ', $columnType, 2)[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $type = key(array_filter([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             'date' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 'date' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             'dateTime' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 'timestamp' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             'string' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 'varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 'text' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             'integer' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 'int', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 'bigint', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 'tinyint' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         ], static function($values) use($type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             return in_array(strtolower($type), $values, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         }, ARRAY_FILTER_USE_BOTH)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         if ($max > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             $type .= "|max:{$max}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         return $type ?? 'string'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 154 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |  |