/** * Check if a file exists in the include path * And if it does, return the absolute path. * @method realPath * @static * @param {string} $filename * Name of the file to look for * @param {boolean} $ignoreCache=false * Defaults to false. If true, then this function ignores * the cached value, if any, and attempts to search * for the file. It will cache the new value. * @return {string|false} * The absolute path if file exists, false if it does not */ static function realPath($filename, $ignoreCache = false) { $filename = str_replace('/', DS, $filename); if (!$ignoreCache) { // Try the extended cache mechanism, if any $result = Q::event('Q/realPath', array(), 'before'); if (isset($result)) { return $result; } // Try the native cache mechanism $result = Q_Cache::get("Q::realPath\t{$filename}"); if (isset($result)) { return $result; } } // Do a search for the file $paths = explode(PS, get_include_path()); array_unshift($paths, ""); $result = false; foreach ($paths as $path) { if (substr($path, -1) == DS) { $fullpath = $path . $filename; } else { $fullpath = ($path ? $path . DS : "") . $filename; } // Note: the following call to the OS may take some time: $realpath = realpath($fullpath); if ($realpath && file_exists($realpath)) { $result = $realpath; break; } } // Notify the cache mechanism, if any Q_Cache::set("Q::realPath\t{$filename}", $result); /** * @event Q/realPath {after} * @param {string} $result */ Q::event('Q/realPath', compact('result'), 'after'); return $result; }
/** * Saves parameters to a file * @method save * @param {string} $filename Name of file to save to. If tree was loaded, you can leave this blank to update that file. * @param {array} [$array_path=array()] Array of keys identifying the path of the config subtree to save * @return {boolean} Returns true if saved, otherwise false; **/ function save($filename = null, $array_path = array(), $prefix_path = null) { if (empty($filename) and !empty($this->filename)) { $filename = $this->filename; } if (!($filename2 = Q::realPath($filename))) { $filename2 = $filename; } if (empty($array_path)) { $array_path = array(); $toSave = $this->parameters; } else { $array_path[] = null; $toSave = call_user_func_array(array($this, 'get'), $array_path); } if (is_null($prefix_path)) { $prefix_path = $array_path; } $prefix_path = array_reverse($prefix_path); foreach ($prefix_path as $ap) { if ($ap) { $toSave = array($ap => $toSave); } } $mask = umask(Q_Config::get('Q', 'internal', 'umask', 00)); $success = file_put_contents($filename2, !empty($toSave) ? Q::json_encode($toSave, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) : '{}', LOCK_EX); clearstatcache(true, $filename2); umask($mask); if ($success) { self::$cache[$filename] = $toSave; Q_Cache::set("Q_Tree\t{$filename}", $toSave); // no need to check result - on failure Q_Cache is disabled } return $success; }
/** * Get contents of config file * Config file is searched in APP_DIR/files forder. If config server url is defined * the filename is searched on config server * @method getFromServer * @static * @param {string} $filename The name of the config file. If config server is defined, file is got from there * @return {array} The loaded tree */ static function getFromServer($filename) { if ($cs = self::serverInfo()) { // check Q_Cache and if set - use it // update class cache as it is not set $arr = Q_Cache::get("Q_Config\t{$filename}"); if (isset($arr)) { $tree = new Q_Tree(); $tree->merge($arr); return $tree->getAll(); } // request config server if (!empty($cs['url'])) { if (!empty($cs['internal'])) { // query "internal" Qbix server $return = Q_Utils::queryInternal('Q/Config', array('Q/method' => 'get', 'filename' => $filename), $cs['url']); } else { // query "external" Qbix server $return = Q_Utils::queryExternal('Q/Config', array('Q/method' => 'get', 'filename' => $filename), $cs['url']); } Q_Cache::set("Q_Config\t{$filename}", $return); return $return; } } // take local file, return empty tree if file does not exists $tree = new Q_Tree(); if (defined('APP_DIR')) { $filename = APP_DIR . DS . 'files' . DS . $filename; } else { throw new Q_Exception("'APP_DIR' is not defined"); } $tree->load($filename); return $tree->getAll(); }