Skip to content

Commit

Permalink
Update installer to not use global databaseConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman committed Jun 21, 2017
1 parent f9b2ba4 commit 5c90d53
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 54 deletions.
52 changes: 35 additions & 17 deletions src/Dev/Install/InstallRequirements.php
Expand Up @@ -16,7 +16,15 @@
*/
class InstallRequirements
{
var $errors, $warnings, $tests;
protected $errors = [];
protected $warnings = [];
protected $tests = [];

/**
* Backup of original ini settings
* @var array
*/
protected $originalIni = [];

/**
* Check the database configuration. These are done one after another
Expand Down Expand Up @@ -172,7 +180,7 @@ public function findWebserver()
*/
public function check()
{
$this->errors = null;
$this->errors = [];
$isApache = $this->isApache();
$isIIS = $this->isIIS();
$webserver = $this->findWebserver();
Expand Down Expand Up @@ -376,7 +384,7 @@ public function check()
'PHP Configuration',
'date.timezone setting and validity',
'date.timezone option in php.ini must be set correctly.',
ini_get('date.timezone')
$this->getOriginalIni('date.timezone')
));

$this->suggestClass('finfo', array(
Expand Down Expand Up @@ -441,24 +449,33 @@ public function check()
"PHP Configuration",
"Memory allocation (PHP config option 'memory_limit')",
"SilverStripe needs a minimum of 32M allocated to PHP, but recommends 64M.",
ini_get("memory_limit")
$this->getOriginalIni("memory_limit")
));

return $this->errors;
}

/**
* Get ini setting
*
* @param string $settingName
* @return mixed
*/
protected function getOriginalIni($settingName)
{
if (isset($this->originalIni[$settingName])) {
return $this->originalIni[$settingName];
}
return ini_get($settingName);
}

