public function testDirectory() { $this->assertTrue(CFilePath::directory("/path/to/file.png")->equals("/path/to")); $this->assertTrue(CFilePath::directory("/path/to/file.tar.gz")->equals("/path/to")); $this->assertTrue(CFilePath::directory("./path/to/file.png")->equals("path/to")); $this->assertTrue(CFilePath::directory("../path/to/file.png")->equals("../path/to")); $this->assertTrue(CFilePath::directory("../path/to/../file.png")->equals("../path")); $this->assertTrue(CFilePath::directory("file.png")->equals(".")); $this->assertTrue(CFilePath::directory("/path//to///file.png")->equals("/path/to")); $this->assertTrue(CFilePath::directory("/path/./to//./file.png")->equals("/path/to")); $this->assertTrue(CFilePath::directory("/")->equals("/")); $this->assertTrue(CFilePath::directory("/..")->equals("/")); $this->assertTrue(CFilePath::directory("/../..")->equals("/")); $this->assertTrue(CFilePath::directory(".")->equals(".")); $this->assertTrue(CFilePath::directory("..")->equals(".")); $this->assertTrue(CFilePath::directory("../dir")->equals("..")); $this->assertTrue(CFilePath::directory("../../dir")->equals("../..")); }
/** * @ignore */ public static function onThirdPartyUpdateByPackageManager() { if (!self::isInCliMode()) { // This method can be run in CLI mode only. assert('false', vs(isset($this), get_defined_vars())); } $timeoutPause = new CTimeoutPause(); CShell::speak("Processing third-party components ..."); $tpDps = CFile::listDirectories(CFilePath::absolute($GLOBALS["PHRED_PATH_TO_THIRD_PARTY"])); $ignorePackages = CConfiguration::option("upd.thirdPartyOopWrappingIgnorePackages"); $ignorePackagesL2 = CArray::filter($ignorePackages, function ($package) { return CString::find($package, "/"); }); $newTpDps = CArray::make(); $len = CArray::length($tpDps); for ($i = 0; $i < $len; $i++) { $tpDp = $tpDps[$i]; $dirName = CFilePath::name($tpDp); if (!CArray::find($ignorePackages, $dirName)) { $dpHasL2DirsToIgnore = CArray::find($ignorePackagesL2, $dirName, function ($packageL2, $dirName) { return CString::equals(CFilePath::directory($packageL2), $dirName); }); if (!$dpHasL2DirsToIgnore) { CArray::push($newTpDps, $tpDp); } else { $tpSubDps = CFile::listDirectories($tpDp); $tpSubDps = CArray::filter($tpSubDps, function ($subDp) use($ignorePackagesL2) { return !CArray::find($ignorePackagesL2, $subDp, function ($packageL2, $subDp) { return CString::endsWith($subDp, $packageL2); }); }); CArray::pushArray($newTpDps, $tpSubDps); } } } $tpDps = $newTpDps; $wrapProtectedMethods = CConfiguration::option("upd.thirdPartyOopWrappingInProtectedMethods"); $wrapPrivateMethods = CConfiguration::option("upd.thirdPartyOopWrappingInPrivateMethods"); static $s_stdPhpTag = "<?php"; static $s_progressResolution = 0.05; $prevProgressDivR = 0; $tpDpsLen = CArray::length($tpDps); for ($i0 = 0; $i0 < $tpDpsLen; $i0++) { $tpFps = CFile::reFindFilesRecursive($tpDps[$i0], "/\\.php\\d?\\z/"); $tpFpsLen = CArray::length($tpFps); for ($i1 = 0; $i1 < $tpFpsLen; $i1++) { $fileCode = CFile::read($tpFps[$i1]); if (!CString::find($fileCode, self::$ms_thirdPartyAlreadyOopWrappedMark)) { $parser = new PhpParser\Parser(new PhpParser\Lexer()); try { // Parse the code. $statements = $parser->parse($fileCode); // Wrap the code into OOP. $traverser = new PhpParser\NodeTraverser(); $mainVisitor = new CMainVisitor($wrapProtectedMethods, $wrapPrivateMethods); $traverser->addVisitor($mainVisitor); $statements = $traverser->traverse($statements); $wrappedCode = (new PhpParser\PrettyPrinter\Standard())->prettyPrint($statements); $phpTagPos = CString::indexOf($wrappedCode, $s_stdPhpTag); if ($phpTagPos == -1) { $wrappedCode = "{$s_stdPhpTag}\n\n{$wrappedCode}"; $phpTagPos = 0; } $wrappedCode = CString::insert($wrappedCode, $phpTagPos + CString::length($s_stdPhpTag), "\n\n" . self::$ms_thirdPartyAlreadyOopWrappedMark); // Save. CFile::write($tpFps[$i1], $wrappedCode); } catch (PhpParser\Error $parserError) { CShell::say("\nPhpParser: " . $tpFps[$i1] . ", at line " . $parserError->getRawLine() . ": " . $parserError->getRawMessage()); } } $progress = (double) ($i0 / $tpDpsLen + 1 / $tpDpsLen * $i1 / $tpFpsLen); $progressDivR = CMathi::floor($progress / $s_progressResolution); if ($progressDivR != $prevProgressDivR) { $perc = CMathi::round($progressDivR * $s_progressResolution * 100); CShell::speak("{$perc}%"); } $prevProgressDivR = $progressDivR; } } CShell::speak("100%"); CShell::say("Done."); $timeoutPause->end(); }
/** * Returns the path to the directory where the running script is located. * * @return CUStringObject The path to the directory where the running script is located. */ public static function scriptDp() { return CFilePath::directory(CSystem::initScriptFp()); }
public function testRename() { $filePath0 = CFile::createTemporary(); $newName = CFilePath::name($filePath0) . "-renamed"; $filePath1 = CFilePath::add(CFilePath::directory($filePath0), $newName); CFile::write($filePath0, "Hello there!"); CFile::rename($filePath0, $newName); $this->assertTrue(!CFile::exists($filePath0) && CFile::exists($filePath1) && CFile::read($filePath1)->equals("Hello there!")); CFile::delete($filePath1); }
/** * Returns the paths to the files and subdirectories found by a wildcard pattern, also searching in the * subdirectories and so on. * * Wildcard patterns use "\*" to match zero or more arbitrary characters, except the character of "/", and "?" to * match any single arbitrary character, again except the character of "/". For example, "\*.png" pattern searches * for all PNG images in the *current* directory, and "/path/to/any\*subdir/\*thumb-year-201?\*" searches for all * thumbnail images dated by the years of 2010-2019 and that are contained in the directories with a path matching * the rest of the pattern. Distinct from regular expressions, wildcard patterns need to be anchored at both ends, * so if the pattern in the last example was not concluded with "\*", no images would be found. Wildcards also * support character classes, such as "[abc]" and "[0-9]" as well as "[^abc]" and "[^0-9]", which however is only a * small portion of what regular expressions can do. * * The returned paths are always absolute. * * @param string $wildcardPattern The wildcard pattern to be used for searching. * @param bool $sort **OPTIONAL. Default is** `false`. Tells whether the returned paths should be sorted, in the * ascending order. * * @return CArrayObject The paths to the files and subdirectories found by the wildcard pattern specified, * including ones found in the subdirectories and so on. */ public static function findItemsRecursive($wildcardPattern, $sort = false) { assert('is_cstring($wildcardPattern) && is_bool($sort)', vs(isset($this), get_defined_vars())); $wildcardPattern = CFilePath::frameworkPath($wildcardPattern); return oop_a(self::recurseFindItems(CFilePath::directory($wildcardPattern), CFilePath::name($wildcardPattern), $sort)); }