public function __construct($database) { global $ss_client; $this->root = dirname(dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)))); require_once DRUPAL_ROOT . '/core/includes/database.inc'; Database::setMultipleConnectionInfo($database); $this->connection = Database::getConnection(); $this->ss_client =& $ss_client; $this->database = $database; $query = db_select('config', 'n')->fields('n')->condition('name', $this->options, 'IN'); $result = $query->execute(); if ($result && is_object($result)) { foreach ($result as $key => $row) { $values = unserialize($row->data); foreach ($values[key($values)] as $value_key => $value) { $this->data_options[$value_key] = $value; } } } if (defined('STACKSIGHT_SETTINGS_IN_DB') && STACKSIGHT_SETTINGS_IN_DB === true) { if (isset($this->data_options['token'])) { $this->ready = true; } } else { $this->ready = true; } define('STACKSIGHT_PHP_SDK_INCLUDE', TRUE); }
/** * Bootstraps settings.php and the Settings singleton. * * @param string $app_root * The app root. * @param string $site_path * The current site path. * @param \Composer\Autoload\ClassLoader $class_loader * The class loader that is used for this request. Passed by reference and * exposed to the local scope of settings.php, so as to allow it to be * decorated with Symfony's ApcClassLoader, for example. * * @see default.settings.php */ public static function initialize($app_root, $site_path, &$class_loader) { // Export these settings.php variables to the global namespace. global $config_directories, $config; $settings = array(); $config = array(); $databases = array(); if (is_readable($app_root . '/' . $site_path . '/settings.php')) { require $app_root . '/' . $site_path . '/settings.php'; } // Initialize Database. Database::setMultipleConnectionInfo($databases); // Initialize Settings. new Settings($settings); }
/** * Bootstraps settings.php and the Settings singleton. * * @param string $site_path * The current site path. */ public static function initialize($site_path) { // Export these settings.php variables to the global namespace. global $base_url, $cookie_domain, $config_directories, $config; $settings = array(); $config = array(); $databases = array(); // Make conf_path() available as local variable in settings.php. if (is_readable(DRUPAL_ROOT . '/' . $site_path . '/settings.php')) { require DRUPAL_ROOT . '/' . $site_path . '/settings.php'; } // Initialize Database. Database::setMultipleConnectionInfo($databases); // Initialize Settings. new Settings($settings); }
/** * Tests the serialization and unserialization of a database connection. */ public function testConnectionSerialization() { $db = Database::getConnection('default', 'default'); try { $serialized = serialize($db); $this->pass('The database connection can be serialized.'); $unserialized = unserialize($serialized); $this->assertTrue(get_class($unserialized) === get_class($db)); } catch (\Exception $e) { $this->fail('The database connection cannot be serialized.'); } // Ensure that all properties on the unserialized object are the same. $db_reflection = new \ReflectionObject($db); $unserialized_reflection = new \ReflectionObject($unserialized); foreach ($db_reflection->getProperties() as $value) { $value->setAccessible(TRUE); // Skip properties that are lazily populated on access. if ($value->getName() === 'driverClasses' || $value->getName() === 'schema') { continue; } $unserialized_property = $unserialized_reflection->getProperty($value->getName()); $unserialized_property->setAccessible(TRUE); // For the PDO object, just check the statement class attribute. if ($value->getName() == 'connection') { $db_statement_class = $unserialized_property->getValue($db)->getAttribute(\PDO::ATTR_STATEMENT_CLASS); $unserialized_statement_class = $unserialized_property->getValue($unserialized)->getAttribute(\PDO::ATTR_STATEMENT_CLASS); // Assert the statement class. $this->assertEqual($unserialized_statement_class[0], $db_statement_class[0]); // Assert the connection argument that is passed into the statement. $this->assertEqual(get_class($unserialized_statement_class[1][0]), get_class($db_statement_class[1][0])); } else { $actual = $unserialized_property->getValue($unserialized); $expected = $value->getValue($db); $this->assertEqual($actual, $expected, vsprintf('Unserialized Connection property %s value %s is equal to expected %s', array(var_export($value->getName(), TRUE), is_object($actual) ? print_r($actual, TRUE) : var_export($actual, TRUE), is_object($expected) ? print_r($expected, TRUE) : var_export($expected, TRUE)))); } } // By using "key", we ensure that its not a key used in the serialized PHP. $not_serialized_properties = ['"connection"', '"connectionOptions"', '"schema"', '"prefixes"', '"prefixReplace"', '"driverClasses"']; foreach ($not_serialized_properties as $property) { $this->assertIdentical(FALSE, strpos($serialized, $property)); } // Serialize the DB connection again, but this time change the connection // information under the hood. $serialized = serialize($db); $db_connection_info = Database::getAllConnectionInfo(); // Use reflection to empty out $databaseInfo. $reflection_class = new \ReflectionClass('Drupal\\Core\\Database\\Database'); $database_info_reflection = $reflection_class->getProperty('databaseInfo'); $database_info_reflection->setAccessible(TRUE); $database_info_reflection->setValue(NULL, []); // Setup a different DB connection which should be picked up after the // unserialize. $db_connection_info['default']['default']['extra'] = 'value'; Database::setMultipleConnectionInfo($db_connection_info); /** @var \Drupal\Core\Database\Connection $db */ $db = unserialize($serialized); $this->assertEqual($db->getConnectionOptions()['extra'], 'value'); }