|
13 | 13 | class DatabaseServiceProvider extends AbstractServiceProvider implements BootableServiceProviderInterface, BootablePluginProviderInterface
|
14 | 14 | {
|
15 | 15 |
|
16 |
| - /** |
17 |
| - * The provided array is a way to let the container |
18 |
| - * know that a service is provided by this service |
19 |
| - * provider. Every service that is registered via |
20 |
| - * this service provider must have an alias added |
21 |
| - * to this array or it will be ignored. |
22 |
| - * |
23 |
| - * @var array |
24 |
| - */ |
25 |
| - protected $provides = [ |
26 |
| - 'database' |
27 |
| - ]; |
28 |
| - |
29 |
| - /** |
30 |
| - * Add the Capsule\Manager into the plugin. |
31 |
| - * |
32 |
| - * @return void |
33 |
| - * |
34 |
| - */ |
35 |
| - public function boot() |
36 |
| - { |
37 |
| - global $wpdb; |
38 |
| - $container = $this->getContainer(); |
39 |
| - |
40 |
| - $container |
41 |
| - ->share('database', Capsule::class) |
42 |
| - ->addMethodCall('addConnection', [ |
43 |
| - 'config' => [ |
44 |
| - 'driver' => 'mysql', |
45 |
| - 'host' => DB_HOST, |
46 |
| - 'database' => DB_NAME, |
47 |
| - 'username' => DB_USER, |
48 |
| - 'password' => DB_PASSWORD, |
49 |
| - 'charset' => DB_CHARSET, |
50 |
| - 'prefix' => $wpdb->prefix, |
51 |
| - ] |
52 |
| - ]) |
53 |
| - ->addMethodCall('setAsGlobal'); |
54 |
| - |
55 |
| - // Boot eloquent should be when capsule is initialized. |
56 |
| - $container->get('database')->bootEloquent(); |
57 |
| - |
58 |
| - |
59 |
| - } |
60 |
| - |
61 |
| - /** |
62 |
| - * @return void |
63 |
| - */ |
64 |
| - public function register() |
65 |
| - { |
66 |
| - |
67 |
| - |
68 |
| - } |
69 |
| - |
70 |
| - /** |
71 |
| - * When the plugin is booted, register a new macro. |
72 |
| - * |
73 |
| - * Adds the `database()` method that returns shard instance of the Illuminate\Database\Capsule\Manager class. |
74 |
| - * |
75 |
| - * @return void |
76 |
| - * @example Model::where('wp_users', 1)->get(); |
77 |
| - */ |
78 |
| - public function bootPlugin() |
79 |
| - { |
80 |
| - |
81 |
| - $instance = $this; |
82 |
| - |
83 |
| - $this->getContainer()::macro( |
84 |
| - 'database', |
85 |
| - function () use ($instance) { |
86 |
| - return $instance->getContainer()->get('database'); |
87 |
| - } |
88 |
| - ); |
89 |
| - |
90 |
| - } |
| 16 | + /** |
| 17 | + * The provided array is a way to let the container |
| 18 | + * know that a service is provided by this service |
| 19 | + * provider. Every service that is registered via |
| 20 | + * this service provider must have an alias added |
| 21 | + * to this array or it will be ignored. |
| 22 | + * |
| 23 | + * @var array |
| 24 | + */ |
| 25 | + protected $provides = [ |
| 26 | + 'database' |
| 27 | + ]; |
| 28 | + |
| 29 | + /** |
| 30 | + * Add the Capsule\Manager into the plugin. |
| 31 | + * |
| 32 | + * @return void |
| 33 | + * |
| 34 | + */ |
| 35 | + public function boot() |
| 36 | + { |
| 37 | + global $wpdb; |
| 38 | + $container = $this->getContainer(); |
| 39 | + |
| 40 | + // Determine the charset and collation to use. |
| 41 | + $charsetCollate = $wpdb->determine_charset((!empty($wpdb->dbcharset) ? $wpdb->dbcharset : 'utf8'), ''); |
| 42 | + |
| 43 | + // Prepare the configuration array. |
| 44 | + $config = array( |
| 45 | + 'driver' => 'mysql', |
| 46 | + 'database' => $wpdb->dbname, |
| 47 | + 'username' => $wpdb->dbuser, |
| 48 | + 'password' => $wpdb->dbpassword, |
| 49 | + 'prefix' => $wpdb->prefix, |
| 50 | + 'charset' => $charsetCollate['charset'], |
| 51 | + ); |
| 52 | + |
| 53 | + // Parse the host to determine if it is an IPv6 address. |
| 54 | + list($host, $port, $socket, $isIpV6) = $wpdb->parse_db_host($wpdb->dbhost); |
| 55 | + |
| 56 | + // If the host is an IPv6 address, wrap it in square brackets. |
| 57 | + if (\extension_loaded('mysqlnd') && $isIpV6) { |
| 58 | + $host = "[{$host}]"; |
| 59 | + } |
| 60 | + |
| 61 | + if (isset($socket)) { |
| 62 | + $config['unix_socket'] = $socket; |
| 63 | + } else { |
| 64 | + $config['host'] = $host; |
| 65 | + if (isset($port)) { |
| 66 | + $config['port'] = $port; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + $container |
| 71 | + ->share('database', Capsule::class) |
| 72 | + ->addMethodCall('addConnection', [ |
| 73 | + 'config' => $config |
| 74 | + ]) |
| 75 | + ->addMethodCall('setAsGlobal'); |
| 76 | + |
| 77 | + // Boot eloquent should be when capsule is initialized. |
| 78 | + $container->get('database')->bootEloquent(); |
| 79 | + |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return void |
| 85 | + */ |
| 86 | + public function register() |
| 87 | + { |
| 88 | + |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * When the plugin is booted, register a new macro. |
| 94 | + * |
| 95 | + * Adds the `database()` method that returns shard instance of the Illuminate\Database\Capsule\Manager class. |
| 96 | + * |
| 97 | + * @return void |
| 98 | + * @example Model::where('wp_users', 1)->get(); |
| 99 | + */ |
| 100 | + public function bootPlugin() |
| 101 | + { |
| 102 | + |
| 103 | + $instance = $this; |
| 104 | + |
| 105 | + $this->getContainer()::macro( |
| 106 | + 'database', |
| 107 | + function () use ($instance) { |
| 108 | + return $instance->getContainer()->get('database'); |
| 109 | + } |
| 110 | + ); |
| 111 | + |
| 112 | + } |
91 | 113 |
|
92 | 114 | }
|
0 commit comments