CoreFiles by mySites.guru
Database.php114 lines, 3,194 bytes, from Joomla_4.0.0-alpha5-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha3
md5
15fcfa5bac5458e8f3f953317c2fa6c1
sha1
0111b3a677d63aa54ce9574cb286ab93eccd8d69
sha256
d0bcb9bc007757a8d888d8270249614e99d2943b05a98c3c0ed5ffe4e5950f21
<?php
/**
 * @package     Joomla.Libraries
 * @subpackage  Service
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 */

namespace Joomla\CMS\Service\Provider;

defined('JPATH_PLATFORM') or die;

use Joomla\Database\DatabaseDriver;
use Joomla\Database\DatabaseInterface;
use Joomla\Database\Mysql\MysqlDriver;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;

/**
 * Service provider for the application's database dependency
 *
 * @since  4.0
 */
class Database implements ServiceProviderInterface
{
	/**
	 * Registers the service provider with a DI container.
	 *
	 * @param   Container  $container  The DI container.
	 *
	 * @return  void
	 *
	 * @since   4.0
	 */
	public function register(Container $container)
	{
		$container->alias('db', DatabaseInterface::class)
			->alias('DatabaseDriver', DatabaseInterface::class)
			->alias(DatabaseDriver::class, DatabaseInterface::class)
			->share(
				DatabaseInterface::class,
			function (Container $container)
			{
				$conf = $container->get('config');

				$dbtype = $conf->get('dbtype');

				/*
				 * In Joomla! 3.x and earlier the `mysql` type was used for the `ext/mysql` PHP extension, which is no longer supported.
				 * The `pdomysql` type represented the PDO MySQL adapter.  With the Framework's package in use, the PDO MySQL adapter
				 * is now the `mysql` type.  Therefore, we check two conditions:
				 *
				 * 1) Is the type `pdomysql`, if so switch to `mysql`
				 * 2) Is the type `mysql`, if so make sure PDO MySQL is supported and if not switch to `mysqli`
				 *
				 * For these cases, if a connection cannot be made with MySQLi, the database API will handle throwing an Exception
				 * so we don't need to make any additional checks for MySQLi.
				 */
				if (strtolower($dbtype) === 'pdomysql')
				{
					$dbtype = 'mysql';
				}

				if (strtolower($dbtype) === 'mysql')
				{
					if (!MysqlDriver::isSupported())
					{
						$dbtype = 'mysqli';
					}
				}

				/*
				 * Joomla! 4.0 removes support for the `ext/pgsql` PHP extension.  To help with the migration, we will migrate the configuration
				 * to the PDO PostgreSQL driver regardless of if the environment supports it.  Instead of getting a "driver not found" type of
				 * error, this will instead force the API to report that the driver is not supported.
				 */
				if (strtolower($dbtype) === 'postgresql')
				{
					$dbtype = 'pgsql';
				}

				$options = [
					'driver'   => $dbtype,
					'host'     => $conf->get('host'),
					'user'     => $conf->get('user'),
					'password' => $conf->get('password'),
					'database' => $conf->get('db'),
					'prefix'   => $conf->get('dbprefix'),
				];

				try
				{
					$db = DatabaseDriver::getInstance($options);
				}
				catch (\RuntimeException $e)
				{
					if (!headers_sent())
					{
						header('HTTP/1.1 500 Internal Server Error');
					}

					jexit('Database Error: ' . $e->getMessage());
				}

				$db->setDispatcher($container->get(DispatcherInterface::class));

				return $db;
			},
			true
		);
	}
}

The same content is in 2 releases, Joomla 4.0.0-alpha5 to 4.0.0-alpha6. See them all, or every version of this file.