This commit is contained in:
Paolo A
2024-08-13 13:44:16 +00:00
parent 1bbb23088d
commit e796d76612
4001 changed files with 30101 additions and 40075 deletions

85
vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php vendored Normal file → Executable file
View File

@@ -2,12 +2,14 @@
namespace Illuminate\Database;
use Doctrine\DBAL\Types\Type;
use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Support\Arr;
use Illuminate\Support\ConfigurationUrlParser;
use Illuminate\Support\Str;
use InvalidArgumentException;
use PDO;
use RuntimeException;
/**
* @mixin \Illuminate\Database\Connection
@@ -49,6 +51,13 @@ class DatabaseManager implements ConnectionResolverInterface
*/
protected $reconnector;
/**
* The custom Doctrine column types.
*
* @var array
*/
protected $doctrineTypes = [];
/**
* Create a new database manager instance.
*
@@ -62,7 +71,7 @@ class DatabaseManager implements ConnectionResolverInterface
$this->factory = $factory;
$this->reconnector = function ($connection) {
$this->reconnect($connection->getName());
$this->reconnect($connection->getNameWithReadWriteType());
};
}
@@ -165,7 +174,7 @@ class DatabaseManager implements ConnectionResolverInterface
*/
protected function configure(Connection $connection, $type)
{
$connection = $this->setPdoForType($connection, $type);
$connection = $this->setPdoForType($connection, $type)->setReadWriteType($type);
// First we'll set the fetch mode and a few other dependencies of the database
// connection. This method basically just configures and prepares it to get
@@ -174,11 +183,17 @@ class DatabaseManager implements ConnectionResolverInterface
$connection->setEventDispatcher($this->app['events']);
}
if ($this->app->bound('db.transactions')) {
$connection->setTransactionManager($this->app['db.transactions']);
}
// Here we'll set a reconnector callback. This reconnector can be any callable
// so we will set a Closure to reconnect from this manager with the name of
// the connection, which will allow us to reconnect from the connections.
$connection->setReconnector($this->reconnector);
$this->registerConfiguredDoctrineTypes($connection);
return $connection;
}
@@ -200,6 +215,49 @@ class DatabaseManager implements ConnectionResolverInterface
return $connection;
}
/**
* Register custom Doctrine types with the connection.
*
* @param \Illuminate\Database\Connection $connection
* @return void
*/
protected function registerConfiguredDoctrineTypes(Connection $connection): void
{
foreach ($this->app['config']->get('database.dbal.types', []) as $name => $class) {
$this->registerDoctrineType($class, $name, $name);
}
foreach ($this->doctrineTypes as $name => [$type, $class]) {
$connection->registerDoctrineType($class, $name, $type);
}
}
/**
* Register a custom Doctrine type.
*
* @param string $class
* @param string $name
* @param string $type
* @return void
*
* @throws \Doctrine\DBAL\DBALException
* @throws \RuntimeException
*/
public function registerDoctrineType(string $class, string $name, string $type): void
{
if (! class_exists('Doctrine\DBAL\Connection')) {
throw new RuntimeException(
'Registering a custom Doctrine type requires Doctrine DBAL (doctrine/dbal).'
);
}
if (! Type::hasType($name)) {
Type::addType($name, $class);
}
$this->doctrineTypes[$name] = [$type, $class];
}
/**
* Disconnect from the given database and remove from local cache.
*
@@ -271,11 +329,15 @@ class DatabaseManager implements ConnectionResolverInterface
*/
protected function refreshPdoConnections($name)
{
$fresh = $this->makeConnection($name);
[$database, $type] = $this->parseConnectionName($name);
$fresh = $this->configure(
$this->makeConnection($database), $type
);
return $this->connections[$name]
->setPdo($fresh->getRawPdo())
->setReadPdo($fresh->getRawReadPdo());
->setPdo($fresh->getRawPdo())
->setReadPdo($fresh->getRawReadPdo());
}
/**
@@ -355,6 +417,19 @@ class DatabaseManager implements ConnectionResolverInterface
$this->reconnector = $reconnector;
}
/**
* Set the application instance used by the manager.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return $this
*/
public function setApplication($app)
{
$this->app = $app;
return $this;
}
/**
* Dynamically pass methods to the default connection.
*