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