public function suggestPHPSetting($settingName, $settingValues, $testDetails)
{
$this->testing($testDetails);

// special case for display_errors, check the original value before
// it was changed at the start of this script.
if ($settingName == 'display_errors') {
global $originalDisplayErrorsValue;
$val = $originalDisplayErrorsValue;
} else {
$val = ini_get($settingName);
}
$val = $this->getOriginalIni($settingName);

if (!in_array($val, $settingValues) && $val != $settingValues) {
$this->warning($testDetails, "$settingName is set to '$val' in php.ini. $testDetails[2]");
Expand All @@ -469,7 +486,7 @@ public function requirePHPSetting($settingName, $settingValues, $testDetails)
{
$this->testing($testDetails);

$val = ini_get($settingName);
$val = $this->getOriginalIni($settingName);
if (!in_array($val, $settingValues) && $val != $settingValues) {
$this->error($testDetails, "$settingName is set to '$val' in php.ini. $testDetails[2]");
}
Expand All @@ -496,8 +513,8 @@ public function suggestFunction($class, $testDetails)
public function requireDateTimezone($testDetails)
{
$this->testing($testDetails);

$result = ini_get('date.timezone') && in_array(ini_get('date.timezone'), timezone_identifiers_list());
$val = $this->getOriginalIni('date.timezone');
$result = $val && in_array($val, timezone_identifiers_list());
if (!$result) {
$this->error($testDetails);
}
Expand All @@ -508,20 +525,21 @@ public function requireMemory($min, $recommended, $testDetails)
$_SESSION['forcemem'] = false;

$mem = $this->getPHPMemory();
$memLimit = $this->getOriginalIni("memory_limit");
if ($mem < (64 * 1024 * 1024)) {
ini_set('memory_limit', '64M');
$mem = $this->getPHPMemory();
$testDetails[3] = ini_get("memory_limit");
$testDetails[3] = $memLimit;
}

$this->testing($testDetails);

if ($mem < $min && $mem > 0) {
$message = $testDetails[2] . " You only have " . ini_get("memory_limit") . " allocated";
$message = $testDetails[2] . " You only have " . $memLimit . " allocated";
$this->error($testDetails, $message);
return false;
} elseif ($mem < $recommended && $mem > 0) {
$message = $testDetails[2] . " You only have " . ini_get("memory_limit") . " allocated";
$message = $testDetails[2] . " You only have " . $memLimit . " allocated";
$this->warning($testDetails, $message);
return false;
} elseif ($mem == 0) {
Expand All @@ -535,7 +553,7 @@ public function requireMemory($min, $recommended, $testDetails)

public function getPHPMemory()
{
$memString = ini_get("memory_limit");
$memString = $this->getOriginalIni("memory_limit");

switch (strtolower(substr($memString, -1))) {
case "k":
Expand Down
30 changes: 10 additions & 20 deletions src/Dev/Install/Installer.php
Expand Up @@ -13,6 +13,10 @@
use SilverStripe\Security\DefaultAdminService;
use SilverStripe\Security\Security;

/**
* SilverStripe CMS SilverStripe\Dev\Install\Installer
* This installer doesn't use any of the fancy SilverStripe stuff in case it's unsupported.
*/
class Installer extends InstallRequirements
{
public function __construct()
Expand Down Expand Up @@ -126,39 +130,25 @@ public function install($config)
global $usingEnv;
if ($usingEnv) {
$this->statusMessage("Setting up 'mysite/_config.php' for use with environment variables...");
$this->writeToFile("mysite/_config.php", <<<PHP
<?php
global \$project;
\$project = 'mysite';
global \$database;
\$database = '{$dbConfig['database']}';
require_once('conf/ConfigureFromEnv.php');
PHP
);
$this->writeToFile("mysite/_config.php", "<?php\n ");
} else {
$this->statusMessage("Setting up 'mysite/_config.php'...");
// Create databaseConfig
$lines = array(
$lines[] = "\t'type' => '$type'"
$lines[] = " 'type' => '$type'"
);
foreach ($dbConfig as $key => $value) {
$lines[] = "\t'{$key}' => '$value'";
$lines[] = " '{$key}' => '$value'";
}
$databaseConfigContent = implode(",\n", $lines);
$this->writeToFile("mysite/_config.php", <<<PHP
<?php
global \$project;
\$project = 'mysite';
use SilverStripe\\ORM\\DB;
global \$databaseConfig;
\$databaseConfig = array(
DB::setConfig([
{$databaseConfigContent}
);
]);
PHP
);
Expand Down
Binary file added src/Dev/Install/client/images/logo.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions src/Dev/Install/config-form.html
Expand Up @@ -6,7 +6,7 @@
<head>
<title>SilverStripe CMS / Framework Installation</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<script type="application/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="application/javascript" src="//code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="application/javascript" src="<?=FRAMEWORK_NAME; ?>/src/Dev/Install/client/js/install.js"></script>
<link rel="stylesheet" type="text/css" href="<?=FRAMEWORK_NAME; ?>/src/Dev/Install/client/styles/install.css">
<link rel="shortcut icon" href="favicon.ico">
Expand All @@ -29,13 +29,13 @@ <h2>CMS / Framework Installation <?php if($silverstripe_version) echo "<small>Ve
<div id="Layout">
<div class="typography">
<form action="install.php" method="post">
<?php if(isset($hasErrorOtherThanDatabase)): ?>
<?php if($hasErrorOtherThanDatabase): ?>
<p class="message error">
You aren't currently able to install the software. Please <a href="#requirements">see below</a> for details.<br>
If you are having problems meeting the requirements, see the <a href="http://doc.silverstripe.org/framework/en/installation/server-requirements" target="_blank">server requirements</a>.
</p>
<?php if (isset($phpIniLocation)): ?>
<p>Your php.ini file is located at <?=$phpIniLocation; ?></p>
<?php if ($phpIniLocation): ?>
<p class="message warning">Your php.ini file is located at <?=$phpIniLocation; ?></p>
<?php endif; ?>
<?php else: ?>
<?php if($alreadyInstalled): ?>
Expand Down Expand Up @@ -252,7 +252,7 @@ <h3 class="sectionHeading">Theme selection <small>Step 4 of 5</small></h3>
</ul>
<h3 class="sectionHeading" id="install">Confirm Install <small>Step 5 of 5</small></h3>

<?php if(isset($hasErrorOtherThanDatabase)): ?>
<?php if($hasErrorOtherThanDatabase): ?>
<p class="error">
You aren't currently able to install the software. Please <a href="#requirements">see above</a> for details.<br>
If you are having problems meeting the requirements, see the <a href="http://doc.silverstripe.org/doku.php?id=server-requirements">server requirements page</a>.
Expand Down
40 changes: 28 additions & 12 deletions src/Dev/Install/install5.php
Expand Up @@ -11,20 +11,23 @@

namespace SilverStripe\Dev\Install;

/**
* SilverStripe CMS SilverStripe\Dev\Install\Installer
* This installer doesn't use any of the fancy SilverStripe stuff in case it's unsupported.
*/
// Back up original ini config
$originalIni = [];
$iniSet = function ($name, $value) use (&$originalIni) {
if (!isset($originalIni[$name])) {
$originalIni[$name] = ini_get($name);
}
ini_set($name, $value);
};

// speed up mysql_connect timeout if the server can't be found
ini_set('mysql.connect_timeout', 5);
$iniSet('mysql.connect_timeout', 5);
// Don't die half was through installation; that does more harm than good
ini_set('max_execution_time', 0);
$iniSet('max_execution_time', 0);

// set display_errors php setting to on to force installer to avoid blank screen of death.
// get the original value so it can be used in PHP requirement checks later in this script.
$originalDisplayErrorsValue = ini_get('display_errors');
ini_set('display_errors', '1');
$iniSet('display_errors', '1');

error_reporting(E_ALL | E_STRICT);

Expand Down Expand Up @@ -201,19 +204,22 @@
}

// Check requirements
$req = new InstallRequirements();
$req = new InstallRequirements($originalIni);
$req->check();

$webserverConfigFile = '';
if ($req->isIIS()) {
$webserverConfigFile = 'web.config';
} else {
$webserverConfigFile = '.htaccess';
}

$hasErrorOtherThanDatabase = false;
$hasOnlyWarnings = false;
$phpIniLocation = php_ini_loaded_file();
if ($req->hasErrors()) {
$hasErrorOtherThanDatabase = true;
$phpIniLocation = php_ini_loaded_file();
} elseif ($req->hasWarnings()) {
$hasOnlyWarnings = true;
}

$dbReq = new InstallRequirements();
Expand All @@ -237,6 +243,17 @@
exit(1);
}

// config-form.html vars (placeholder to prevent deletion)
[
$defaultLocale,
$silverstripe_version,
$locales,
$webserverConfigFile,
$hasErrorOtherThanDatabase,
$hasOnlyWarnings, // If warnings but not errors
$phpIniLocation
];

if ((isset($_REQUEST['go']) || $installFromCli)
&& !$req->hasErrors()
&& !$dbReq->hasErrors()
Expand All @@ -262,4 +279,3 @@
} else {
include(__DIR__ . '/config-form.html');
}

0 comments on commit 5c90d53

Please sign in to comment.