comparison core/lib/Drupal/Core/Database/Database.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
363 363
364 if (!$driver = self::$databaseInfo[$key][$target]['driver']) { 364 if (!$driver = self::$databaseInfo[$key][$target]['driver']) {
365 throw new DriverNotSpecifiedException('Driver not specified for this database connection: ' . $key); 365 throw new DriverNotSpecifiedException('Driver not specified for this database connection: ' . $key);
366 } 366 }
367 367
368 if (!empty(self::$databaseInfo[$key][$target]['namespace'])) { 368 $namespace = static::getDatabaseDriverNamespace(self::$databaseInfo[$key][$target]);
369 $driver_class = self::$databaseInfo[$key][$target]['namespace'] . '\\Connection'; 369 $driver_class = $namespace . '\\Connection';
370 }
371 else {
372 // Fallback for Drupal 7 settings.php.
373 $driver_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
374 }
375 370
376 $pdo_connection = $driver_class::open(self::$databaseInfo[$key][$target]); 371 $pdo_connection = $driver_class::open(self::$databaseInfo[$key][$target]);
377 $new_connection = new $driver_class($pdo_connection, self::$databaseInfo[$key][$target]); 372 $new_connection = new $driver_class($pdo_connection, self::$databaseInfo[$key][$target]);
378 $new_connection->setTarget($target); 373 $new_connection->setTarget($target);
379 $new_connection->setKey($key); 374 $new_connection->setKey($key);
453 * @throws \InvalidArgumentException 448 * @throws \InvalidArgumentException
454 * Exception thrown when the provided URL does not meet the minimum 449 * Exception thrown when the provided URL does not meet the minimum
455 * requirements. 450 * requirements.
456 */ 451 */
457 public static function convertDbUrlToConnectionInfo($url, $root) { 452 public static function convertDbUrlToConnectionInfo($url, $root) {
458 $info = parse_url($url); 453 // Check that the URL is well formed, starting with 'scheme://', where
459 if (!isset($info['scheme'], $info['host'], $info['path'])) { 454 // 'scheme' is a database driver name.
460 throw new \InvalidArgumentException('Minimum requirement: driver://host/database'); 455 if (preg_match('/^(.*):\/\//', $url, $matches) !== 1) {
461 } 456 throw new \InvalidArgumentException("Missing scheme in URL '$url'");
462 $info += [ 457 }
463 'user' => '', 458 $driver = $matches[1];
464 'pass' => '', 459
465 'fragment' => '', 460 // Discover if the URL has a valid driver scheme. Try with core drivers
466 ]; 461 // first.
467 462 $connection_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
468 // A SQLite database path with two leading slashes indicates a system path. 463 if (!class_exists($connection_class)) {
469 // Otherwise the path is relative to the Drupal root. 464 // If the URL is not relative to a core driver, try with custom ones.
470 if ($info['path'][0] === '/') { 465 $connection_class = "Drupal\\Driver\\Database\\{$driver}\\Connection";
471 $info['path'] = substr($info['path'], 1); 466 if (!class_exists($connection_class)) {
472 } 467 throw new \InvalidArgumentException("Can not convert '$url' to a database connection, class '$connection_class' does not exist");
473 if ($info['scheme'] === 'sqlite' && $info['path'][0] !== '/') { 468 }
474 $info['path'] = $root . '/' . $info['path']; 469 }
475 } 470
476 471 return $connection_class::createConnectionOptionsFromUrl($url, $root);
477 $database = [
478 'driver' => $info['scheme'],
479 'username' => $info['user'],
480 'password' => $info['pass'],
481 'host' => $info['host'],
482 'database' => $info['path'],
483 ];
484 if (isset($info['port'])) {
485 $database['port'] = $info['port'];
486 }
487 return $database;
488 } 472 }
489 473
490 /** 474 /**
491 * Gets database connection info as a URL. 475 * Gets database connection info as a URL.
492 * 476 *
493 * @param string $key 477 * @param string $key
494 * (Optional) The database connection key. 478 * (Optional) The database connection key.
495 * 479 *
496 * @return string 480 * @return string
497 * The connection info as a URL. 481 * The connection info as a URL.
482 *
483 * @throws \RuntimeException
484 * When the database connection is not defined.
498 */ 485 */
499 public static function getConnectionInfoAsUrl($key = 'default') { 486 public static function getConnectionInfoAsUrl($key = 'default') {
500 $db_info = static::getConnectionInfo($key); 487 $db_info = static::getConnectionInfo($key);
501 if ($db_info['default']['driver'] == 'sqlite') { 488 if (empty($db_info) || empty($db_info['default'])) {
502 $db_url = 'sqlite://localhost/' . $db_info['default']['database']; 489 throw new \RuntimeException("Database connection $key not defined or missing the 'default' settings");
503 } 490 }
504 else { 491 $connection_class = static::getDatabaseDriverNamespace($db_info['default']) . '\\Connection';
505 $user = ''; 492 return $connection_class::createUrlFromConnectionOptions($db_info['default']);
506 if ($db_info['default']['username']) { 493 }
507 $user = $db_info['default']['username']; 494
508 if ($db_info['default']['password']) { 495 /**
509 $user .= ':' . $db_info['default']['password']; 496 * Gets the PHP namespace of a database driver from the connection info.
510 } 497 *
511 $user .= '@'; 498 * @param array $connection_info
512 } 499 * The database connection information, as defined in settings.php. The
513 500 * structure of this array depends on the database driver it is connecting
514 $db_url = $db_info['default']['driver'] . '://' . $user . $db_info['default']['host']; 501 * to.
515 if (isset($db_info['default']['port'])) { 502 *
516 $db_url .= ':' . $db_info['default']['port']; 503 * @return string
517 } 504 * The PHP namespace of the driver's database.
518 $db_url .= '/' . $db_info['default']['database']; 505 */
519 } 506 protected static function getDatabaseDriverNamespace(array $connection_info) {
520 if ($db_info['default']['prefix']['default']) { 507 if (isset($connection_info['namespace'])) {
521 $db_url .= '#' . $db_info['default']['prefix']['default']; 508 return $connection_info['namespace'];
522 } 509 }
523 return $db_url; 510 // Fallback for Drupal 7 settings.php.
511 return 'Drupal\\Core\\Database\\Driver\\' . $connection_info['driver'];
524 } 512 }
525 513
526 } 514 }