/** * Gets a parser for a given file extension * * @param string $extension * @return object * @throws UnsupportedFormatException If `$extension` is an unsupported file format */ protected function getParser($extension) { $parser = null; $extension = strtolower($extension); foreach (Processor::all() as $fileParser) { if (in_array($extension, $fileParser->getSupportedExtensions())) { $parser = $fileParser; break; } } // If none exist, then throw an exception if ($parser === null) { throw new UnsupportedFormatException(sprintf('Unsupported configuration format "%s"', $extension)); } return $parser; }
/** * Try to get the root credentials from a variety of locations * * @return mixed Array of creds or false on failure **/ private function getRootCredentials() { $secrets = DS . 'etc' . DS . 'hubzero.secrets'; $conf_file = DS . 'root' . DS . '.my.cnf'; $hub_maint = DS . 'etc' . DS . 'mysql' . DS . 'hubmaint.cnf'; $deb_maint = DS . 'etc' . DS . 'mysql' . DS . 'debian.cnf'; if (is_file($secrets) && is_readable($secrets)) { $conf = Processor::instance('ini')->parse($secrets); $user = isset($conf['DEFAULT']['MYSQL-ROOT-USER']) ? $conf['DEFAULT']['MYSQL-ROOT-USER'] : '******'; $pw = isset($conf['DEFAULT']['MYSQL-ROOT']) ? $conf['DEFAULT']['MYSQL-ROOT'] : false; if ($user && $pw) { return array('user' => $user, 'password' => $pw); } } if (is_file($conf_file) && is_readable($conf_file)) { $conf = Processor::instance('ini')->parse($conf_file, true); $user = isset($conf['client']['user']) ? $conf['client']['user'] : false; $pw = isset($conf['client']['password']) ? $conf['client']['password'] : false; if ($user && $pw) { return array('user' => $user, 'password' => $pw); } } if (is_file($hub_maint) && is_readable($hub_maint)) { $conf = Processor::instance('ini')->parse($hub_maint, true); $user = isset($conf['client']['user']) ? $conf['client']['user'] : false; $pw = isset($conf['client']['password']) ? $conf['client']['password'] : false; if ($user && $pw) { return array('user' => $user, 'password' => $pw); } } if (is_file($deb_maint) && is_readable($deb_maint)) { $conf = Processor::instance('ini')->parse($deb_maint, true); $user = isset($conf['client']['user']) ? $conf['client']['user'] : false; $pw = isset($conf['client']['password']) ? $conf['client']['password'] : false; if ($user && $pw) { return array('user' => $user, 'password' => $pw); } } return false; }
/** * Parse a JSON formatted string and convert it into an object. * * If the string is not in JSON format, this method will attempt to parse it as INI format. * * @param string $data JSON formatted string to convert. * @param array $options Options used by the formatter. * @return object Data object. */ public function stringToObject($data, $options = array('processSections' => false)) { if (is_object($data)) { return $data; } if (is_bool($options)) { $options = array('processSections' => $options); } $data = trim($data); $data = trim($data, '"'); if (substr($data, 0, 1) != '{' && substr($data, -1, 1) != '}') { $obj = Base::instance('ini')->stringToObject($data, $options); } else { $obj = json_decode($data); } return $obj; }
/** * Get the processor for a specific format * * @param string $format Format to return the processor for * @return object */ public function processor($format = 'json') { return Processor::instance($format); }