* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBConfig_Loader'); /** * Config string loader: File * * Load xml config string * * @version 0.2.1 * @package WB * @subpackage base */ class WBConfig_Loader_File extends WBConfig_Loader { /** * load configuration from file * * Well, load it from file or cache or buffer * * @param string $file * @param string $data actual data loaded * @param array $expire expire hint for cache * @param bool $try try to load, don't throw an exception * @return bool true on success * @throws WBException_File */ public function load($file, &$data, &$expire, $try = false) { // load from "root" or config folder $dir = WBParam::get('wb/dir/base'); if (!empty($file) && $file[0] == '/') { // load any file $realFile = $dir . $file . '.xml'; } else { // load config file $dir .= '/' . WBParam::get('wb/dir/config', 'etc'); $realFile = $dir . '/' . $file . '.xml'; // use alternative config file if (!file_exists($realFile)) { $realFile = $dir . '-default/' . $file . '.xml'; } } // file must exist if (!file_exists($realFile)) { if ($try) { return false; } WBClass::load('WBException_File'); throw new WBException_File('Config file "' . $file . '.xml" not found. Neigher in "' . $dir . '" nor in "' . $dir . '-default"', 1, __CLASS__); } $data = file_get_contents($realFile); // cache config values $expire = array( 'filemtime' => $realFile ); return true; } /** * list child nodes * * something like getChildren() * * @param string $file * @return array list of child nodes */ public function ls($file) { // load from "root" or config folder $dir = WBParam::get('wb/dir/base'); $list = array(); // anything in base dir if (!empty($file) && $file[0] == '/') { $real = $dir . $file; if (is_dir($real)) { $this->listDir($real, $file, $list); } return $list; } // load config file $dir .= '/' . WBParam::get('wb/dir/config', 'etc'); $real = $dir . '/'; if (is_dir($real . $file)) { $this->listDir($real, $file, $list); } $dir .= '-default'; $real = $dir . '/'; if (is_dir($real . $file)) { $this->listDir($real, $file, $list); } $list = array_unique($list); sort($list); return $list; } /** * list folder * * List all XML-files and sub directories and append them to list. * Strip ".xml" from file-names * * @param string $dir (parent folder) * @param string $file folder to list * @param array $list */ private function listDir($dir, $file, &$list) { // list files and folders $dh = new DirectoryIterator($dir . $file); foreach ($dh as $i) { $name = $i->getFilename(); if ($i->isDot() || '.' == $name[0]) { continue; } // add all folders if ($i->isDir()) { $list[] = $name; continue; } // add files with .xml extension $name = explode('.', $name); if (end($name) != 'xml') { continue; } array_pop($name); $list[] = implode('.', $name); } } } ?>