Skip to content

Instantly share code, notes, and snippets.

@sminnee
Last active February 22, 2017 04:51
Show Gist options
  • Select an option

  • Save sminnee/21bdf1b70c97b1b8a165ce6c05cac7e4 to your computer and use it in GitHub Desktop.

Select an option

Save sminnee/21bdf1b70c97b1b8a165ce6c05cac7e4 to your computer and use it in GitHub Desktop.
diff --git a/cli-script.php b/cli-script.php
index 7880e90..44c9327 100755
--- a/cli-script.php
+++ b/cli-script.php
@@ -89,41 +89,39 @@ else {
*/
require_once("Core/Core.php");
-global $databaseConfig;
-
// We don't have a session in cli-script, but this prevents errors
$_SESSION = null;
// Connect to database
-if(!isset($databaseConfig) || !isset($databaseConfig['database']) || !$databaseConfig['database']) {
- echo "\nPlease configure your database connection details. You can do this by creating a file
-called .env in " . BASE_PATH;
- echo <<<ENVCONTENT
+// TODO figure out better way of determining database completeness
+// if(!isset($databaseConfig) || !isset($databaseConfig['database']) || !$databaseConfig['database']) {
+// echo "\nPlease configure your database connection details. You can do this by creating a file
+// called .env in " . BASE_PATH;
+// echo <<<ENVCONTENT
-Put the following content into this file:
---------------------------------------------------
+// Put the following content into this file:
+// --------------------------------------------------
-# Change this from 'dev' to 'live' for a production environment.
-SS_ENVIRONMENT_TYPE="dev"
+// # Change this from 'dev' to 'live' for a production environment.
+// SS_ENVIRONMENT_TYPE="dev"
-/* This defines a default database user */
-SS_DATABASE_SERVER="localhost"
-SS_DATABASE_USERNAME="<user>"
-SS_DATABASE_PASSWORD="<password>"
-SS_DATABASE_NAME="<database>"
---------------------------------------------------
+// /* This defines a default database user */
+// SS_DATABASE_SERVER="localhost"
+// SS_DATABASE_USERNAME="<user>"
+// SS_DATABASE_PASSWORD="<password>"
+// SS_DATABASE_NAME="<database>"
+// --------------------------------------------------
-Once you have done that, run 'composer install' or './framework/sake dev/build' to create
-an empty database.
+// Once you have done that, run 'composer install' or './framework/sake dev/build' to create
+// an empty database.
-For more information, please read this page in our docs:
-http://docs.silverstripe.org/en/getting_started/environment_management/
+// For more information, please read this page in our docs:
+// http://docs.silverstripe.org/en/getting_started/environment_management/
-ENVCONTENT;
- exit(1);
-}
-DB::connect($databaseConfig);
+// ENVCONTENT;
+// exit(1);
+// }
// Get the request URL from the querystring arguments
diff --git a/main.php b/main.php
index daa214c..76bfd37 100644
--- a/main.php
+++ b/main.php
@@ -149,10 +149,6 @@ $chain
// Load in core
require_once('Core/Core.php');
- // Connect to database
- global $databaseConfig;
- if ($databaseConfig) DB::connect($databaseConfig);
-
// Check if a token is requesting a redirect
if (!$reloadToken) return;
@@ -180,25 +176,24 @@ $chain
})
->execute();
-global $databaseConfig;
-
// Redirect to the installer if no database is selected
-if(!isset($databaseConfig) || !isset($databaseConfig['database']) || !$databaseConfig['database']) {
- if(!file_exists(BASE_PATH . '/install.php')) {
- header($_SERVER['SERVER_PROTOCOL'] . " 500 Server Error");
- die('SilverStripe Framework requires a $databaseConfig defined.');
- }
- $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
- $s = (isset($_SERVER['SSL']) || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off')) ? 's' : '';
- $installURL = "http$s://" . $host . BASE_URL . '/install.php';
-
- // The above dirname() will equate to "\" on Windows when installing directly from http://localhost (not using
- // a sub-directory), this really messes things up in some browsers. Let's get rid of the backslashes
- $installURL = str_replace('\\', '', $installURL);
-
- header("Location: $installURL");
- die();
-}
+// TO DO : figure out better way determing db config completeness
+// if(!isset($databaseConfig) || !isset($databaseConfig['database']) || !$databaseConfig['database']) {
+// if(!file_exists(BASE_PATH . '/install.php')) {
+// header($_SERVER['SERVER_PROTOCOL'] . " 500 Server Error");
+// die('SilverStripe Framework requires a $databaseConfig defined.');
+// }
+// $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
+// $s = (isset($_SERVER['SSL']) || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off')) ? 's' : '';
+// $installURL = "http$s://" . $host . BASE_URL . '/install.php';
+
+// // The above dirname() will equate to "\" on Windows when installing directly from http://localhost (not using
+// // a sub-directory), this really messes things up in some browsers. Let's get rid of the backslashes
+// $installURL = str_replace('\\', '', $installURL);
+
+// header("Location: $installURL");
+// die();
+// }
// Direct away - this is the "main" function, that hands control to the appropriate controller
DataModel::set_inst(new DataModel());
diff --git a/src/Core/Injector/Injector.php b/src/Core/Injector/Injector.php
index 60fcbbf..006f723 100644
--- a/src/Core/Injector/Injector.php
+++ b/src/Core/Injector/Injector.php
@@ -527,7 +527,7 @@ class Injector
// Evaluate constants surrounded by back ticks
if (preg_match('/^`(?<name>[^`]+)`$/', $value, $matches)) {
- $value = defined($matches['name']) ? constant($matches['name']) : null;
+ $value = defined($matches['name']) ? constant($matches['name']) : getenv($matches['name']);
}
return $value;
diff --git a/src/ORM/DB.php b/src/ORM/DB.php
index fc1b759..b352c54 100644
--- a/src/ORM/DB.php
+++ b/src/ORM/DB.php
@@ -62,7 +62,7 @@ class DB
*/
public static function set_conn(Database $connection, $name = 'default')
{
- self::$connections[$name] = $connection;
+ throw new \LogicException("DB::set_conn() no longer works. Configure your database vai Injector config");
}
/**
@@ -74,10 +74,8 @@ class DB
*/
public static function get_conn($name = 'default')
{
- if (isset(self::$connections[$name])) {
- return self::$connections[$name];
- }
- return null;
+ $service = Database::class . ($name === 'default' ? '' : ".$name");
+ return Injector::inst()->get($service);
}
/**
@@ -259,28 +257,13 @@ class DB
*/
public static function connect($databaseConfig, $label = 'default')
{
+ throw new \LogicException("DB::connect() no longer works. Register a SilverStripe\ORM\Connect\Database service instead");
// This is used by the "testsession" module to test up a test session using an alternative name
+ // TO DO : Replace this
if ($name = self::get_alternative_database_name()) {
$databaseConfig['database'] = $name;
}
-
- if (!isset($databaseConfig['type']) || empty($databaseConfig['type'])) {
- user_error("DB::connect: Not passed a valid database config", E_USER_ERROR);
- }
-
- self::$connection_attempted = true;
-
- $dbClass = $databaseConfig['type'];
-
- // Using Injector->create allows us to use registered configurations
- // which may or may not map to explicit objects
- $conn = Injector::inst()->create($dbClass);
- $conn->connect($databaseConfig);
-
- self::set_conn($conn, $label);
-
- return $conn;
}
/**
@@ -290,7 +273,7 @@ class DB
*/
public static function connection_attempted()
{
- return self::$connection_attempted;
+ throw new \LogicException("DB::connection_attempted() no longer works.");
}
/**
diff --git a/src/conf/ConfigureFromEnv.php b/src/conf/ConfigureFromEnv.php
index 71e140a..55e6c67 100644
--- a/src/conf/ConfigureFromEnv.php
+++ b/src/conf/ConfigureFromEnv.php
@@ -59,66 +59,6 @@ if ($envType = getenv('SS_ENVIRONMENT_TYPE')) {
Director::config()->environment_type = $envType;
}
-global $database;
-
-// No database provided
-if (!isset($database) || !$database) {
- if (!($database = getenv('SS_DATABASE_NAME')) && $chooseName = getenv('SS_DATABASE_CHOOSE_NAME')) {
- $loopCount = (int)$chooseName;
- $databaseDir = BASE_PATH;
- for ($i=0; $i<$loopCount-1; $i++) {
- $databaseDir = dirname($databaseDir);
- }
- $database = getenv('SS_DATABASE_PREFIX') ?: 'SS_';
- $database .= basename($databaseDir);
- $database = str_replace('.', '', $database);
- }
-}
-
-if ($dbUser = getenv('SS_DATABASE_USERNAME')) {
- global $databaseConfig;
-
- // Checks if the database global is defined (if present, wraps with prefix and suffix)
- $databaseNameWrapper = function ($name) {
- if (!$name) {
- return '';
- } else {
- return (getenv('SS_DATABASE_PREFIX') ?: '')
- . $name
- . (getenv('SS_DATABASE_SUFFIX') ?: '');
- }
- };
-
- /** @skipUpgrade */
- $databaseConfig = array(
- "type" => getenv('SS_DATABASE_CLASS') ?: 'MySQLDatabase',
- "server" => getenv('SS_DATABASE_SERVER') ?: 'localhost',
- "username" => $dbUser,
- "password" => getenv('SS_DATABASE_PASSWORD'),
- "database" => $databaseNameWrapper($database),
- );
-
- // Set the port if called for
- if ($dbPort = getenv('SS_DATABASE_PORT')) {
- $databaseConfig['port'] = $dbPort;
- }
-
- // Set the timezone if called for
- if ($dbTZ = getenv('SS_DATABASE_TIMEZONE')) {
- $databaseConfig['timezone'] = $dbTZ;
- }
-
- // For schema enabled drivers:
- if ($dbSchema = getenv('SS_DATABASE_SCHEMA')) {
- $databaseConfig["schema"] = $dbSchema;
- }
-
- // For SQlite3 memory databases (mainly for testing purposes)
- if ($dbMemory = getenv('SS_DATABASE_MEMORY')) {
- $databaseConfig["memory"] = $dbMemory;
- }
-}
-
if ($sendAllEmailsTo = getenv('SS_SEND_ALL_EMAILS_TO')) {
Email::config()->send_all_emails_to = $sendAllEmailsTo;
}
diff --git a/tests/bootstrap/phpunit.php b/tests/bootstrap/phpunit.php
index 9e176fc..d90eb66 100644
--- a/tests/bootstrap/phpunit.php
+++ b/tests/bootstrap/phpunit.php
@@ -10,9 +10,6 @@ use SilverStripe\ORM\DB;
require_once __DIR__ . '/../../src/Core/Core.php';
require_once __DIR__ . '/../php/Control/FakeController.php';
-global $databaseConfig;
-DB::connect($databaseConfig);
-
// Now set a fake REQUEST_URI
$_SERVER['REQUEST_URI'] = BASE_URL;
---
Name: mysite
---
SilverStripe\Core\Injector\Injector:
SilverStripe\ORM\Connect\Database:
class: 'SilverStripe\ORM\Connect\MySQLDatabase'
properties:
connector: %$PDOConnector
schemaManager: %$MySQLSchemaManager
queryBuilder: %$MySQLQueryBuilder
calls:
-
- "connect"
-
- server: `SS_DATABASE_SERVER`
username: `SS_DATABASE_USERNAME`
password: `SS_DATABASE_PASSWORD`
database: SS_ss4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment