Commit 9062e1e4 authored by Crell's avatar Crell

Simplify the class name finding and loading routines, since PSR-0 now handles them for us.

parent 67429ab1
......@@ -583,15 +583,10 @@ protected function expandArguments(&$query, &$args) {
*
* @param string $class
* The class for which we want the potentially driver-specific class.
* @param array $files
* The name of the files in which the driver-specific class can be.
* @param $use_autoload
* If TRUE, attempt to load classes using PHP's autoload capability
* as well as the manual approach here.
* @return string
* The name of the class that should be used for this driver.
*/
public function getDriverClass($class, array $files = array(), $use_autoload = FALSE) {
public function getDriverClass($class) {
if (empty($this->driverClasses[$class])) {
$driver = $this->driver();
$this->driverClasses[$class] = "Drupal\\Database\\Driver\\{$driver}\\{$class}";
......@@ -619,7 +614,7 @@ public function getDriverClass($class, array $files = array(), $use_autoload = F
* @see SelectQuery
*/
public function select($table, $alias = NULL, array $options = array()) {
$class = $this->getDriverClass('Select', array('query.inc', 'select.inc'));
$class = $this->getDriverClass('Select');
return new $class($table, $alias, $this, $options);
}
......@@ -635,7 +630,7 @@ public function select($table, $alias = NULL, array $options = array()) {
* @see InsertQuery
*/
public function insert($table, array $options = array()) {
$class = $this->getDriverClass('Insert', array('query.inc'));
$class = $this->getDriverClass('Insert');
return new $class($this, $table, $options);
}
......@@ -651,7 +646,7 @@ public function insert($table, array $options = array()) {
* @see MergeQuery
*/
public function merge($table, array $options = array()) {
$class = $this->getDriverClass('Merge', array('query.inc'));
$class = $this->getDriverClass('Merge');
return new $class($this, $table, $options);
}
......@@ -668,7 +663,7 @@ public function merge($table, array $options = array()) {
* @see UpdateQuery
*/
public function update($table, array $options = array()) {
$class = $this->getDriverClass('Update', array('query.inc'));
$class = $this->getDriverClass('Update');
return new $class($this, $table, $options);
}
......@@ -684,7 +679,7 @@ public function update($table, array $options = array()) {
* @see DeleteQuery
*/
public function delete($table, array $options = array()) {
$class = $this->getDriverClass('Delete', array('query.inc'));
$class = $this->getDriverClass('Delete');
return new $class($this, $table, $options);
}
......@@ -700,7 +695,7 @@ public function delete($table, array $options = array()) {
* @see TruncateQuery
*/
public function truncate($table, array $options = array()) {
$class = $this->getDriverClass('Truncate', array('query.inc'));
$class = $this->getDriverClass('Truncate');
return new $class($this, $table, $options);
}
......@@ -714,7 +709,7 @@ public function truncate($table, array $options = array()) {
*/
public function schema() {
if (empty($this->schema)) {
$class = $this->getDriverClass('DatabaseSchema', array('schema.inc'));
$class = $this->getDriverClass('DatabaseSchema');
if (class_exists($class)) {
$this->schema = new $class($this);
}
......
......@@ -423,33 +423,4 @@ public static function closeConnection($target = NULL, $key = NULL) {
public static function ignoreTarget($key, $target) {
self::$ignoreTargets[$key][$target] = TRUE;
}
/**
* Load a file for the database that might hold a class.
*
* @param $driver
* The name of the driver.
* @param array $files
* The name of the files the driver specific class can be.
*/
public static function loadDriverFile($driver, array $files = array()) {
static $base_path;
if (empty($base_path)) {
$base_path = dirname(realpath(__FILE__));
}
$driver_base_path = "$base_path/$driver";
foreach ($files as $file) {
// Load the base file first so that classes extending base classes will
// have the base class loaded.
foreach (array("$base_path/$file", "$driver_base_path/$file") as $filename) {
// The OS caches file_exists() and PHP caches require_once(), so
// we'll let both of those take care of performance here.
if (file_exists($filename)) {
require_once $filename;
}
}
}
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\Database\Driver\mysql;
use Drupal\Database\Database;
use Drupal\Database\Connection as DatabaseConnection;
use PDO;
......
......@@ -4,6 +4,8 @@
use Drupal\Database\DatabaseSchema as DatabaseDatabaseSchema;
use Exception;
/**
* @ingroup schemaapi
* @{
......
......@@ -171,7 +171,7 @@ public function useDefaults(array $fields) {
* @return InsertQuery
* The called object.
*/
public function from(SelectQueryInterface $query) {
public function from(SelectInterface $query) {
$this->fromQuery = $query;
return $this;
}
......
......@@ -136,9 +136,7 @@ public function havingCompile(Connection $connection) {
/* Implementations of QueryExtendableInterface. */
public function extend($extender_name) {
// The extender can be anywhere so this needs to go to the registry, which
// is surely loaded by now.
$class = $this->connection->getDriverClass($extender_name, array(), TRUE);
$class = $this->connection->getDriverClass($extender_name);
return new $class($this, $this->connection);
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\Database\Query;
use Drupal\Database\Database;
use Drupal\Database\Connection;
......
<?php
use Drupal\Database\Database;
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Symfony\Component\ClassLoader\ApcUniversalClassLoader;
/**
* @file
......@@ -2290,6 +2293,19 @@ function _drupal_bootstrap_configuration() {
// Initialize the configuration, including variables from settings.php.
drupal_settings_initialize();
drupal_initialize_classloader();
}
/**
* Initializes the class loader.
*
* The class loader is responsible for lazy-loading all PSR-0 compatible
* classes, interfaces, and traits (PHP 5.4 and later). Its only dependency
* is that variable_get() be defined and DRUPAL_ROOT be defined. Otherwise it
* may be called as early as possible.
*/
function drupal_initialize_classloader() {
// Hook up the Symfony ClassLoader for loading PSR-0-compatible classes.
require_once(DRUPAL_ROOT . '/core/includes/Symfony/Component/ClassLoader/UniversalClassLoader.php');
......@@ -2301,7 +2317,7 @@ function _drupal_bootstrap_configuration() {
case 'apc':
if (function_exists('apc_store')) {
require_once(DRUPAL_ROOT . '/core/includes/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php');
$loader = new \Symfony\Component\ClassLoader\ApcUniversalClassLoader('drupal.' . $GLOBALS['drupal_hash_salt']);
$loader = new ApcUniversalClassLoader('drupal.' . $GLOBALS['drupal_hash_salt']);
break;
}
// If APC was not loaded, fall through to the default loader so that
......@@ -2309,7 +2325,7 @@ function _drupal_bootstrap_configuration() {
case 'dev':
case 'default':
default:
$loader = new \Symfony\Component\ClassLoader\UniversalClassLoader();
$loader = new UniversalClassLoader();
break;
}
......
<?php
/**
* @file
* Installation code for MySQL embedded database engine.
*/
/**
* Specifies installation tasks for MySQL and equivalent databases.
*/
class DatabaseTasks_mysql extends DatabaseTasks {
/**
* The PDO driver name for MySQL and equivalent databases.
*
* @var string
*/
protected $pdoDriver = 'mysql';
/**
* Returns a human-readable name string for MySQL and equivalent databases.
*/
public function name() {
return st('MySQL, MariaDB, or equivalent');
}
/**
* Returns the minimum version for MySQL.
*/
public function minimumVersion() {
return '5.0.15';
}
}
<?php
use Drupal\Database\Database;
/**
* @file
* Install functions for PostgreSQL embedded database engine.
*/
// PostgreSQL specific install functions
class DatabaseTasks_pgsql extends DatabaseTasks {
protected $pdoDriver = 'pgsql';
public function __construct() {
$this->tasks[] = array(
'function' => 'checkEncoding',
'arguments' => array(),
);
$this->tasks[] = array(
'function' => 'checkBinaryOutput',
'arguments' => array(),
);
$this->tasks[] = array(
'function' => 'initializeDatabase',
'arguments' => array(),
);
}
public function name() {
return st('PostgreSQL');
}
public function minimumVersion() {
return '8.3';
}
/**
* Check encoding is UTF8.
*/
protected function checkEncoding() {
try {
if (db_query('SHOW server_encoding')->fetchField() == 'UTF8') {
$this->pass(st('Database is encoded in UTF-8'));
}
else {
$replacements = array(
'%encoding' => 'UTF8',
'%driver' => $this->name(),
'!link' => '<a href="INSTALL.pgsql.txt">INSTALL.pgsql.txt</a>'
);
$text = 'The %driver database must use %encoding encoding to work with Drupal.';
$text .= 'Recreate the database with %encoding encoding. See !link for more details.';
$this->fail(st($text, $replacements));
}
}
catch (Exception $e) {
$this->fail(st('Drupal could not determine the encoding of the database was set to UTF-8'));
}
}
/**
* Check Binary Output.
*
* Unserializing does not work on Postgresql 9 when bytea_output is 'hex'.
*/
function checkBinaryOutput() {
// PostgreSQL < 9 doesn't support bytea_output, so verify we are running
// at least PostgreSQL 9.
$database_connection = Database::getConnection();
if (version_compare($database_connection->version(), '9') >= 0) {
if (!$this->checkBinaryOutputSuccess()) {
// First try to alter the database. If it fails, raise an error telling
// the user to do it themselves.
$connection_options = $database_connection->getConnectionOptions();
// It is safe to include the database name directly here, because this
// code is only called when a connection to the database is already
// established, thus the database name is guaranteed to be a correct
// value.
$query = "ALTER DATABASE \"" . $connection_options['database'] . "\" SET bytea_output = 'escape';";
try {
db_query($query);
}
catch (Exception $e) {
// Ignore possible errors when the user doesn't have the necessary
// privileges to ALTER the database.
}
// Close the database connection so that the configuration parameter
// is applied to the current connection.
db_close();
// Recheck, if it fails, finally just rely on the end user to do the
// right thing.
if (!$this->checkBinaryOutputSuccess()) {
$replacements = array(
'%setting' => 'bytea_output',
'%current_value' => 'hex',
'%needed_value' => 'escape',
'!query' => "<code>" . $query . "</code>",
);
$this->fail(st("The %setting setting is currently set to '%current_value', but needs to be '%needed_value'. Change this by running the following query: !query", $replacements));
}
}
}
}
/**
* Verify that a binary data roundtrip returns the original string.
*/
protected function checkBinaryOutputSuccess() {
$bytea_output = db_query("SELECT 'encoding'::bytea AS output")->fetchField();
return ($bytea_output == 'encoding');
}
/**
* Make PostgreSQL Drupal friendly.
*/
function initializeDatabase() {
// We create some functions using global names instead of prefixing them
// like we do with table names. This is so that we don't double up if more
// than one instance of Drupal is running on a single database. We therefore
// avoid trying to create them again in that case.
try {
// Create functions.
db_query('CREATE OR REPLACE FUNCTION "greatest"(numeric, numeric) RETURNS numeric AS
\'SELECT CASE WHEN (($1 > $2) OR ($2 IS NULL)) THEN $1 ELSE $2 END;\'
LANGUAGE \'sql\''
);
db_query('CREATE OR REPLACE FUNCTION "greatest"(numeric, numeric, numeric) RETURNS numeric AS
\'SELECT greatest($1, greatest($2, $3));\'
LANGUAGE \'sql\''
);
// Don't use {} around pg_proc table.
if (!db_query("SELECT COUNT(*) FROM pg_proc WHERE proname = 'rand'")->fetchField()) {
db_query('CREATE OR REPLACE FUNCTION "rand"() RETURNS float AS
\'SELECT random();\'
LANGUAGE \'sql\''
);
}
db_query('CREATE OR REPLACE FUNCTION "substring_index"(text, text, integer) RETURNS text AS
\'SELECT array_to_string((string_to_array($1, $2)) [1:$3], $2);\'
LANGUAGE \'sql\''
);
// Using || to concatenate in Drupal is not recommeneded because there are
// database drivers for Drupal that do not support the syntax, however
// they do support CONCAT(item1, item2) which we can replicate in
// PostgreSQL. PostgreSQL requires the function to be defined for each
// different argument variation the function can handle.
db_query('CREATE OR REPLACE FUNCTION "concat"(anynonarray, anynonarray) RETURNS text AS
\'SELECT CAST($1 AS text) || CAST($2 AS text);\'
LANGUAGE \'sql\'
');
db_query('CREATE OR REPLACE FUNCTION "concat"(text, anynonarray) RETURNS text AS
\'SELECT $1 || CAST($2 AS text);\'
LANGUAGE \'sql\'
');
db_query('CREATE OR REPLACE FUNCTION "concat"(anynonarray, text) RETURNS text AS
\'SELECT CAST($1 AS text) || $2;\'
LANGUAGE \'sql\'
');
db_query('CREATE OR REPLACE FUNCTION "concat"(text, text) RETURNS text AS
\'SELECT $1 || $2;\'
LANGUAGE \'sql\'
');
$this->pass(st('PostgreSQL has initialized itself.'));
}
catch (Exception $e) {
$this->fail(st('Drupal could not be correctly setup with the existing database. Revise any errors.'));
}
}
}
<?php
/**
* @file
* SQLite specific install functions
*/
class DatabaseTasks_sqlite extends DatabaseTasks {
protected $pdoDriver = 'sqlite';
public function name() {
return st('SQLite');
}
/**
* Minimum engine version.
*
* @todo: consider upping to 3.6.8 in Drupal 8 to get SAVEPOINT support.
*/
public function minimumVersion() {
return '3.3.7';
}
public function getFormOptions($database) {
$form = parent::getFormOptions($database);
// Remove the options that only apply to client/server style databases.
unset($form['username'], $form['password'], $form['advanced_options']['host'], $form['advanced_options']['port']);
// Make the text more accurate for SQLite.
$form['database']['#title'] = st('Database file');
$form['database']['#description'] = st('The absolute path to the file where @drupal data will be stored. This must be writable by the web server and should exist outside of the web root.', array('@drupal' => drupal_install_profile_distribution_name()));
$default_database = conf_path(FALSE, TRUE) . '/files/.ht.sqlite';
$form['database']['#default_value'] = empty($database['database']) ? $default_database : $database['database'];
return $form;
}
public function validateDatabaseSettings($database) {
// Perform standard validation.
$errors = parent::validateDatabaseSettings($database);
// Verify the database is writable.
$db_directory = new SplFileInfo(dirname($database['database']));
if (!$db_directory->isWritable()) {
$errors[$database['driver'] . '][database'] = st('The directory you specified is not writable by the web server.');
}
return $errors;
}
}
<?php
use Drupal\Database\Database;
use Drupal\Database\Install\TaskException;
/**
* @file
* API functions for installing Drupal.
......@@ -228,6 +231,11 @@ function install_begin_request(&$install_state) {
// Allow command line scripts to override server variables used by Drupal.
require_once DRUPAL_ROOT . '/core/includes/bootstrap.inc';
// Ensure that the class loader is available so that we can leverage classes
// as part of the install routine.
drupal_initialize_classloader();
if (!$install_state['interactive']) {
drupal_override_server_variables($install_state['server']);
}
......@@ -967,7 +975,7 @@ function install_database_errors($database, $settings_file) {
try {
db_run_tasks($driver);
}
catch (DatabaseTaskException $e) {
catch (TaskException $e) {
// These are generic errors, so we do not have any specific key of the
// database connection array to attach them to; therefore, we just put
// them in the error array with standard numeric keys.
......
This diff is collapsed.
......@@ -22,18 +22,12 @@
*/
function _registry_update() {
// The registry serves as a central autoloader for all classes, including
// the database query builders. However, the registry rebuild process
// requires write ability to the database, which means having access to the
// query builders that require the registry in order to be loaded. That
// causes a fatal race condition. Therefore we manually include the
// appropriate query builders for the currently active database before the
// registry rebuild process runs.
// The registry serves as a central autoloader for all non-namespaced classes.
// It is backed by the database, but the database system is autoloaded using
// a PSR-0 class loader. That avoids a fata circular dependency here, since
// the other class loader will be able to load the database for us.
$connection_info = Database::getConnectionInfo();
$driver = $connection_info['default']['driver'];
require_once DRUPAL_ROOT . '/core/includes/database/query.inc';
require_once DRUPAL_ROOT . '/core/includes/database/select.inc';
require_once DRUPAL_ROOT . '/core/includes/database/' . $driver . '/query.inc';
// Get current list of modules and their files.
$modules = db_query("SELECT * FROM {system} WHERE type = 'module'")->fetchAll();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment