| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Database\Capsule;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Container\Container;
|
|
|
6 |
use Illuminate\Contracts\Events\Dispatcher;
|
|
|
7 |
use Illuminate\Database\Connectors\ConnectionFactory;
|
|
|
8 |
use Illuminate\Database\DatabaseManager;
|
|
|
9 |
use Illuminate\Database\Eloquent\Model as Eloquent;
|
|
|
10 |
use Illuminate\Support\Traits\CapsuleManagerTrait;
|
|
|
11 |
use PDO;
|
|
|
12 |
|
|
|
13 |
class Manager
|
|
|
14 |
{
|
|
|
15 |
use CapsuleManagerTrait;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* The database manager instance.
|
|
|
19 |
*
|
|
|
20 |
* @var \Illuminate\Database\DatabaseManager
|
|
|
21 |
*/
|
|
|
22 |
protected $manager;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Create a new database capsule manager.
|
|
|
26 |
*
|
|
|
27 |
* @param \Illuminate\Container\Container|null $container
|
|
|
28 |
* @return void
|
|
|
29 |
*/
|
|
|
30 |
public function __construct(Container $container = null)
|
|
|
31 |
{
|
|
|
32 |
$this->setupContainer($container ?: new Container);
|
|
|
33 |
|
|
|
34 |
// Once we have the container setup, we will setup the default configuration
|
|
|
35 |
// options in the container "config" binding. This will make the database
|
|
|
36 |
// manager work correctly out of the box without extreme configuration.
|
|
|
37 |
$this->setupDefaultConfiguration();
|
|
|
38 |
|
|
|
39 |
$this->setupManager();
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Setup the default database configuration options.
|
|
|
44 |
*
|
|
|
45 |
* @return void
|
|
|
46 |
*/
|
|
|
47 |
protected function setupDefaultConfiguration()
|
|
|
48 |
{
|
|
|
49 |
$this->container['config']['database.fetch'] = PDO::FETCH_OBJ;
|
|
|
50 |
|
|
|
51 |
$this->container['config']['database.default'] = 'default';
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* Build the database manager instance.
|
|
|
56 |
*
|
|
|
57 |
* @return void
|
|
|
58 |
*/
|
|
|
59 |
protected function setupManager()
|
|
|
60 |
{
|
|
|
61 |
$factory = new ConnectionFactory($this->container);
|
|
|
62 |
|
|
|
63 |
$this->manager = new DatabaseManager($this->container, $factory);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Get a connection instance from the global manager.
|
|
|
68 |
*
|
|
|
69 |
* @param string|null $connection
|
|
|
70 |
* @return \Illuminate\Database\Connection
|
|
|
71 |
*/
|
|
|
72 |
public static function connection($connection = null)
|
|
|
73 |
{
|
|
|
74 |
return static::$instance->getConnection($connection);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Get a fluent query builder instance.
|
|
|
79 |
*
|
|
|
80 |
* @param \Closure|\Illuminate\Database\Query\Builder|string $table
|
|
|
81 |
* @param string|null $as
|
|
|
82 |
* @param string|null $connection
|
|
|
83 |
* @return \Illuminate\Database\Query\Builder
|
|
|
84 |
*/
|
|
|
85 |
public static function table($table, $as = null, $connection = null)
|
|
|
86 |
{
|
|
|
87 |
return static::$instance->connection($connection)->table($table, $as);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Get a schema builder instance.
|
|
|
92 |
*
|
|
|
93 |
* @param string|null $connection
|
|
|
94 |
* @return \Illuminate\Database\Schema\Builder
|
|
|
95 |
*/
|
|
|
96 |
public static function schema($connection = null)
|
|
|
97 |
{
|
|
|
98 |
return static::$instance->connection($connection)->getSchemaBuilder();
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Get a registered connection instance.
|
|
|
103 |
*
|
|
|
104 |
* @param string|null $name
|
|
|
105 |
* @return \Illuminate\Database\Connection
|
|
|
106 |
*/
|
|
|
107 |
public function getConnection($name = null)
|
|
|
108 |
{
|
|
|
109 |
return $this->manager->connection($name);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Register a connection with the manager.
|
|
|
114 |
*
|
|
|
115 |
* @param array $config
|
|
|
116 |
* @param string $name
|
|
|
117 |
* @return void
|
|
|
118 |
*/
|
|
|
119 |
public function addConnection(array $config, $name = 'default')
|
|
|
120 |
{
|
|
|
121 |
$connections = $this->container['config']['database.connections'];
|
|
|
122 |
|
|
|
123 |
$connections[$name] = $config;
|
|
|
124 |
|
|
|
125 |
$this->container['config']['database.connections'] = $connections;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Bootstrap Eloquent so it is ready for usage.
|
|
|
130 |
*
|
|
|
131 |
* @return void
|
|
|
132 |
*/
|
|
|
133 |
public function bootEloquent()
|
|
|
134 |
{
|
|
|
135 |
Eloquent::setConnectionResolver($this->manager);
|
|
|
136 |
|
|
|
137 |
// If we have an event dispatcher instance, we will go ahead and register it
|
|
|
138 |
// with the Eloquent ORM, allowing for model callbacks while creating and
|
|
|
139 |
// updating "model" instances; however, it is not necessary to operate.
|
|
|
140 |
if ($dispatcher = $this->getEventDispatcher()) {
|
|
|
141 |
Eloquent::setEventDispatcher($dispatcher);
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Set the fetch mode for the database connections.
|
|
|
147 |
*
|
|
|
148 |
* @param int $fetchMode
|
|
|
149 |
* @return $this
|
|
|
150 |
*/
|
|
|
151 |
public function setFetchMode($fetchMode)
|
|
|
152 |
{
|
|
|
153 |
$this->container['config']['database.fetch'] = $fetchMode;
|
|
|
154 |
|
|
|
155 |
return $this;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Get the database manager instance.
|
|
|
160 |
*
|
|
|
161 |
* @return \Illuminate\Database\DatabaseManager
|
|
|
162 |
*/
|
|
|
163 |
public function getDatabaseManager()
|
|
|
164 |
{
|
|
|
165 |
return $this->manager;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* Get the current event dispatcher instance.
|
|
|
170 |
*
|
|
|
171 |
* @return \Illuminate\Contracts\Events\Dispatcher|null
|
|
|
172 |
*/
|
|
|
173 |
public function getEventDispatcher()
|
|
|
174 |
{
|
|
|
175 |
if ($this->container->bound('events')) {
|
|
|
176 |
return $this->container['events'];
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
/**
|
|
|
181 |
* Set the event dispatcher instance to be used by connections.
|
|
|
182 |
*
|
|
|
183 |
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
|
|
|
184 |
* @return void
|
|
|
185 |
*/
|
|
|
186 |
public function setEventDispatcher(Dispatcher $dispatcher)
|
|
|
187 |
{
|
|
|
188 |
$this->container->instance('events', $dispatcher);
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Dynamically pass methods to the default connection.
|
|
|
193 |
*
|
|
|
194 |
* @param string $method
|
|
|
195 |
* @param array $parameters
|
|
|
196 |
* @return mixed
|
|
|
197 |
*/
|
|
|
198 |
public static function __callStatic($method, $parameters)
|
|
|
199 |
{
|
|
|
200 |
return static::connection()->$method(...$parameters);
|
|
|
201 |
}
|
|
|
202 |
}
|