| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Elimuswift\DbExporter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class DbSeeding extends DbExporter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public $database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public $filename; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $seedingStub; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected $customDb = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Set the database name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param string $database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function __construct($database) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->database = $database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | //end __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * Write the seed file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function write() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         // Check if convert method was called before | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         // If not, call it on default DB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         if (!$this->customDb) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $this->convert(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         foreach ($this->seedingStub as $table => $value)  | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $seed = $this->compile($table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $absolutePath = Config::get('db-exporter.export_path.seeds'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $this->filename = ucfirst(Str::camel($table)) . 'DatabaseSeeder'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->makePath($absolutePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             file_put_contents($absolutePath . "/{$this->filename}.php", $seed); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | //end write() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Convert the database tables to something usefull. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param null $database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function convert($database = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (!is_null($database)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $this->database = $database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $this->customDb = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // Get the tables for the database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $tables = $this->getTables(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         // Get tables to ignore | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $config = config('db-exporter.seeds'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $ignore_tables = collect([]); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 88 |  | View Code Duplication |         if(!is_null($config) && isset($config['ignore_tables']) && !is_null($config['ignore_tables'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $ignore_tables = collect($config['ignore_tables']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $show_tables = collect([]); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 93 |  | View Code Duplication |         if(!is_null($config) && isset($config['use_tables']) && !is_null($config['use_tables'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $show_tables = collect($config['use_tables']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         // Loop over the tables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         foreach ($tables as $key => $value)  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             if($show_tables->count() > 0 && !$show_tables->contains($value['table_name'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             if($ignore_tables->contains($value['table_name'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             // Do not export the ignored tables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             if (in_array($value['table_name'], self::$ignore)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $tableName = $value['table_name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $tableData = $this->getTableData($value['table_name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $insertStub = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             foreach ($tableData as $obj) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 $insertStub .= " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             [\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 foreach ($obj as $prop => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     $insertStub .= $this->insertPropertyAndValue($prop, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 if (count($tableData) > 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                     $insertStub .= "            ],\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     $insertStub .= "            ]\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             $insertStub = " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         \$data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             {$insertStub} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         ];"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             if ($this->hasTableData($tableData)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 $stub = $insertStub.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         foreach($data as $item)  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             $this->saveData("'.$tableName.'", $item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         }';  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 $result[$tableName] = $stub;  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         }//end foreach | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $this->seedingStub = $result; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | //end convert() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * Compile the current seedingStub with the seed template. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     protected function compile($table) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         // Grab the template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $template = File::get(__DIR__ . '/stubs/seed.stub'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         // Replace the classname | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $template = str_replace('{{className}}', ucfirst(Str::camel($table)) . 'DatabaseSeeder', $template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         $template = str_replace('{{run}}', $this->seedingStub[$table], $template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         return $template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | //end compile() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |     private function insertPropertyAndValue($prop, $value) | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |         if(strlen($prop) > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |             $prop = "'{$prop}'"; | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |             $prop = 'null'; | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |         if(strlen($value) > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |             $value = str_replace("'", "\'", $value); | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |             $value = "'{$value}'"; | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |             $value = 'null'; | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |         return "                {$prop} => {$value},\n"; | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | //end insertPropertyAndValue() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * @param $tableData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     public function hasTableData($tableData) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         return count($tableData) >= 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | //end hasTableData() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 203 |  |  | }//end class | 
            
                                                        
            
                                    
            
            
                | 204 |  |  |  |