|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ShiftOneLabs\LaravelNomad; |
|
4
|
|
|
|
|
5
|
|
|
use LogicException; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Illuminate\Database\Connection; |
|
8
|
|
|
use Illuminate\Support\ServiceProvider; |
|
9
|
|
|
|
|
10
|
|
|
class LaravelNomadServiceProvider extends ServiceProvider |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The connection classes to use for the drivers. |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $classes = [ |
|
18
|
|
|
'mysql' => 'ShiftOneLabs\LaravelNomad\Extension\Database\MySqlConnection', |
|
19
|
|
|
'pgsql' => 'ShiftOneLabs\LaravelNomad\Extension\Database\PostgresConnection', |
|
20
|
|
|
'sqlite' => 'ShiftOneLabs\LaravelNomad\Extension\Database\SQLiteConnection', |
|
21
|
|
|
'sqlsrv' => 'ShiftOneLabs\LaravelNomad\Extension\Database\SqlServerConnection', |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Register any application services. |
|
26
|
|
|
* |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
2613 |
|
public function register() |
|
30
|
|
|
{ |
|
31
|
2613 |
|
$this->registerFeatureDetection(); |
|
32
|
|
|
|
|
33
|
2613 |
|
$this->registerConnections(); |
|
34
|
2613 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Register the feature detection service. |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
2613 |
|
public function registerFeatureDetection() |
|
42
|
|
|
{ |
|
43
|
2613 |
|
$this->app->singleton('nomad.feature.detection', 'ShiftOneLabs\LaravelNomad\FeatureDetection'); |
|
44
|
2613 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Register the database connection extensions. |
|
48
|
|
|
* |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
2613 |
|
public function registerConnections() |
|
52
|
|
|
{ |
|
53
|
2613 |
|
$driver = $this->app['nomad.feature.detection']->connectionResolverDriver(); |
|
54
|
|
|
|
|
55
|
2613 |
|
$method = 'registerVia'.Str::studly($driver); |
|
56
|
|
|
|
|
57
|
2613 |
|
if (method_exists($this, $method)) { |
|
58
|
2613 |
|
return $this->{$method}(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
67 |
|
throw new LogicException(sprintf('Connection registration method [%s] does not exist.', $method)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Register the database connection extensions through app bindings. |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
1599 |
|
public function registerViaBindings() |
|
70
|
|
|
{ |
|
71
|
1599 |
|
foreach ($this->classes as $driver => $class) { |
|
72
|
1599 |
|
$this->app->bind('db.connection.'.$driver, $class); |
|
73
|
390 |
|
} |
|
74
|
1599 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Register the database connection extensions through the `Connection` method. |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
1014 |
|
public function registerViaConnectionMethod() |
|
82
|
|
|
{ |
|
83
|
1014 |
|
foreach ($this->classes as $driver => $class) { |
|
84
|
1014 |
|
Connection::resolverFor($driver, function ($connection, $database, $prefix, $config) use ($class) { |
|
85
|
728 |
|
return new $class($connection, $database, $prefix, $config); |
|
86
|
1014 |
|
}); |
|
87
|
39 |
|
} |
|
88
|
1014 |
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|