From dc9be835deea5d65ce9bf140afed2b664d8def7f Mon Sep 17 00:00:00 2001 From: Doug Sisk Date: Wed, 23 Dec 2015 16:39:57 -0700 Subject: [PATCH 1/4] Lumen Service Providers --- src/Lumen50ServiceProvider.php | 144 ++++++++++++++++++++++++++++++++ src/LumenServiceProvider.php | 145 +++++++++++++++++++++++++++++++++ 2 files changed, 289 insertions(+) create mode 100644 src/Lumen50ServiceProvider.php create mode 100644 src/LumenServiceProvider.php diff --git a/src/Lumen50ServiceProvider.php b/src/Lumen50ServiceProvider.php new file mode 100644 index 0000000..a808e58 --- /dev/null +++ b/src/Lumen50ServiceProvider.php @@ -0,0 +1,144 @@ +mergeConfigFrom($configPath, 'backup-manager'); + $this->registerFilesystemProvider(); + $this->registerDatabaseProvider(); + $this->registerCompressorProvider(); + $this->registerShellProcessor(); + $this->registerArtisanCommands(); + } + + /** + * Register the filesystem provider. + * + * @return void + */ + private function registerFilesystemProvider() { + $this->app->bind(\BackupManager\Filesystems\FilesystemProvider::class, function ($app) { + $provider = new Filesystems\FilesystemProvider(new Config($app['config']['backup-manager'])); + $provider->add(new Filesystems\Awss3Filesystem); + $provider->add(new Filesystems\DropboxFilesystem); + $provider->add(new Filesystems\FtpFilesystem); + $provider->add(new Filesystems\LocalFilesystem); + $provider->add(new Filesystems\RackspaceFilesystem); + $provider->add(new Filesystems\SftpFilesystem); + return $provider; + }); + } + + /** + * Register the database provider. + * + * @return void + */ + private function registerDatabaseProvider() { + $this->app->bind(\BackupManager\Databases\DatabaseProvider::class, function ($app) { + $provider = new Databases\DatabaseProvider($this->getDatabaseConfig($app['config']['database.connections'])); + $provider->add(new Databases\MysqlDatabase); + $provider->add(new Databases\PostgresqlDatabase); + return $provider; + }); + } + + /** + * Register the compressor provider. + * + * @return void + */ + private function registerCompressorProvider() { + $this->app->bind(\BackupManager\Compressors\CompressorProvider::class, function () { + $provider = new Compressors\CompressorProvider; + $provider->add(new Compressors\GzipCompressor); + $provider->add(new Compressors\NullCompressor); + return $provider; + }); + } + + /** + * Register the filesystem provider. + * + * @return void + */ + private function registerShellProcessor() { + $this->app->bind(\BackupManager\ShellProcessing\ShellProcessor::class, function () { + return new ShellProcessor(new Process('')); + }); + } + + /** + * Register the artisan commands. + * + * @return void + */ + private function registerArtisanCommands() { + $this->commands([ + \BackupManager\Laravel\Laravel50DbBackupCommand::class, + \BackupManager\Laravel\Laravel50DbRestoreCommand::class, + \BackupManager\Laravel\Laravel50DbListCommand::class, + ]); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() { + return [ + \BackupManager\Filesystems\FilesystemProvider::class, + \BackupManager\Databases\DatabaseProvider::class, + \BackupManager\ShellProcessing\ShellProcessor::class, + ]; + } + + private function getDatabaseConfig($connections) { + $mapped = array_map(function ($connection) { + if ( ! in_array($connection['driver'], ['mysql', 'pgsql'])) { + return; + } + + if (isset($connection['port'])) { + $port = $connection['port']; + } else { + if ($connection['driver'] == 'mysql') { + $port = '3306'; + } elseif ($connection['driver'] == 'pgsql') { + $port = '5432'; + } + } + + return [ + 'type' => $connection['driver'], + 'host' => $connection['host'], + 'port' => $port, + 'user' => $connection['username'], + 'pass' => $connection['password'], + 'database' => $connection['database'], + ]; + }, $connections); + return new Config($mapped); + } +} diff --git a/src/LumenServiceProvider.php b/src/LumenServiceProvider.php new file mode 100644 index 0000000..64cec63 --- /dev/null +++ b/src/LumenServiceProvider.php @@ -0,0 +1,145 @@ +mergeConfigFrom($configPath, 'backup-manager'); + $this->registerFilesystemProvider(); + $this->registerDatabaseProvider(); + $this->registerCompressorProvider(); + $this->registerShellProcessor(); + $this->registerArtisanCommands(); + } + + /** + * Register the filesystem provider. + * + * @return void + */ + private function registerFilesystemProvider() { + $this->app->bind(\BackupManager\Filesystems\FilesystemProvider::class, function ($app) { + $provider = new Filesystems\FilesystemProvider(new Config($app['config']['backup-manager'])); + $provider->add(new Filesystems\Awss3Filesystem); + $provider->add(new Filesystems\GcsFilesystem); + $provider->add(new Filesystems\DropboxFilesystem); + $provider->add(new Filesystems\FtpFilesystem); + $provider->add(new Filesystems\LocalFilesystem); + $provider->add(new Filesystems\RackspaceFilesystem); + $provider->add(new Filesystems\SftpFilesystem); + return $provider; + }); + } + + /** + * Register the database provider. + * + * @return void + */ + private function registerDatabaseProvider() { + $this->app->bind(\BackupManager\Databases\DatabaseProvider::class, function ($app) { + $provider = new Databases\DatabaseProvider($this->getDatabaseConfig($app['config']['database.connections'])); + $provider->add(new Databases\MysqlDatabase); + $provider->add(new Databases\PostgresqlDatabase); + return $provider; + }); + } + + /** + * Register the compressor provider. + * + * @return void + */ + private function registerCompressorProvider() { + $this->app->bind(\BackupManager\Compressors\CompressorProvider::class, function () { + $provider = new Compressors\CompressorProvider; + $provider->add(new Compressors\GzipCompressor); + $provider->add(new Compressors\NullCompressor); + return $provider; + }); + } + + /** + * Register the filesystem provider. + * + * @return void + */ + private function registerShellProcessor() { + $this->app->bind(\BackupManager\ShellProcessing\ShellProcessor::class, function () { + return new ShellProcessor(new Process('', null, null, null, null)); + }); + } + + /** + * Register the artisan commands. + * + * @return void + */ + private function registerArtisanCommands() { + $this->commands([ + \BackupManager\Laravel\Laravel5DbBackupCommand::class, + \BackupManager\Laravel\Laravel5DbRestoreCommand::class, + \BackupManager\Laravel\Laravel5DbListCommand::class, + ]); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() { + return [ + \BackupManager\Filesystems\FilesystemProvider::class, + \BackupManager\Databases\DatabaseProvider::class, + \BackupManager\ShellProcessing\ShellProcessor::class, + ]; + } + + private function getDatabaseConfig($connections) { + $mapped = array_map(function ($connection) { + if ( ! in_array($connection['driver'], ['mysql', 'pgsql'])) { + return; + } + + if (isset($connection['port'])) { + $port = $connection['port']; + } else { + if ($connection['driver'] == 'mysql') { + $port = '3306'; + } elseif ($connection['driver'] == 'pgsql') { + $port = '5432'; + } + } + + return [ + 'type' => $connection['driver'], + 'host' => $connection['host'], + 'port' => $port, + 'user' => $connection['username'], + 'pass' => $connection['password'], + 'database' => $connection['database'], + ]; + }, $connections); + return new Config($mapped); + } +} From 25fb03cfb6768a165be7806745b80a1b512296f0 Mon Sep 17 00:00:00 2001 From: Doug Sisk Date: Thu, 24 Dec 2015 13:25:20 -0700 Subject: [PATCH 2/4] Lumen configuration in README --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 5ab5f79..eb4e740 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,22 @@ php artisan vendor:publish --provider="BackupManager\Laravel\Laravel5ServiceProv The Backup Manager will make use of Laravel's database configuration. But, it won't know about any connections that might be tied to other environments, so it can be best to just list multiple connections in the `config/database.php` file. +#### Lumen Configuration + +To install into a Lumen project, first do the composer install then add *ONE* of the following service providers to your `boostrap/app.php`. + +```php +// FOR LUMEN 5.0 ONLY +$app->configure('backup-manager'); +$app->register(BackupManager\Laravel\Lumen50ServiceProvider::class); + +// FOR LUMEN 5.1 AND ABOVE +$app->configure('backup-manager'); +$app->register(BackupManager\Laravel\LumenServiceProvider::class); +``` + +Copy the `vendor/backup-manager/laravel/config/backup-manager.php` file to `config/backup-manager.php` and configure it to suit your needs. + **IoC Resolution** `BackupManager\Manager` can be automatically resolved through constructor injection thanks to Laravel's IoC container. From ed2522b5695ed2956791c8b67dd19e27b56b304e Mon Sep 17 00:00:00 2001 From: Doug Sisk Date: Thu, 24 Dec 2015 13:26:02 -0700 Subject: [PATCH 3/4] Incorrect spelling --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eb4e740..a96d78a 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ The Backup Manager will make use of Laravel's database configuration. But, it wo #### Lumen Configuration -To install into a Lumen project, first do the composer install then add *ONE* of the following service providers to your `boostrap/app.php`. +To install into a Lumen project, first do the composer install then add *ONE* of the following service providers to your `bootstrap/app.php`. ```php // FOR LUMEN 5.0 ONLY From 163102eb80a7580c125a77b25dbd380f952f7147 Mon Sep 17 00:00:00 2001 From: Doug Sisk Date: Thu, 24 Dec 2015 13:27:10 -0700 Subject: [PATCH 4/4] Configuration file has to be manually loaded --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a96d78a..51a54fc 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ The Backup Manager will make use of Laravel's database configuration. But, it wo #### Lumen Configuration -To install into a Lumen project, first do the composer install then add *ONE* of the following service providers to your `bootstrap/app.php`. +To install into a Lumen project, first do the composer install then add the configuration file loader and *ONE* of the following service providers to your `bootstrap/app.php`. ```php // FOR LUMEN 5.0 ONLY