1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
<?php namespace Illuminate\Database\Capsule;
use PDO; use Illuminate\Support\Fluent; use Illuminate\Events\Dispatcher; use Illuminate\Cache\CacheManager; use Illuminate\Container\Container; use Illuminate\Database\DatabaseManager; use Illuminate\Database\Eloquent\Model as Eloquent; use Illuminate\Database\Connectors\ConnectionFactory;
class Manager {
/** * The current globally used instance. * * @var \Illuminate\Database\Capsule\Manager */ protected static $instance;
/** * The database manager instance. * * @var \Illuminate\Database\DatabaseManager */ protected $manager;
/** * The container instance. * * @var \Illuminate\Container\Container */ protected $container;
/** * Create a new database capsule manager. * * @param \Illuminate\Container\Container|null $container * @return void */ public function __construct(Container $container = null) { $this->setupContainer($container);
// Once we have the container setup, we will setup the default configuration // options in the container "config" binding. This will make the database // manager behave correctly since all the correct binding are in place. $this->setupDefaultConfiguration();
$this->setupManager(); }
/** * Setup the IoC container instance. * * @param \Illuminate\Container\Container|null $container * @return void */ protected function setupContainer($container) { $this->container = $container ?: new Container;
$this->container->instance('config', new Fluent); }
/** * Setup the default database configuration options. * * @return void */ protected function setupDefaultConfiguration() { $this->container['config']['database.fetch'] = PDO::FETCH_ASSOC;
$this->container['config']['database.default'] = 'default'; }
/** * Build the database manager instance. * * @return void */ protected function setupManager() { $factory = new ConnectionFactory($this->container);
$this->manager = new DatabaseManager($this->container, $factory); }
/** * Get a connection instance from the global manager. * * @param string $connection * @return \Illuminate\Database\Connection */ public static function connection($connection = null) { return static::$instance->getConnection($connection); }
/** * Get a fluent query builder instance. * * @param string $table * @param string $connection * @return \Illuminate\Database\Query\Builder */ public static function table($table, $connection = null) { return static::$instance->connection($connection)->table($table); }
/** * Get a schema builder instance. * * @param string $connection * @return \Illuminate\Database\Schema\Builder */ public static function schema($connection = null) { return static::$instance->connection($connection)->getSchemaBuilder(); }
/** * Get a registered connection instance. * * @param string $name * @return \Illuminate\Database\Connection */ public function getConnection($name = null) { return $this->manager->connection($name); }
/** * Register a connection with the manager. * * @param array $config * @param string $name * @return void */ public function addConnection(array $config, $name = 'default') { $connections = $this->container['config']['database.connections'];
$connections[$name] = $config;
$this->container['config']['database.connections'] = $connections; }
/** * Bootstrap Eloquent so it is ready for usage. * * @return void */ public function bootEloquent() { Eloquent::setConnectionResolver($this->manager);
// If we have an event dispatcher instance, we will go ahead and register it // with the Eloquent ORM, allowing for model callbacks while creating and // updating "model" instances; however, if it not necessary to operate. if ($dispatcher = $this->getEventDispatcher()) { Eloquent::setEventDispatcher($dispatcher); } }
/** * Set the fetch mode for the database connections. * * @param int $fetchMode * @return \Illuminate\Database\Capsule\Manager */ public function setFetchMode($fetchMode) { $this->container['config']['database.fetch'] = $fetchMode;
return $this; }
/** * Make this capsule instance available globally. * * @return void */ public function setAsGlobal() { static::$instance = $this; }
/** * Get the database manager instance. * * @return \Illuminate\Database\Manager */ public function getDatabaseManager() { return $this->manager; }
/** * Get the current event dispatcher instance. * * @return \Illuminate\Events\Dispatcher */ public function getEventDispatcher() { if ($this->container->bound('events')) { return $this->container['events']; } }
/** * Set the event dispatcher instance to be used by connections. * * @param \Illuminate\Events\Dispatcher $dispatcher * @return void */ public function setEventDispatcher(Dispatcher $dispatcher) { $this->container->instance('events', $dispatcher); }
/** * Get the current cache manager instance. * * @return \Illuminate\Cache\Manager */ public function getCacheManager() { if ($this->container->bound('cache')) { return $this->container['cache']; } }
/** * Set the cache manager to be used by connections. * * @param \Illuminate\Cache\CacheManager $cache * @return void */ public function setCacheManager(CacheManager $cache) { $this->container->instance('cache', $cache); }
/** * Get the IoC container instance. * * @return \Illuminate\Container\Container */ public function getContainer() { return $this->container; }
/** * Set the IoC container instance. * * @param \Illuminate\Container\Container $container * @return void */ public function setContainer(Container $container) { $this->container = $container; }
/** * Dynamically pass methods to the default connection. * * @param string $method * @param array $parameters * @return mixed */ public static function __callStatic($method, $parameters) { return call_user_func_array(array(static::connection(), $method), $parameters); }
}
|