| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\Backup\Tasks\Backup; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Spatie\DbDumper\DbDumper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Spatie\DbDumper\Databases\MySql; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Spatie\DbDumper\Databases\Sqlite; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Spatie\DbDumper\Databases\MongoDb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Spatie\DbDumper\Databases\PostgreSql; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Spatie\Backup\Exceptions\CannotCreateDbDumper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class DbDumperFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected static $custom = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public static function createFromConnection(string $dbConnectionName): DbDumper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $dbConfig = config("database.connections.{$dbConnectionName}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         if (isset($dbConfig['read'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             $dbConfig = Arr::except( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |                 array_merge($dbConfig, $dbConfig['read']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                 ['read', 'write'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $dbDumper = static::forDriver($dbConfig['driver']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ->setHost(Arr::first(Arr::wrap($dbConfig['host'] ?? ''))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             ->setDbName($dbConfig['database']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             ->setUserName($dbConfig['username'] ?? '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             ->setPassword($dbConfig['password'] ?? ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if ($dbDumper instanceof MySql) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $dbDumper->setDefaultCharacterSet($dbConfig['charset'] ?? ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if ($dbDumper instanceof MongoDb) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             $dbDumper->setAuthenticationDatabase(config('database.connections.mongodb.dump.mongodb_user_auth') ?? ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         if (isset($dbConfig['port'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $dbDumper = $dbDumper->setPort($dbConfig['port']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (isset($dbConfig['dump'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $dbDumper = static::processExtraDumpParameters($dbConfig['dump'], $dbDumper); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return $dbDumper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     public static function extend(string $driver, callable $callback) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         static::$custom[$driver] = $callback; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     protected static function forDriver($dbDriver): DbDumper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $driver = strtolower($dbDriver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if (isset(static::$custom[$driver])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             return (static::$custom[$driver])(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if ($driver === 'mysql' || $driver === 'mariadb') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return new MySql(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if ($driver === 'pgsql') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             return new PostgreSql(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if ($driver === 'sqlite') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             return new Sqlite(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         if ($driver === 'mongodb') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             return new MongoDb(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         throw CannotCreateDbDumper::unsupportedDriver($driver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     protected static function processExtraDumpParameters(array $dumpConfiguration, DbDumper $dbDumper): DbDumper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         collect($dumpConfiguration)->each(function ($configValue, $configName) use ($dbDumper) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $methodName = lcfirst(Str::studly(is_numeric($configName) ? $configValue : $configName)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $methodValue = is_numeric($configName) ? null : $configValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $methodName = static::determineValidMethodName($dbDumper, $methodName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             if (method_exists($dbDumper, $methodName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 static::callMethodOnDumper($dbDumper, $methodName, $methodValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return $dbDumper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     protected static function callMethodOnDumper(DbDumper $dbDumper, string $methodName, $methodValue): DbDumper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         if (! $methodValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $dbDumper->$methodName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             return $dbDumper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $dbDumper->$methodName($methodValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return $dbDumper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     protected static function determineValidMethodName(DbDumper $dbDumper, string $methodName): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return collect([$methodName, 'set'.ucfirst($methodName)]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             ->first(function (string $methodName) use ($dbDumper) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 return method_exists($dbDumper, $methodName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             }, ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  |