/** * Fetch module name according to URI * @return type * @throws \zinux\kernel\exceptions\notFoundException */ protected function FetchModuleName() { __LOADING_CACHE: # all folders in ../modules folders considered a module folder # define module root dir defined('MODULE_ROOT') || define('MODULE_ROOT', \zinux\kernel\utilities\fileSystem::resolve_path(ZINUX_ROOT . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'modules')); # define default module $this->module = new \zinux\kernel\mvc\module("default", MODULE_ROOT . "defaultModule"); $module_dir = dirname($this->module->GetPath()); # module collection instance $mc = new \stdClass(); # fail-safe for module dir existance if (!file_exists($module_dir)) { die("Couldn't find modules directory"); } # fetch all modules directory paths$name $modules = glob($module_dir . "/*", GLOB_ONLYDIR); # fail-safe for module lackness if (!count($modules)) { die("No module found."); } # checking if modules has been cached or not # don't use xCache it will overload the session file $fc = new \zinux\kernel\caching\fileCache(__CLASS__); # checking if module cached if ($fc->isCached(__FUNCTION__)) { # catch maintaining optz. $mc = $fc->fetch(__FUNCTION__); # if the module directory has been updated if ($mc->modified_time != filemtime($module_dir)) { # update the catch data goto __LOAD_MODULES; } goto __FETCHING_MODULES; } # loading modules directories from hard drive __LOAD_MODULES: # modules collections $mc->modules = array(); # save directory modified time $mc->modified_time = filemtime($module_dir); # fetching namespace prefixes for modules $namespace_prex = \trim(\str_replace(array(PROJECT_ROOT, \DIRECTORY_SEPARATOR), array("", "\\"), MODULE_ROOT), "\\"); # foreach module found foreach ($modules as $module) { # add current module into our module collections # except default module every other module # has namespace with the module's name prefix $m = new \zinux\kernel\mvc\module(basename($module), $module, $namespace_prex); $mc->modules[] = $m; } # now module collection is ready # caching module collections data $fc->save(__FUNCTION__, $mc); # fetching related modules accoring to requested URI __FETCHING_MODULES: # if not parts provided picking up default module if (!count($this->_parts)) { return; } foreach ($mc->modules as $module) { # checking if first part of URI matches with any modules if (strtolower($module->name) == strtolower($this->_parts[0])) { # this is should NEVER ever MATCH TRUE condition if (!file_exists($module->GetPath())) { # delete cached data $fc->deleteAll(); # throw exception throw new \zinux\kernel\exceptions\notFoundException("Wired! `{$module->module_name}` not found at `{$module->module_path}`"); } # saving target modules $this->module = $module; # removing modules name from URI parts array_shift($this->_parts); break; } } }
/** * Get all loaded config * @return array * @throws \zinux\kernel\exceptions\invalidOperationException if no config ever loaded */ public static function GetAll() { # don't use xCache it will overload the session file $xf = new \zinux\kernel\caching\fileCache(__CLASS__); if (!self::HasLoaded()) { throw new \zinux\kernel\exceptions\invalidOperationException("The config file has not been loaded"); } $conf = array(); foreach (self::$load_cache_sig as $cache_sig) { if ($xf->isCached($cache_sig)) { $c = $xf->fetch($cache_sig); $conf = array_merge($conf, $c->config); } } return $conf; }
/** * resolve the path with insensitive behavioral pattern * @param string $path target path to resolve * @param boolean $convert_to_real_path should the function convert the ultimate to realpath? * @param boolean $cache_built_path should enable caching during the building path? * @return string|null is path exists the resolved path will be returned otherwise null */ public static function resolve_path($path, $convert_to_real_path = 1, $cache_built_path = 0) { # check if string is valid if (!strlen($path)) { return FALSE; } # a primary check if (file_exists($path)) { return $path; } # open the cache file require_once ZINUX_ROOT . 'kernel/caching/xCache.php'; # we will use fileCache to cache # don't use xCache it will overload the session file $fc = new \zinux\kernel\caching\fileCache(__CLASS__); # create a base cache signiture $base_cache_sig = __FUNCTION__ . "@"; # create the cache sig $cache_sig = "{$base_cache_sig}" . strtolower($path); # check cache file and validate it if ($fc->isCached($cache_sig) && file_exists($fc->fetch($cache_sig))) { # it was a HIT! return $fc->fetch($cache_sig); } # if it is ab $is_absolute_path = $path[0] == DIRECTORY_SEPARATOR; # depart the path # normalizing array's parts require_once '_array.php'; # create a path part $path_parts = explode(DIRECTORY_SEPARATOR, strtolower($path)); # normalize the path parts _array::array_normalize($path_parts); # a fail safe if (!count($path_parts)) { return false; } # UNIX fs style $resolved_path = $is_absolute_path ? "" : "."; # WINNT fs style require_once 'string.php'; if (string::Contains($path_parts[0], ":")) { $is_absolute_path = 1; $resolved_path = $is_absolute_path ? "{$path_parts[0]}:" : "." . DIRECTORY_SEPARATOR; } # create a departed array $depart = explode(DIRECTORY_SEPARATOR, $path); # normalize the array _array::array_normalize($depart); # fetch the target file's name $file = $depart[count($depart) - 1]; # unset the file's name unset($depart[count($depart) - 1]); # create a cache sig $this_cache_sig = $base_cache_sig . strtolower(DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $depart)); # check for cache for files directory if ($fc->isCached($this_cache_sig)) { # fetch the file's directory $resolved_path = $fc->fetch($this_cache_sig); # through a hunch for file's address $hunch = $resolved_path . DIRECTORY_SEPARATOR . $file; if (file_exists($hunch)) { $resolved_path = $hunch; goto __RETURN; } # update the directory which bellow FOREACH will search under $path_parts = array($path_parts[count($path_parts) - 1]); } # do a BFS in subdirz foreach ($path_parts as $part) { if (!empty($part)) { $target_path = $resolved_path . DIRECTORY_SEPARATOR . $part; # create cache sig for this path $this_cache_sig = $base_cache_sig . strtolower($target_path); # check for fore head cache existance if ($fc->isCached($this_cache_sig)) { $target_path = $fc->fetch($this_cache_sig); if ($target_path[strlen($target_path) - 1] == DIRECTORY_SEPARATOR) { $target_path = strlen($target_path, 0, strlen($target_path) - 1); } } # check target path if (file_exists($target_path)) { if ($cache_built_path) { $fc->save($this_cache_sig, $target_path); } $resolved_path = $target_path; continue; } else { # delete any possible miss-formed cache data regarding to current path $fc->delete($this_cache_sig); } # a fail safe if (!is_readable($resolved_path)) { return false; } $files = scandir($resolved_path); $match_found = FALSE; foreach ($files as $file) { if (strtolower($file) == strtolower($part)) { # flag found $match_found = TRUE; # update target path $target_path = $resolved_path . DIRECTORY_SEPARATOR . $file; # update cache sig for this file $this_cache_sig = $base_cache_sig . strtolower($resolved_path . DIRECTORY_SEPARATOR . $part); # cache the path $fc->save($this_cache_sig, $target_path); # update resolved path $resolved_path = $target_path; # goto for next file iter. break; } } if (!$match_found) { return FALSE; } } } __RETURN: if ($convert_to_real_path) { $resolved_path = realpath($resolved_path); } # cache the result $fc->save($cache_sig, $resolved_path); return $resolved_path; }