* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBConfig_Loader'); /** * Config string loader: File * * Load xml config string * * @version 1.1.0 * @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 { $realFile = $this->getRealFileFromResouce($file); } // 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.', 1, __CLASS__); } $data = file_get_contents($realFile); // cache config values $expire = array( 'filemtime' => $realFile ); return true; } /** * Resolve Real File Name 4 Config * * * * @oaram string * @return string */ private function getRealFileFromResouce($file) { // config folder $dir = WBParam::get('wb/dir/base'); $dir .= '/' . WBParam::get('wb/dir/config', 'etc'); // specific config file $realFile = $dir . '/' . $file . '.xml'; if (file_exists($realFile)) { return $realFile; } // use distribution's config file $realFile = $dir . '-default/' . $file . '.xml'; if (file_exists($realFile)) { return $realFile; } // use resources' config file $root = explode('/', $file); $root = array_shift($root); $use = intval(WBParam::get('wb/config/resource/' . $root, 0)); if (1 > $use) { return ''; } $res = WBParam::get('wb/dir/resource', array()); foreach ($res as $r) { $realFile = sprintf('%s/etc-default/%s.xml', $r, $file); if (file_exists($realFile)) { return $realFile; } } return ''; } /** * list child nodes * * something like getChildren() * * @param string $file * @return array list of child nodes */ public function ls($file) { $res = array(WBParam::get('wb/dir/base')); $list = array(); // basedir etc folder $dir = $res[0] . '/' . WBParam::get('wb/dir/config', 'etc'); if (!is_dir($dir)) { return; } if (is_dir($dir . '/' . $file)) { $this->listDir($dir, $file, $list); } // use resources' config file $root = explode('/', $file); $root = array_shift($root); $use = intval(WBParam::get('wb/config/resource/' . $root, 0)); if (empty($root)) { $use = 1; } if (0 < $use) { $res = array_merge($res, WBParam::get('wb/dir/resource', array())); } // all resources etc-default folders foreach ($res as $r) { $dir = $r . '/' . WBParam::get('wb/dir/config', 'etc') . '-default'; if (!is_dir($dir)) { return; } if (is_dir($dir . '/' . $file)) { $this->listDir($dir, $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()) { // add only once if (in_array($name, $list)) { continue; } $list[] = $name; continue; } // add files with .xml extension $name = explode('.', $name); if (end($name) != 'xml') { continue; } // remove extension array_pop($name); $name = implode('.', $name); // add only once if (in_array($name, $list)) { continue; } $list[] = $name; } } }