/** * 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; }
/** * ParseSql constructor. * * @param array $params */ public function __construct($params = array()) { $this->log_file = 'parse_sql_' . date('Y-m-d') . '.log'; // handle parameters if (Helpers::is_array_ne($params)) { // get directory if (array_key_exists('dir', $params) && Helpers::is_string_ne($params['dir'])) { $dir = realpath($params['dir']); if (Helpers::is_string_ne($dir)) { $this->dir = $dir . DIRECTORY_SEPARATOR; } } // get file name (and maybe directory name) if (array_key_exists('file', $params) && Helpers::is_string_ne($params['file'])) { if (file_exists($this->dir . $params['file'])) { // directory has been set properly and file exists $this->file = $params['file']; } elseif (file_exists($params['file'])) { // file is a full path, so split it to directory and file name $this->dir = dirname($params['file']) . DIRECTORY_SEPARATOR; $this->file = basename($params['file']); } elseif (file_exists(__DIR__ . DIRECTORY_SEPARATOR . $params['file'])) { // file exists in this directory, so set directory and file name $this->dir = __DIR__ . DIRECTORY_SEPARATOR; $this->file = $params['file']; } } if (array_key_exists('log_level', $params) && Helpers::is_valid_int($params['log_level'])) { $this->log_level = $params['log_level']; } if (array_key_exists('log_directory', $params) && Helpers::is_string_ne($params['log_directory'])) { if (is_dir($params['log_directory'])) { $this->log_directory = $params['log_directory']; } else { $this->log_directory = LOG_DIR; } } if (array_key_exists('log_file', $params) && Helpers::is_string_ne($params['log_file'])) { $this->log_file = $params['log_file']; } } // set up Log $this->Log = new Log(['file' => $this->log_file, 'log_level' => $this->log_level, 'log_directory' => $this->log_directory]); // verify log file was set properly $log_file = $this->Log->file(); if ($log_file !== $this->log_file) { $this->Log->write('could not set file properly', Log::LOG_LEVEL_WARNING); } $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); }