[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Service/Provider/ -> Database.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright   (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  namespace Joomla\CMS\Service\Provider;
  11  
  12  use Joomla\Database\DatabaseDriver;
  13  use Joomla\Database\DatabaseInterface;
  14  use Joomla\Database\Mysql\MysqlDriver;
  15  use Joomla\DI\Container;
  16  use Joomla\DI\ServiceProviderInterface;
  17  use Joomla\Event\DispatcherInterface;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('JPATH_PLATFORM') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Service provider for the application's database dependency
  25   *
  26   * @since  4.0.0
  27   */
  28  class Database implements ServiceProviderInterface
  29  {
  30      /**
  31       * Registers the service provider with a DI container.
  32       *
  33       * @param   Container  $container  The DI container.
  34       *
  35       * @return  void
  36       *
  37       * @since   4.0.0
  38       */
  39      public function register(Container $container)
  40      {
  41          $container->alias('db', DatabaseInterface::class)
  42              ->alias('DatabaseDriver', DatabaseInterface::class)
  43              ->alias(DatabaseDriver::class, DatabaseInterface::class)
  44              ->share(
  45                  DatabaseInterface::class,
  46                  function (Container $container) {
  47                      $conf = $container->get('config');
  48  
  49                      /**
  50                       * @todo: This 'sensible' default is required in the installer for now. Eventually we need to
  51                       *        refactor the installer so it is not required
  52                       */
  53                      $dbtype = $conf->get('dbtype', 'mysqli');
  54  
  55                      /*
  56                       * In Joomla! 3.x and earlier the `mysql` type was used for the `ext/mysql` PHP extension, which is no longer supported.
  57                       * The `pdomysql` type represented the PDO MySQL adapter.  With the Framework's package in use, the PDO MySQL adapter
  58                       * is now the `mysql` type.  Therefore, we check two conditions:
  59                       *
  60                       * 1) Is the type `pdomysql`, if so switch to `mysql`
  61                       * 2) Is the type `mysql`, if so make sure PDO MySQL is supported and if not switch to `mysqli`
  62                       *
  63                       * For these cases, if a connection cannot be made with MySQLi, the database API will handle throwing an Exception
  64                       * so we don't need to make any additional checks for MySQLi.
  65                       */
  66                      if (strtolower($dbtype) === 'pdomysql') {
  67                          $dbtype = 'mysql';
  68                      }
  69  
  70                      if (strtolower($dbtype) === 'mysql') {
  71                          if (!MysqlDriver::isSupported()) {
  72                              $dbtype = 'mysqli';
  73                          }
  74                      }
  75  
  76                      /*
  77                       * Joomla! 4.0 removes support for the `ext/pgsql` PHP extension.  To help with the migration, we will migrate the configuration
  78                       * to the PDO PostgreSQL driver regardless of if the environment supports it.  Instead of getting a "driver not found" type of
  79                       * error, this will instead force the API to report that the driver is not supported.
  80                       */
  81                      if (strtolower($dbtype) === 'postgresql') {
  82                          $dbtype = 'pgsql';
  83                      }
  84  
  85                      $options = [
  86                          'driver'   => $dbtype,
  87                          'host'     => $conf->get('host'),
  88                          'user'     => $conf->get('user'),
  89                          'password' => $conf->get('password'),
  90                          'database' => $conf->get('db'),
  91                          'prefix'   => $conf->get('dbprefix'),
  92                      ];
  93  
  94                      if ((int) $conf->get('dbencryption') !== 0) {
  95                          $options['ssl'] = [
  96                              'enable'             => true,
  97                              'verify_server_cert' => (bool) $conf->get('dbsslverifyservercert'),
  98                          ];
  99  
 100                          foreach (['cipher', 'ca', 'key', 'cert'] as $value) {
 101                              $confVal = trim($conf->get('dbssl' . $value, ''));
 102  
 103                              if ($confVal !== '') {
 104                                  $options['ssl'][$value] = $confVal;
 105                              }
 106                          }
 107                      }
 108  
 109                      // Enable utf8mb4 connections for mysql adapters
 110                      if (strtolower($dbtype) === 'mysqli') {
 111                          $options['utf8mb4'] = true;
 112                      }
 113  
 114                      if (strtolower($dbtype) === 'mysql') {
 115                          $options['charset'] = 'utf8mb4';
 116                      }
 117  
 118                      if (JDEBUG) {
 119                          $options['monitor'] = new \Joomla\Database\Monitor\DebugMonitor();
 120                      }
 121  
 122                      try {
 123                          $db = DatabaseDriver::getInstance($options);
 124                      } catch (\RuntimeException $e) {
 125                          if (!headers_sent()) {
 126                              header('HTTP/1.1 500 Internal Server Error');
 127                          }
 128  
 129                          jexit('Database Error: ' . $e->getMessage());
 130                      }
 131  
 132                      $db->setDispatcher($container->get(DispatcherInterface::class));
 133  
 134                      return $db;
 135                  },
 136                  true
 137              );
 138      }
 139  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer