Skip to content

Commit

Permalink
Merge pull request #3 from Zuluru/master
Browse files Browse the repository at this point in the history
Simplify some try blocks; update salt and baseurl on installation
  • Loading branch information
anuj9196 authored Aug 23, 2018
2 parents 85a7ed0 + a4fbd32 commit 064eb1e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 71 deletions.
7 changes: 1 addition & 6 deletions src/Controller/Component/InstallComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@ class InstallComponent extends Component

public function installationCheck()
{

// check for installation of database configuration

// first check for database connection using default connection
// connection to the database
try {
$connection = ConnectionManager::get('default');
$connected = $connection->connect();
$connection->connect();
} catch (\Exception $connectionError) {
$connected = false;
}

if ($connected === false) {
if (Configure::read('Database.installed') == true) {
// if database connection not established
// and Database.installed is set to TRUE
Expand Down
114 changes: 49 additions & 65 deletions src/Controller/InstallController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,59 +77,60 @@ public function connection() {
}
}

// add some extra bits and pieces that may be helpful
$salt = '';
$salt_chars = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
for ($i = 0; $i < 40; ++ $i) {
$salt .= $salt_chars[mt_rand(0, strlen($salt_chars) - 1)];
}
Configure::write('Installer.Connection.salt', $salt);
Configure::write('Installer.Connection.baseurl', "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}");

try {
/**
* Try to connect the database with the new configuration
*/
ConnectionManager::config('my_default', Configure::read('Installer.Connection'));
$db = ConnectionManager::get('my_default');
$db->connect();

try {
$connected = $db->connect();
} catch (Exception $connectionError) {
$connected = false;
$this->Flash->error(__('Cannot connect to database: {0}', $connectionError->getMessage()));
}
/**
* We will create the true database configuration file with our configuration
*/
$success = true;
$written = [];
foreach (Configure::read('Installer.Files') as $key => $config) {
if ($config['use'] && !file_exists(CONFIG . $config['filename'])) {
$input = new File($config['default']);
$content = $input->read();
foreach (Configure::read('Installer.Connection') as $k => $v) {
$content = str_replace('{default_' . $k . '}', $v, $content);
}

if ($connected) {
/**
* We will create the true database configuration file with our configuration
*/
$success = true;
$written = [];
foreach (Configure::read('Installer.Files') as $key => $config) {
if ($config['use'] && !file_exists(CONFIG . $config['filename'])) {
$input = new File($config['default']);
$content = $input->read();
foreach (Configure::read('Installer.Connection') as $k => $v) {
$content = str_replace('{default_' . $k . '}', $v, $content);
}

$output = new File(CONFIG . $config['filename']);
if ($output->write($content)) {
$written[] = $output;
} else {
$this->Flash->error(__('{0} file cannot be modified', $config['filename']));
$success = false;
break;
}
$output = new File(CONFIG . $config['filename']);
if ($output->write($content)) {
$written[] = $output;
} else {
$this->Flash->error(__('{0} file cannot be modified', $config['filename']));
$success = false;
break;
}
}
}

if ($success) {
$this->Flash->success(__('Connected to the database'));
if ($success) {
$this->Flash->success(__('Connected to the database'));

// import database if import_database is checked
if ($import_database) {
$this->redirect(['action' => 'data']);
} else {
$this->redirect(['action' => 'finish']);
}
// import database if import_database is checked
if ($import_database) {
$this->redirect(['action' => 'data']);
} else {
// Remove any config files that were written successfully, so that we try them again next time.
foreach ($written as $file) {
$file->delete();
}
$this->redirect(['action' => 'finish']);
}
} else {
// Remove any config files that were written successfully, so that we try them again next time.
foreach ($written as $file) {
$file->delete();
}
}
} catch (MissingConnectionException $e) {
Expand All @@ -155,33 +156,16 @@ public function data() {

// connection to the database
try {
$database_connect = $db->connect();
} catch (Exception $connectionError) {
$database_connect = false;
$this->Flash->error(__('Can not connect to database: {0}', $connectionError->getMessage()));
}

$this->set(compact('database_connect'));

if ($this->request->is('post')) {

$db = ConnectionManager::get('default');
$db->connect();
$this->set(['database_connect' => true]);

// connection to the database
try {
$connected = $db->connect();
} catch (Exception $connectionError) {
$connected = false;
$this->Flash->error(__('Can not connect to database: {0}', $connectionError->getMessage()));
}

// connection to the database
if ($connected) {
if ($this->_importSchema($db) && $this->_handleMigrations()) {
$this->Flash->success(__('Database imported'));
$this->redirect(array('action' => 'finish'));
}
if ($this->request->is('post') && $this->_importSchema($db) && $this->_handleMigrations()) {
$this->Flash->success(__('Database imported'));
$this->redirect(array('action' => 'finish'));
}
} catch (Exception $connectionError) {
$this->set(['database_connect' => false]);
$this->Flash->error(__('Can not connect to database: {0}', $connectionError->getMessage()));
}

$this->set($d);
Expand Down

0 comments on commit 064eb1e

Please sign in to comment.