/** * Normalizes a path by removing any trailing slashes, any redundant slashes, any references to the current * directory, and any references to the parent directory where possible, and returns the new path. * * For example, "/path//./dir-a/.././to//../dir-b/" is normalized to "/path/dir-b". * * @param string $path The path to be normalized (can be absolute or relative). * @param bool $targetIsExecutable **OPTIONAL. Default is** `false`. Tells whether the path's target should be * treated as an executable so that, if the path starts with ".", the resulting path will start with "." too and * the "." will not be removed as a reference to the current directory. * * @return CUStringObject The normalized path. */ public static function normalize($path, $targetIsExecutable = false) { assert('is_cstring($path) && is_bool($targetIsExecutable)', vs(isset($this), get_defined_vars())); assert('!CString::isEmpty($path)', vs(isset($this), get_defined_vars())); $path = CRegex::replace($path, "/\\/{2,}/", "/"); // normalize consecutive slashes $path = CString::stripEnd($path, "/"); // remove the trailing slash, if any if (CString::isEmpty($path)) { return "/"; } $path = CRegex::remove($path, "/\\/\\.(?=\\/|\\z)/"); // remove any "/." followed by a slash or at the end if (CString::isEmpty($path)) { return "/"; } if (!$targetIsExecutable) { $path = CString::stripStart($path, "./"); } $pathIsAbsolute; if (!CString::startsWith($path, "/")) { $pathIsAbsolute = false; } else { $pathIsAbsolute = true; $path = CString::substr($path, 1); } if (!CString::find($path, "/")) { if ($pathIsAbsolute) { if (!CString::equals($path, "..")) { $path = "/{$path}"; } else { $path = "/"; } } return $path; } // Recompose the path. $components = CString::split($path, "/"); $newComponents = CArray::make(); $len = CArray::length($components); for ($i = 0; $i < $len; $i++) { $comp = $components[$i]; $lastAddedComp = ""; $noCompsAddedYet = CArray::isEmpty($newComponents); if (!$noCompsAddedYet) { $lastAddedComp = CArray::last($newComponents); } if (CString::equals($comp, "..")) { if ($noCompsAddedYet || CString::equals($lastAddedComp, "..") || CString::equals($lastAddedComp, ".")) { if (!($noCompsAddedYet && $pathIsAbsolute)) { CArray::push($newComponents, $comp); } } else { CArray::pop($newComponents); } } else { CArray::push($newComponents, $comp); } } $path = CArray::join($newComponents, "/"); if ($pathIsAbsolute) { $path = "/{$path}"; } else { if (CString::isEmpty($path)) { $path = "."; } } return $path; }
/** * Returns the last element from an array. * * @return mixed The last element in the array. */ public function last() { return CArray::last($this->m_splArray); }
public function testLast() { $array = CArray::fromElements("a", "b", "c"); $this->assertTrue(CArray::last($array) === "c"); }
/** * @ignore */ public static function initialize() { $configDp = CFilePath::add($GLOBALS["PHRED_PATH_TO_APP"], "Configuration"); $configEnvsDp = CFilePath::add($configDp, "Environments"); $configs = CArray::make(); $currEnv; if ($GLOBALS["PHRED_TESTS"]) { $currEnv = "tst"; } // Main configuration files. $configFps = CFile::findFiles(CFilePath::add($configDp, "*.json")); $numConfigs = CArray::length($configFps); for ($i = 0; $i < $numConfigs; $i++) { $configFp = $configFps[$i]; $configName = CFilePath::nameOnly($configFp); self::readAndAddConfig($configFp, $configName, $configs); if (!isset($currEnv) && CString::equals($configName, "Application")) { $config = CArray::last($configs); $currEnv = $config[self::$ms_configAliases["Application"]]["environment"]; } } assert('is_cstring($currEnv)', vs(isset($this), get_defined_vars())); // The configuration files from the current environment's directory. $currEnvDp = CFilePath::add($configEnvsDp, $currEnv); assert('CFile::exists($currEnvDp)', vs(isset($this), get_defined_vars())); $configFps = CFile::findFiles(CFilePath::add($currEnvDp, "*.json")); $numConfigs = CArray::length($configFps); for ($i = 0; $i < $numConfigs; $i++) { $configFp = $configFps[$i]; $configName = CFilePath::nameOnly($configFp); self::readAndAddConfig($configFp, $configName, $configs); } self::$ms_config = call_user_func_array("CMap::merge", CArray::toPArray($configs)); self::$ms_isInitialized = true; }