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