Skip to content

Commit f58f955

Browse files
author
Shawn McCool
committed
Merge pull request #35 from DougSisk/lumen
Lumen Service Providers
2 parents a063dd5 + 163102e commit f58f955

File tree

3 files changed

+305
-0
lines changed

3 files changed

+305
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ php artisan vendor:publish --provider="BackupManager\Laravel\Laravel5ServiceProv
8686

8787
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.
8888

89+
#### Lumen Configuration
90+
91+
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`.
92+
93+
```php
94+
// FOR LUMEN 5.0 ONLY
95+
$app->configure('backup-manager');
96+
$app->register(BackupManager\Laravel\Lumen50ServiceProvider::class);
97+
98+
// FOR LUMEN 5.1 AND ABOVE
99+
$app->configure('backup-manager');
100+
$app->register(BackupManager\Laravel\LumenServiceProvider::class);
101+
```
102+
103+
Copy the `vendor/backup-manager/laravel/config/backup-manager.php` file to `config/backup-manager.php` and configure it to suit your needs.
104+
89105
**IoC Resolution**
90106

91107
`BackupManager\Manager` can be automatically resolved through constructor injection thanks to Laravel's IoC container.

src/Lumen50ServiceProvider.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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 Lumen50ServiceProvider 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\DropboxFilesystem);
44+
$provider->add(new Filesystems\FtpFilesystem);
45+
$provider->add(new Filesystems\LocalFilesystem);
46+
$provider->add(new Filesystems\RackspaceFilesystem);
47+
$provider->add(new Filesystems\SftpFilesystem);
48+
return $provider;
49+
});
50+
}
51+
52+
/**
53+
* Register the database provider.
54+
*
55+
* @return void
56+
*/
57+
private function registerDatabaseProvider() {
58+
$this->app->bind(\BackupManager\Databases\DatabaseProvider::class, function ($app) {
59+
$provider = new Databases\DatabaseProvider($this->getDatabaseConfig($app['config']['database.connections']));
60+
$provider->add(new Databases\MysqlDatabase);
61+
$provider->add(new Databases\PostgresqlDatabase);
62+
return $provider;
63+
});
64+
}
65+
66+
/**
67+
* Register the compressor provider.
68+
*
69+
* @return void
70+
*/
71+
private function registerCompressorProvider() {
72+
$this->app->bind(\BackupManager\Compressors\CompressorProvider::class, function () {
73+
$provider = new Compressors\CompressorProvider;
74+
$provider->add(new Compressors\GzipCompressor);
75+
$provider->add(new Compressors\NullCompressor);
76+
return $provider;
77+
});
78+
}
79+
80+
/**
81+
* Register the filesystem provider.
82+
*
83+
* @return void
84+
*/
85+
private function registerShellProcessor() {
86+
$this->app->bind(\BackupManager\ShellProcessing\ShellProcessor::class, function () {
87+
return new ShellProcessor(new Process(''));
88+
});
89+
}
90+
91+
/**
92+
* Register the artisan commands.
93+
*
94+
* @return void
95+
*/
96+
private function registerArtisanCommands() {
97+
$this->commands([
98+
\BackupManager\Laravel\Laravel50DbBackupCommand::class,
99+
\BackupManager\Laravel\Laravel50DbRestoreCommand::class,
100+
\BackupManager\Laravel\Laravel50DbListCommand::class,
101+
]);
102+
}
103+
104+
/**
105+
* Get the services provided by the provider.
106+
*
107+
* @return array
108+
*/
109+
public function provides() {
110+
return [
111+
\BackupManager\Filesystems\FilesystemProvider::class,
112+
\BackupManager\Databases\DatabaseProvider::class,
113+
\BackupManager\ShellProcessing\ShellProcessor::class,
114+
];
115+
}
116+
117+
private function getDatabaseConfig($connections) {
118+
$mapped = array_map(function ($connection) {
119+
if ( ! in_array($connection['driver'], ['mysql', 'pgsql'])) {
120+
return;
121+
}
122+
123+
if (isset($connection['port'])) {
124+
$port = $connection['port'];
125+
} else {
126+
if ($connection['driver'] == 'mysql') {
127+
$port = '3306';
128+
} elseif ($connection['driver'] == 'pgsql') {
129+
$port = '5432';
130+
}
131+
}
132+
133+
return [
134+
'type' => $connection['driver'],
135+
'host' => $connection['host'],
136+
'port' => $port,
137+
'user' => $connection['username'],
138+
'pass' => $connection['password'],
139+
'database' => $connection['database'],
140+
];
141+
}, $connections);
142+
return new Config($mapped);
143+
}
144+
}

src/LumenServiceProvider.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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

Comments
 (0)