PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.2.0
Independent Analytics – WordPress Analytics Plugin v2.2.0
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / illuminate / database / DatabaseManager.php
independent-analytics / vendor / illuminate / database Last commit date
Capsule 2 years ago Concerns 2 years ago Connectors 2 years ago Console 2 years ago DBAL 2 years ago Eloquent 2 years ago Events 2 years ago Migrations 2 years ago PDO 2 years ago Query 2 years ago Schema 2 years ago ClassMorphViolationException.php 2 years ago ConfigurationUrlParser.php 2 years ago Connection.php 2 years ago ConnectionInterface.php 2 years ago ConnectionResolver.php 2 years ago ConnectionResolverInterface.php 2 years ago DatabaseManager.php 2 years ago DatabaseServiceProvider.php 2 years ago DatabaseTransactionRecord.php 2 years ago DatabaseTransactionsManager.php 2 years ago DetectsConcurrencyErrors.php 2 years ago DetectsLostConnections.php 2 years ago Grammar.php 2 years ago LazyLoadingViolationException.php 2 years ago MigrationServiceProvider.php 2 years ago MultipleRecordsFoundException.php 2 years ago MySqlConnection.php 2 years ago PostgresConnection.php 2 years ago QueryException.php 2 years ago RecordsNotFoundException.php 2 years ago SQLiteConnection.php 2 years ago Seeder.php 2 years ago SqlServerConnection.php 2 years ago
DatabaseManager.php
375 lines
1 <?php
2
3 namespace IAWP_SCOPED\Illuminate\Database;
4
5 use IAWP_SCOPED\Doctrine\DBAL\Types\Type;
6 use IAWP_SCOPED\Illuminate\Database\Connectors\ConnectionFactory;
7 use IAWP_SCOPED\Illuminate\Support\Arr;
8 use IAWP_SCOPED\Illuminate\Support\ConfigurationUrlParser;
9 use IAWP_SCOPED\Illuminate\Support\Str;
10 use InvalidArgumentException;
11 use PDO;
12 use RuntimeException;
13 /**
14 * @mixin \Illuminate\Database\Connection
15 * @internal
16 */
17 class DatabaseManager implements ConnectionResolverInterface
18 {
19 /**
20 * The application instance.
21 *
22 * @var \Illuminate\Contracts\Foundation\Application
23 */
24 protected $app;
25 /**
26 * The database connection factory instance.
27 *
28 * @var \Illuminate\Database\Connectors\ConnectionFactory
29 */
30 protected $factory;
31 /**
32 * The active connection instances.
33 *
34 * @var array
35 */
36 protected $connections = [];
37 /**
38 * The custom connection resolvers.
39 *
40 * @var array
41 */
42 protected $extensions = [];
43 /**
44 * The callback to be executed to reconnect to a database.
45 *
46 * @var callable
47 */
48 protected $reconnector;
49 /**
50 * The custom Doctrine column types.
51 *
52 * @var array
53 */
54 protected $doctrineTypes = [];
55 /**
56 * Create a new database manager instance.
57 *
58 * @param \Illuminate\Contracts\Foundation\Application $app
59 * @param \Illuminate\Database\Connectors\ConnectionFactory $factory
60 * @return void
61 */
62 public function __construct($app, ConnectionFactory $factory)
63 {
64 $this->app = $app;
65 $this->factory = $factory;
66 $this->reconnector = function ($connection) {
67 $this->reconnect($connection->getNameWithReadWriteType());
68 };
69 }
70 /**
71 * Get a database connection instance.
72 *
73 * @param string|null $name
74 * @return \Illuminate\Database\Connection
75 */
76 public function connection($name = null)
77 {
78 [$database, $type] = $this->parseConnectionName($name);
79 $name = $name ?: $database;
80 // If we haven't created this connection, we'll create it based on the config
81 // provided in the application. Once we've created the connections we will
82 // set the "fetch mode" for PDO which determines the query return types.
83 if (!isset($this->connections[$name])) {
84 $this->connections[$name] = $this->configure($this->makeConnection($database), $type);
85 }
86 return $this->connections[$name];
87 }
88 /**
89 * Parse the connection into an array of the name and read / write type.
90 *
91 * @param string $name
92 * @return array
93 */
94 protected function parseConnectionName($name)
95 {
96 $name = $name ?: $this->getDefaultConnection();
97 return Str::endsWith($name, ['::read', '::write']) ? \explode('::', $name, 2) : [$name, null];
98 }
99 /**
100 * Make the database connection instance.
101 *
102 * @param string $name
103 * @return \Illuminate\Database\Connection
104 */
105 protected function makeConnection($name)
106 {
107 $config = $this->configuration($name);
108 // First we will check by the connection name to see if an extension has been
109 // registered specifically for that connection. If it has we will call the
110 // Closure and pass it the config allowing it to resolve the connection.
111 if (isset($this->extensions[$name])) {
112 return \call_user_func($this->extensions[$name], $config, $name);
113 }
114 // Next we will check to see if an extension has been registered for a driver
115 // and will call the Closure if so, which allows us to have a more generic
116 // resolver for the drivers themselves which applies to all connections.
117 if (isset($this->extensions[$driver = $config['driver']])) {
118 return \call_user_func($this->extensions[$driver], $config, $name);
119 }
120 return $this->factory->make($config, $name);
121 }
122 /**
123 * Get the configuration for a connection.
124 *
125 * @param string $name
126 * @return array
127 *
128 * @throws \InvalidArgumentException
129 */
130 protected function configuration($name)
131 {
132 $name = $name ?: $this->getDefaultConnection();
133 // To get the database connection configuration, we will just pull each of the
134 // connection configurations and get the configurations for the given name.
135 // If the configuration doesn't exist, we'll throw an exception and bail.
136 $connections = $this->app['config']['database.connections'];
137 if (\is_null($config = Arr::get($connections, $name))) {
138 throw new InvalidArgumentException("Database connection [{$name}] not configured.");
139 }
140 return (new ConfigurationUrlParser())->parseConfiguration($config);
141 }
142 /**
143 * Prepare the database connection instance.
144 *
145 * @param \Illuminate\Database\Connection $connection
146 * @param string $type
147 * @return \Illuminate\Database\Connection
148 */
149 protected function configure(Connection $connection, $type)
150 {
151 $connection = $this->setPdoForType($connection, $type)->setReadWriteType($type);
152 // First we'll set the fetch mode and a few other dependencies of the database
153 // connection. This method basically just configures and prepares it to get
154 // used by the application. Once we're finished we'll return it back out.
155 if ($this->app->bound('events')) {
156 $connection->setEventDispatcher($this->app['events']);
157 }
158 if ($this->app->bound('db.transactions')) {
159 $connection->setTransactionManager($this->app['db.transactions']);
160 }
161 // Here we'll set a reconnector callback. This reconnector can be any callable
162 // so we will set a Closure to reconnect from this manager with the name of
163 // the connection, which will allow us to reconnect from the connections.
164 $connection->setReconnector($this->reconnector);
165 $this->registerConfiguredDoctrineTypes($connection);
166 return $connection;
167 }
168 /**
169 * Prepare the read / write mode for database connection instance.
170 *
171 * @param \Illuminate\Database\Connection $connection
172 * @param string|null $type
173 * @return \Illuminate\Database\Connection
174 */
175 protected function setPdoForType(Connection $connection, $type = null)
176 {
177 if ($type === 'read') {
178 $connection->setPdo($connection->getReadPdo());
179 } elseif ($type === 'write') {
180 $connection->setReadPdo($connection->getPdo());
181 }
182 return $connection;
183 }
184 /**
185 * Register custom Doctrine types with the connection.
186 *
187 * @param \Illuminate\Database\Connection $connection
188 * @return void
189 */
190 protected function registerConfiguredDoctrineTypes(Connection $connection) : void
191 {
192 foreach ($this->app['config']->get('database.dbal.types', []) as $name => $class) {
193 $this->registerDoctrineType($class, $name, $name);
194 }
195 foreach ($this->doctrineTypes as $name => [$type, $class]) {
196 $connection->registerDoctrineType($class, $name, $type);
197 }
198 }
199 /**
200 * Register a custom Doctrine type.
201 *
202 * @param string $class
203 * @param string $name
204 * @param string $type
205 * @return void
206 *
207 * @throws \Doctrine\DBAL\DBALException
208 * @throws \RuntimeException
209 */
210 public function registerDoctrineType(string $class, string $name, string $type) : void
211 {
212 if (!\class_exists('IAWP_SCOPED\\Doctrine\\DBAL\\Connection')) {
213 throw new RuntimeException('Registering a custom Doctrine type requires Doctrine DBAL (doctrine/dbal).');
214 }
215 if (!Type::hasType($name)) {
216 Type::addType($name, $class);
217 }
218 $this->doctrineTypes[$name] = [$type, $class];
219 }
220 /**
221 * Disconnect from the given database and remove from local cache.
222 *
223 * @param string|null $name
224 * @return void
225 */
226 public function purge($name = null)
227 {
228 $name = $name ?: $this->getDefaultConnection();
229 $this->disconnect($name);
230 unset($this->connections[$name]);
231 }
232 /**
233 * Disconnect from the given database.
234 *
235 * @param string|null $name
236 * @return void
237 */
238 public function disconnect($name = null)
239 {
240 if (isset($this->connections[$name = $name ?: $this->getDefaultConnection()])) {
241 $this->connections[$name]->disconnect();
242 }
243 }
244 /**
245 * Reconnect to the given database.
246 *
247 * @param string|null $name
248 * @return \Illuminate\Database\Connection
249 */
250 public function reconnect($name = null)
251 {
252 $this->disconnect($name = $name ?: $this->getDefaultConnection());
253 if (!isset($this->connections[$name])) {
254 return $this->connection($name);
255 }
256 return $this->refreshPdoConnections($name);
257 }
258 /**
259 * Set the default database connection for the callback execution.
260 *
261 * @param string $name
262 * @param callable $callback
263 * @return mixed
264 */
265 public function usingConnection($name, callable $callback)
266 {
267 $previousName = $this->getDefaultConnection();
268 $this->setDefaultConnection($name);
269 return tap($callback(), function () use($previousName) {
270 $this->setDefaultConnection($previousName);
271 });
272 }
273 /**
274 * Refresh the PDO connections on a given connection.
275 *
276 * @param string $name
277 * @return \Illuminate\Database\Connection
278 */
279 protected function refreshPdoConnections($name)
280 {
281 [$database, $type] = $this->parseConnectionName($name);
282 $fresh = $this->configure($this->makeConnection($database), $type);
283 return $this->connections[$name]->setPdo($fresh->getRawPdo())->setReadPdo($fresh->getRawReadPdo());
284 }
285 /**
286 * Get the default connection name.
287 *
288 * @return string
289 */
290 public function getDefaultConnection()
291 {
292 return $this->app['config']['database.default'];
293 }
294 /**
295 * Set the default connection name.
296 *
297 * @param string $name
298 * @return void
299 */
300 public function setDefaultConnection($name)
301 {
302 $this->app['config']['database.default'] = $name;
303 }
304 /**
305 * Get all of the support drivers.
306 *
307 * @return array
308 */
309 public function supportedDrivers()
310 {
311 return ['mysql', 'pgsql', 'sqlite', 'sqlsrv'];
312 }
313 /**
314 * Get all of the drivers that are actually available.
315 *
316 * @return array
317 */
318 public function availableDrivers()
319 {
320 return \array_intersect($this->supportedDrivers(), \str_replace('dblib', 'sqlsrv', PDO::getAvailableDrivers()));
321 }
322 /**
323 * Register an extension connection resolver.
324 *
325 * @param string $name
326 * @param callable $resolver
327 * @return void
328 */
329 public function extend($name, callable $resolver)
330 {
331 $this->extensions[$name] = $resolver;
332 }
333 /**
334 * Return all of the created connections.
335 *
336 * @return array
337 */
338 public function getConnections()
339 {
340 return $this->connections;
341 }
342 /**
343 * Set the database reconnector callback.
344 *
345 * @param callable $reconnector
346 * @return void
347 */
348 public function setReconnector(callable $reconnector)
349 {
350 $this->reconnector = $reconnector;
351 }
352 /**
353 * Set the application instance used by the manager.
354 *
355 * @param \Illuminate\Contracts\Foundation\Application $app
356 * @return $this
357 */
358 public function setApplication($app)
359 {
360 $this->app = $app;
361 return $this;
362 }
363 /**
364 * Dynamically pass methods to the default connection.
365 *
366 * @param string $method
367 * @param array $parameters
368 * @return mixed
369 */
370 public function __call($method, $parameters)
371 {
372 return $this->connection()->{$method}(...$parameters);
373 }
374 }
375