|  | 
|  | 1 | +<?php namespace BackupManager\Laravel; | 
|  | 2 | + | 
|  | 3 | +use BackupManager\Databases; | 
|  | 4 | +use BackupManager\Filesystems; | 
|  | 5 | +use BackupManager\Compressors; | 
|  | 6 | +use Symfony\Component\Process\Process; | 
|  | 7 | +use Illuminate\Support\ServiceProvider; | 
|  | 8 | +use BackupManager\Config\Config; | 
|  | 9 | +use BackupManager\ShellProcessing\ShellProcessor; | 
|  | 10 | + | 
|  | 11 | +/** | 
|  | 12 | + * Class BackupManagerServiceProvider | 
|  | 13 | + * @package BackupManager\Laravel | 
|  | 14 | + */ | 
|  | 15 | +class Laravel4BackupManagerServiceProvider extends ServiceProvider { | 
|  | 16 | + | 
|  | 17 | +    protected $defer = true; | 
|  | 18 | + | 
|  | 19 | +    /** | 
|  | 20 | +     * Bootstrap the application events. | 
|  | 21 | +     * | 
|  | 22 | +     * @return void | 
|  | 23 | +     */ | 
|  | 24 | +    public function boot() { | 
|  | 25 | +        $this->package('backup-manager/laravel-4', 'backup-manager', realpath(app_path("config/packages/backup-manager/laravel-4"))); | 
|  | 26 | +    } | 
|  | 27 | + | 
|  | 28 | +    /** | 
|  | 29 | +     * Register the service provider. | 
|  | 30 | +     * | 
|  | 31 | +     * @return void | 
|  | 32 | +     */ | 
|  | 33 | +    public function register() { | 
|  | 34 | +        $this->registerFilesystemProvider(); | 
|  | 35 | +        $this->registerDatabaseProvider(); | 
|  | 36 | +        $this->registerCompressorProvider(); | 
|  | 37 | +        $this->registerShellProcessor(); | 
|  | 38 | +        $this->registerArtisanCommands(); | 
|  | 39 | +    } | 
|  | 40 | + | 
|  | 41 | +    /** | 
|  | 42 | +     * Register the filesystem provider. | 
|  | 43 | +     * | 
|  | 44 | +     * @return void | 
|  | 45 | +     */ | 
|  | 46 | +    private function registerFilesystemProvider() { | 
|  | 47 | +        $this->app->bind(\BackupManager\Filesystems\FilesystemProvider::class, function ($app) { | 
|  | 48 | +            $provider = new Filesystems\FilesystemProvider(new Config($app['config']['backup-manager::storage'])); | 
|  | 49 | +            $provider->add(new Filesystems\Awss3Filesystem); | 
|  | 50 | +            $provider->add(new Filesystems\DropboxFilesystem); | 
|  | 51 | +            $provider->add(new Filesystems\FtpFilesystem); | 
|  | 52 | +            $provider->add(new Filesystems\LocalFilesystem); | 
|  | 53 | +            $provider->add(new Filesystems\RackspaceFilesystem); | 
|  | 54 | +            $provider->add(new Filesystems\SftpFilesystem); | 
|  | 55 | +            return $provider; | 
|  | 56 | +        }); | 
|  | 57 | +    } | 
|  | 58 | + | 
|  | 59 | +    /** | 
|  | 60 | +     * Register the database provider. | 
|  | 61 | +     * | 
|  | 62 | +     * @return void | 
|  | 63 | +     */ | 
|  | 64 | +    private function registerDatabaseProvider() { | 
|  | 65 | +        $this->app->bind(\BackupManager\Databases\DatabaseProvider::class, function ($app) { | 
|  | 66 | +            $provider = new Databases\DatabaseProvider($this->getDatabaseConfig($app['config']['database.connections'])); | 
|  | 67 | +            $provider->add(new Databases\MysqlDatabase); | 
|  | 68 | +            $provider->add(new Databases\PostgresqlDatabase); | 
|  | 69 | +            return $provider; | 
|  | 70 | +        }); | 
|  | 71 | +    } | 
|  | 72 | + | 
|  | 73 | +    /** | 
|  | 74 | +     * Register the compressor provider. | 
|  | 75 | +     * | 
|  | 76 | +     * @return void | 
|  | 77 | +     */ | 
|  | 78 | +    private function registerCompressorProvider() { | 
|  | 79 | +        $this->app->bind(\BackupManager\Compressors\CompressorProvider::class, function () { | 
|  | 80 | +            $provider = new Compressors\CompressorProvider; | 
|  | 81 | +            $provider->add(new Compressors\GzipCompressor); | 
|  | 82 | +            $provider->add(new Compressors\NullCompressor); | 
|  | 83 | +            return $provider; | 
|  | 84 | +        }); | 
|  | 85 | +    } | 
|  | 86 | + | 
|  | 87 | +    /** | 
|  | 88 | +     * Register the filesystem provider. | 
|  | 89 | +     * | 
|  | 90 | +     * @return void | 
|  | 91 | +     */ | 
|  | 92 | +    private function registerShellProcessor() { | 
|  | 93 | +        $this->app->bind(\BackupManager\ShellProcessing\ShellProcessor::class, function () { | 
|  | 94 | +            return new ShellProcessor(new Process('')); | 
|  | 95 | +        }); | 
|  | 96 | +    } | 
|  | 97 | + | 
|  | 98 | +    /** | 
|  | 99 | +     * Register the artisan commands. | 
|  | 100 | +     * | 
|  | 101 | +     * @return void | 
|  | 102 | +     */ | 
|  | 103 | +    private function registerArtisanCommands() { | 
|  | 104 | +        $this->commands([ | 
|  | 105 | +            \BackupManager\Laravel\DbBackupCommand::class, | 
|  | 106 | +            \BackupManager\Laravel\DbRestoreCommand::class, | 
|  | 107 | +            \BackupManager\Laravel\DbListCommand::class | 
|  | 108 | +        ]); | 
|  | 109 | +    } | 
|  | 110 | + | 
|  | 111 | +    /** | 
|  | 112 | +     * Get the services provided by the provider. | 
|  | 113 | +     * | 
|  | 114 | +     * @return array | 
|  | 115 | +     */ | 
|  | 116 | +    public function provides() { | 
|  | 117 | +        return [ | 
|  | 118 | +            \BackupManager\Filesystems\FilesystemProvider::class, | 
|  | 119 | +            \BackupManager\Databases\DatabaseProvider::class, | 
|  | 120 | +            \BackupManager\ShellProcessing\ShellProcessor::class, | 
|  | 121 | +        ]; | 
|  | 122 | +    } | 
|  | 123 | + | 
|  | 124 | +    private function getDatabaseConfig($connections) { | 
|  | 125 | +        $mapped = array_map(function ($connection) { | 
|  | 126 | +            if ( ! in_array($connection['driver'], ['mysql', 'pgsql'])) { | 
|  | 127 | +                return; | 
|  | 128 | +            } | 
|  | 129 | + | 
|  | 130 | +            if (isset($connection['port'])) { | 
|  | 131 | +                $port = $connection['port']; | 
|  | 132 | +            } else { | 
|  | 133 | +                if ($connection['driver'] == 'mysql') { | 
|  | 134 | +                    $port = '3306'; | 
|  | 135 | +                } elseif ($connection['driver'] == 'pgsql') { | 
|  | 136 | +                    $port = '5432'; | 
|  | 137 | +                } | 
|  | 138 | +            } | 
|  | 139 | + | 
|  | 140 | +            return [ | 
|  | 141 | +                'type'     => $connection['driver'], | 
|  | 142 | +                'host'     => $connection['host'], | 
|  | 143 | +                'port'     => $port, | 
|  | 144 | +                'user'     => $connection['username'], | 
|  | 145 | +                'pass'     => $connection['password'], | 
|  | 146 | +                'database' => $connection['database'], | 
|  | 147 | +            ]; | 
|  | 148 | +        }, $connections); | 
|  | 149 | +        return new Config($mapped); | 
|  | 150 | +    } | 
|  | 151 | +} | 
0 commit comments