示例#1
0
 /**
  * Build the PHP array and write it to a file.
  *
  * @param string $database
  * @param string $table
  * @return bool
  * @uses Relationship::getRows()
  * @uses Relationship::buildArray()
  * @uses Helpers::print_array()
  * @uses Relationship::buildTopContent()
  * @uses Relationship::write()
  */
 public function build($database = '', $table = '')
 {
     $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION);
     // input validation
     if (!Helpers::is_string_ne($database)) {
         $this->Log->write('database is invalid', Log::LOG_LEVEL_WARNING, $database);
         return false;
     }
     // get SQL and parameters from the table
     $result = $this->getQuery($database, $table);
     if (!Helpers::is_array_ne($result)) {
         $this->Log->write('could not get SQL for ' . $table . ' in ' . $database, Log::LOG_LEVEL_WARNING);
         return false;
     }
     list($sql, $params) = $result;
     // set the iterator from the SQL
     $set_iterator = $this->setIterator($sql, $params);
     if (!$set_iterator) {
         $this->Log->write('could not set iterator with SQL and params', Log::LOG_LEVEL_WARNING, $result);
     }
     // generate the results
     $built = $this->generate();
     if (!$built) {
         $this->Log->write('could not build an array with rows', Log::LOG_LEVEL_WARNING);
         return false;
     }
     // get PHP array as string
     $this->php = Helpers::print_array($this->relationship_array, 1, false, false, true);
     if (!Helpers::is_string_ne($this->php)) {
         $this->Log->write('could not convert PHP array to string array', Log::LOG_LEVEL_WARNING, $this->relationship_array);
         return false;
     }
     // write the declaration
     $this->php = '$GLOBALS[\'table_relationships\'] = array(' . PHP_EOL . $this->php . ');' . PHP_EOL;
     // create the top PHP content for the file
     $size = $this->buildTopContent(__CLASS__);
     if (!Helpers::is_valid_int($size) || $size < 1) {
         $this->Log->write('could not write top content', Log::LOG_LEVEL_WARNING);
         return false;
     }
     // write the PHP string to the file
     $size = $this->write();
     if (!Helpers::is_valid_int($size) || $size < 1) {
         $this->Log->write('could not write php', Log::LOG_LEVEL_WARNING, $this->php);
         return false;
     }
     $this->Log->write('successfully created and wrote file', Log::LOG_LEVEL_USER, $this->file_path);
     require_once $this->file_path;
     return true;
 }