/**
  * Determines what action should be performed and takes that action.
  *
  * @uses MPM_DB_PATH
  * @uses MpmDbHelper::test()
  * @uses MpmListHelper::getFiles()
  * @uses MpmCommandLineWriter::getInstance()
  * @uses MpmCommandLineWriter::addText()
  * @uses MpmCommandLineWriter::write()
  * @uses MpmDbHelper::getMethod()
  * @uses MpmUpController::displayHelp()
  *
  * @return void
  */
 public function doAction()
 {
     // make sure system is init'ed
     MpmDbHelper::test();
     // get date stamp for use in generating filename
     $date_stamp = date('Y_m_d_H_i_s');
     $filename = $date_stamp . '.php';
     $vars = array('timestamp' => $date_stamp);
     //$classname = 'Migration_' . $date_stamp;
     // get list of files
     $files = MpmListHelper::getFiles();
     // if filename is taken, throw error
     if (in_array($filename, $files)) {
         $obj = MpmCommandLineWriter::getInstance();
         $obj->addText('Unable to obtain a unique filename for your migration.  Please try again in a few seconds.');
         $obj->write();
     }
     // create file
     if (MpmDbHelper::getMethod() == MPM_METHOD_PDO) {
         $file = MpmTemplateHelper::getTemplate('pdo_migration.txt', $vars);
     } else {
         $file = MpmTemplateHelper::getTemplate('mysqli_migration.txt', $vars);
     }
     // write the file
     $fp = fopen(MPM_DB_PATH . $filename, "w");
     if ($fp == false) {
         $obj = MpmCommandLineWriter::getInstance();
         $obj->addText('Unable to write new migration file.');
         $obj->write();
     }
     $success = fwrite($fp, $file);
     if ($success == false) {
         $obj = MpmCommandLineWriter::getInstance();
         $obj->addText('Unable to write new migration file.');
         $obj->write();
     }
     fclose($fp);
     // display success message
     $obj = MpmCommandLineWriter::getInstance();
     $obj->addText('New migration created: file /db/' . $filename);
     $obj->write();
 }
 /**
  * Create an empty stub for the schema.php file
  */
 protected function add()
 {
     // make sure the schema file doesn't exist
     if (file_exists(MPM_DB_PATH . 'schema.php') || file_exists(MPM_DB_PATH . 'test_data.php')) {
         $this->clw->addText('The schema and/or test data files already exist.  Delete them first if you want to use this option.');
         $this->clw->write();
         exit;
     }
     $file = MpmTemplateHelper::getTemplate('schema.txt');
     $test_data_file = MpmTemplateHelper::getTemplate('test_data.txt');
     $fp = fopen(MPM_DB_PATH . 'schema.php', "w");
     if ($fp == false) {
         echo "\nUnable to write to file.  Initialization failed!\n\n";
         exit;
     }
     $success = fwrite($fp, $file);
     if ($success == false) {
         echo "\nUnable to write to file.  Initialization failed!\n\n";
         exit;
     }
     fclose($fp);
     $fp = fopen(MPM_DB_PATH . 'test_data.php', "w");
     if ($fp == false) {
         echo "\nUnable to write to file.  Initialization failed!\n\n";
         exit;
     }
     $success = fwrite($fp, $test_data_file);
     if ($success == false) {
         echo "\nUnable to write to file.  Initialization failed!\n\n";
         exit;
     }
     fclose($fp);
     $this->clw->addText('File ' . MPM_DB_PATH . 'schema.php has been created.');
     $this->clw->addText('File ' . MPM_DB_PATH . 'test_data.php has been created.');
     $this->clw->write();
 }
 /**
  * Returns the requested template file as an array, each item in that array is a single line from the file.
  *
  * @uses MpmTemplateHelpger::getTemplate()
  *
  * @param string $file the filename of the template being requested
  * @param array	 $vars an array of key value pairs that correspond to variables that should be replaced in the template file
  *
  * @return array
  */
 public static function getTemplateAsArrayOfLines($file, $vars = array())
 {
     $contents = MpmTemplateHelper::getTemplate($file, $vars);
     $arr = explode("\n", $contents);
     return $arr;
 }
 /**
  * Determines what action should be performed and takes that action.
  *
  * @uses MpmDbHelper::test()
  * @uses MpmCommandLineWriter::getInstance()
  * @uses MpmCommandLineWriter::addText()
  * @uses MpmCommandLineWriter::write()
  * @uses MpmCommandLineWriter::writeHeader()
  * @uses MpmCommandLineWriter::writeFooter()
  * @uses MpmBuildController::build()
  * @uses MPM_DB_PATH
  *
  * @return void
  */
 public function doAction()
 {
     // make sure system is init'ed
     MpmDbHelper::test();
     $clw = MpmCommandLineWriter::getInstance();
     $forced = false;
     $with_data = false;
     // are we adding a schema file?
     if (isset($this->arguments[0]) && $this->arguments[0] == 'add') {
         // make sure the schema file doesn't exist
         if (file_exists(MPM_DB_PATH . 'schema.php') || file_exists(MPM_DB_PATH . 'test_data.php')) {
             $clw->addText('The schema and/or test data files already exist.  Delete them first if you want to use this option.');
             $clw->write();
             exit;
         }
         $file = MpmTemplateHelper::getTemplate('schema.txt');
         $test_data_file = MpmTemplateHelper::getTemplate('test_data.txt');
         $fp = fopen(MPM_DB_PATH . 'schema.php', "w");
         if ($fp == false) {
             echo "\nUnable to write to file.  Initialization failed!\n\n";
             exit;
         }
         $success = fwrite($fp, $file);
         if ($success == false) {
             echo "\nUnable to write to file.  Initialization failed!\n\n";
             exit;
         }
         fclose($fp);
         $fp = fopen(MPM_DB_PATH . 'test_data.php', "w");
         if ($fp == false) {
             echo "\nUnable to write to file.  Initialization failed!\n\n";
             exit;
         }
         $success = fwrite($fp, $test_data_file);
         if ($success == false) {
             echo "\nUnable to write to file.  Initialization failed!\n\n";
             exit;
         }
         fclose($fp);
         $clw->addText('File ' . MPM_DB_PATH . 'schema.php has been created.');
         $clw->addText('File ' . MPM_DB_PATH . 'test_data.php has been created.');
         $clw->write();
         exit;
     } else {
         if (isset($this->arguments[0]) && $this->arguments[0] == 'with_data') {
             $with_data = true;
         } else {
             if (isset($this->arguments[0]) && $this->arguments[0] == '--force') {
                 $forced = true;
             }
         }
     }
     // make sure the schema file exists
     if (!file_exists(MPM_DB_PATH . 'schema.php')) {
         $clw->addText('The schema file does not exist.  Run this command with the "add" argument to create one (only a stub).');
         $clw->write();
         exit;
     }
     // make sure the test data file exists
     if ($with_data == true && !file_exists(MPM_DB_PATH . 'test_data.php')) {
         $clw->addText('The test data file does not exist.  Run this command with the "add" argument to create one (only a stub).');
         $clw->write();
         exit;
     }
     $clw->writeHeader();
     if (!$forced) {
         echo "\nWARNING:  IF YOU CONTINUE, ALL TABLES IN YOUR DATABASE WILL BE ERASED!";
         echo "\nDO YOU WANT TO CONTINUE? [y/N] ";
         $answer = fgets(STDIN);
         $answer = trim($answer);
         $answer = strtolower($answer);
         if (empty($answer) || substr($answer, 0, 1) == 'n') {
             echo "\nABORTED!\n\n";
             $clw->writeFooter();
             exit;
         }
     }
     echo "\n";
     $this->build($with_data);
     $clw->writeFooter();
     exit;
 }
 /**
  * Adds the header to the $text property.
  *
  * @return void
  */
 private function addHeader()
 {
     $blank = (object) array();
     $blank->text = ' ';
     $blank->indent = 0;
     array_unshift($this->text, $blank);
     $max_line_len = $this->maxWidth - 12;
     $bar = '';
     for ($i = 0; $i < $max_line_len; $i++) {
         $bar .= "*";
     }
     $bar .= ' v' . MPM_VERSION . ' ***';
     $bar_obj = (object) array();
     $bar_obj->text = $bar;
     $bar_obj->indent = 0;
     array_unshift($this->text, $bar_obj);
     $lines = MpmTemplateHelper::getTemplateAsArrayOfLines('header.txt');
     $start = count($lines) - 1;
     for ($i = $start; $i >= 0; $i--) {
         $line = $lines[$i];
         $a = $this->maxWidth - strlen($line);
         $indent = floor($a / 2);
         $txt = (object) array();
         $txt->text = $line;
         $txt->indent = 0;
         array_unshift($this->text, $txt);
     }
     return;
 }