DatabaseServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace CloudyCity\LaravelBuilderMacros\Providers;
4
5
use CloudyCity\LaravelBuilderMacros\Library\Database\MySqlConnection;
6
use Illuminate\Database\Connection;
7
use Illuminate\Support\ServiceProvider;
8
9
class DatabaseServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Override the default connection for MySQL. This allows us to use `replace` etc.
13
     *
14
     * @link https://stidges.com/extending-the-connection-class-in-laravel
15
     * @link https://gist.github.com/VinceG/0fb570925748ab35bc53f2a798cb517c
16
     *
17
     * @return void
18
     */
19
    public function boot()
20
    {
21
        // 5.4 and above will use this method to bind
22
        if (method_exists(Connection::class, 'resolverFor')) {
23
            /* @noinspection PhpUndefinedMethodInspection */
24
            Connection::resolverFor('mysql', function ($connection, $database, $prefix, $config) {
25
                return new MySqlConnection($connection, $database, $prefix, $config);
26
            });
27
        }
28
    }
29
30
    /**
31
     * Register the service provider.
32
     *
33
     * @return void
34
     */
35
    public function register()
36
    {
37
        // 5.3 and below will use this method to bind
38
        if (! method_exists(Connection::class, 'resolverFor')) {
39
            $this->app->bind('db.connection.mysql', MySqlConnection::class);
40
        }
41
    }
42
}
43