From 55654edeb0f92818b41981d6943c17613b375817 Mon Sep 17 00:00:00 2001 From: Mathieu Ducharme Date: Fri, 13 Mar 2015 13:54:14 -0400 Subject: [PATCH] Add a composer dependency on CLImate. Add a new script to setup database. --- .gitignore | 2 + composer.json | 5 +++ setup_db.php | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 composer.json create mode 100644 setup_db.php diff --git a/.gitignore b/.gitignore index dd1b908..ef8d68a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ project-x/modules/boilerplate/node_modules/ +vendor +composer.lock diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9f7e092 --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "league/climate": "~3.0" + } +} diff --git a/setup_db.php b/setup_db.php new file mode 100644 index 0000000..3de5e2d --- /dev/null +++ b/setup_db.php @@ -0,0 +1,114 @@ +out('Charcoal Boilerplate Local Database Setup'); + +$input = $climate->input('What is your database username? ["root"]'); +$input->defaultTo('root'); +$db_user = $input->prompt(); + +$input = $climate->input('What is your database password? [""]'); +$input->defaultTo(''); +$db_password = $input->prompt(); + +$input = $climate->input('What is your database name?'); +$db_name = $input->prompt(); +if($db_name == '') { + $climate->red()->out('Setup aborted. Please enter a database name'); + die(); +} + +$dsn = 'mysql:dbname='.$db_name; + +try { + $dbh = new PDO($dsn, $db_user, $db_password); + $climate->out(sprintf('Database %s already exists.', $db_name)); + $input = $climate->bold()->confirm('Do you want to use this database for this project?'); + if ($input->confirmed()) { + // ... + } + else { + $climate->red()->out('Setup aborted.'); + die(); + } +} +catch (PDOException $e) { + $climate->out(sprintf('Database %s does not exist.', $db_name)); + $input = $climate->bold()->confirm('Do you want to create it?'); + if ($input->confirmed()) { + try { + $dbh = new PDO("mysql:", $db_user, $db_password); + + $res = $dbh->exec("CREATE DATABASE `$db_name`;"); + if(!$res) { + $climate->red()->out('Setup aborted. Could not create database.'); + die(); + } + else { + $climate->green()->out('Database created successfully.'); + } + + } + catch (PDOException $e) { + $climate->red()->out('Setup aborted. Could not create database.'); + die(); + } + } + else { + $climate->red()->out('Setup aborted.'); + die(); + } +} + +// If here, DB is created. Setup config.json +$climate->out('Setting up the database for the project...'); + +$filename = 'project-x/config/config.json'; +if(!file_exists($filename) || !is_writable($filename)) { + $climate->red()->out('Could not open config.json or file not writeable'); + die(); +} +$config = file_get_contents($filename); +$json = json_decode($config, true); + +$input = $climate->input('What should the database ident be in the config file? ["local"]'); +$input->defaultTo('local'); +$db_ident = $input->prompt(); + +if(!isset($json['databases'])) { + $json['databases'] = []; +} + +$databases = array_keys($json['databases']); +if(in_array($db_ident, $databases)) { + $input = $climate->bold()->confirm('Database already exists in the config file. Overwrite?'); + if (!$input->confirmed()) { + $climate->red()->out('Setup aborted.'); + die(); + } +} + +$json['databases'][$db_ident] = [ + 'database' => $db_name, + 'username' => $db_user, + 'password' => $db_password +]; + +$climate->green()->out('Database added successfully to config file.'); + +$input = $climate->bold()->confirm(sprintf('Do you want to use "%s" as the default database?', $db_name)); +if ($input->confirmed()) { + $json['default_database'] = $db_ident; +} + +$config = json_encode($json, JSON_PRETTY_PRINT); +$res = file_put_contents($filename, $config); +if($res === false) { + $climate->red()->out('Could not write config file. Setup aborted'); +} +else { + $climate->green()->out('Database configuration successful.'); +} +