exists() private method

Tests current filesystem object existence and returns boolean.
private exists ( ) : boolean
return boolean 'True' if filesystem object exists, otherwise 'False'
Example #1
0
 /**
  * @ignore
  */
 public static function maybeUpdateThirdParty()
 {
     if (!self::isInCliMode()) {
         // This method can be run in CLI mode only.
         assert('false', vs(isset($this), get_defined_vars()));
         return false;
     }
     $updates = CConfiguration::option("updates");
     $updatesAreEnabled = $updates["enable"];
     if ($updatesAreEnabled) {
         $minTimeBetweenDoUpdatesDays = $updates["minTimeBetweenDoUpdatesDays"];
         $components = $updates["components"];
         assert('is_int($minTimeBetweenDoUpdatesDays)', vs(isset($this), get_defined_vars()));
         // Logging.
         $logging = $updates["logging"];
         $loggingIsEnabled = $logging["enable"];
         $logFp = $logging["logFilePath"];
         if ($loggingIsEnabled) {
             assert('!CString::isEmpty($logFp)', vs(isset($this), get_defined_vars()));
             $logFp = CFilePath::frameworkPath($logFp);
             CShell::setLogging($logFp);
         }
         // Mailing.
         $mailing = $updates["mailing"];
         $mailingIsEnabled = $mailing["enable"];
         if ($mailingIsEnabled) {
             $adminMail = CConfiguration::option("admin.mail");
             $to = $adminMail["to"];
             $from = $adminMail["from"];
             $transport = $adminMail["transport"];
             assert('!CString::isEmpty($to) && !CString::isEmpty($from) && !CString::isEmpty($transport)', vs(isset($this), get_defined_vars()));
             $mail;
             if (CString::equalsCi($transport, "smtp")) {
                 $smtpOutgoingServer = $adminMail["smtpOutgoingServer"];
                 $smtpUsername = $adminMail["smtpUsername"];
                 $smtpPassword = $adminMail["smtpPassword"];
                 assert('!CString::isEmpty($smtpOutgoingServer) && !CString::isEmpty($smtpUsername) && ' . '!CString::isEmpty($smtpPassword)', vs(isset($this), get_defined_vars()));
                 $mail = CMail::makeSmtp($smtpOutgoingServer, $smtpUsername, $smtpPassword, $from, $to);
             } else {
                 if (CString::equalsCi($transport, "system")) {
                     $mail = CMail::makeSystem($from, $to);
                 } else {
                     assert('false', vs(isset($this), get_defined_vars()));
                 }
             }
             CShell::setMailing($mail);
         }
         $thirdPartyDp = $GLOBALS["PHRED_PATH_TO_THIRD_PARTY"];
         $lastUpdateTimeFp = CFilePath::add($thirdPartyDp, self::$ms_thirdPartyLastUpdateTimeFn);
         // Read the file containing the Unix seconds of the last update time stamp (if exists) and compare that
         // time with the current time.
         $numDaysSinceLastUpdate;
         if (CFile::exists($lastUpdateTimeFp)) {
             $lastUpdateTime = new CTime(CString::toInt(CFile::read($lastUpdateTimeFp)));
             $currTime = CTime::now();
             if ($lastUpdateTime->isBefore($currTime)) {
                 $numDaysSinceLastUpdate = $currTime->diffInDays($lastUpdateTime);
                 if ($numDaysSinceLastUpdate < $minTimeBetweenDoUpdatesDays) {
                     // It is too early for updates yet.
                     return false;
                 }
             } else {
                 assert('false', vs(isset($this), get_defined_vars()));
             }
         }
         $date = CShell::currentDate();
         CShell::say("Started on {$date}.");
         if (isset($numDaysSinceLastUpdate)) {
             CShell::say("It has been {$numDaysSinceLastUpdate} day(s) since last successful update.");
         }
         $concurrLockFp = CFilePath::add($thirdPartyDp, self::$ms_thirdPartyConcurrLockFn);
         // Try locking the operation.
         if (!self::setLock($concurrLockFp, false)) {
             assert('false', vs(isset($this), get_defined_vars()));
             CShell::onError(false, "Could not obtain a lock on the operation.");
             CShell::writeToLog("\n");
             return false;
         }
         $phpConfigNeedsReload = false;
         $totalNumComponents = CMap::length($components);
         $numComponentsUpdated = 0;
         // The Browser Capabilities Project (BrowsCap).
         if (CMap::hasKey($components, "browsCap")) {
             $browsCap = $components["browsCap"];
             $skip = $browsCap["skip"];
             if (!$skip) {
                 CShell::say("Updating the Browser Capabilities Project (BrowsCap) ...");
                 $lookupFileUrl = $browsCap["lookupFileUrl"];
                 assert('!CString::isEmpty($lookupFileUrl)', vs(isset($this), get_defined_vars()));
                 // Component-related constants.
                 static $s_configOptName = "browscap";
                 static $s_lookupFileDownloadTimeoutSeconds = 120;
                 if (self::hasConfigOption($s_configOptName)) {
                     $browsCapLookupFp = CString::trim(self::configOption($s_configOptName));
                     if (!CString::isEmpty($browsCapLookupFp)) {
                         $browsCapDp = CFilePath::directory($browsCapLookupFp);
                         CShell::say("Downloading a BrowsCap lookup file from '{$lookupFileUrl}' ...");
                         $temporaryFp = CFile::createTemporary($browsCapDp);
                         $downloadRes = CInetRequest::downloadFile($lookupFileUrl, $temporaryFp, $s_lookupFileDownloadTimeoutSeconds);
                         if ($downloadRes) {
                             // After the file is downloaded into a temporary one, move it to the destination,
                             // safely replacing the existing file, if any.
                             CFile::move($temporaryFp, $browsCapLookupFp);
                             $numComponentsUpdated++;
                             $phpConfigNeedsReload = true;
                             $downloadedFileSizeKB = CUUnit::convertStoragef((double) CFile::size($browsCapLookupFp), CUUnit::BYTE, CUUnit::KILOBYTE);
                             $downloadedFileSizeKB = CMathf::round($downloadedFileSizeKB, 2);
                             CShell::say("Done. The downloaded file is {$downloadedFileSizeKB} KB in size.");
                         } else {
                             CShell::onError(false, "Could not download a BrowsCap lookup file from '{$lookupFileUrl}'.");
                         }
                         // Just in case, check for any temporary files that could have been left by any previous
                         // operations in the directory.
                         $leftoverFiles = CFile::findFiles(CFilePath::add($browsCapDp, CFile::DEFAULT_TEMPORARY_FILE_PREFIX . "*"));
                         if (!CArray::isEmpty($leftoverFiles)) {
                             // Cleanup the directory from the temporary files.
                             $len = CArray::length($leftoverFiles);
                             for ($i = 0; $i < $len; $i++) {
                                 CFile::delete($leftoverFiles[$i]);
                             }
                         }
                     } else {
                         CShell::onError(false, "Could not read the value of '{$s_configOptName}' option " . "in the PHP CLI configuration file.");
                     }
                 } else {
                     CShell::onError(false, "Could not find '{$s_configOptName}' option in the PHP CLI configuration file.");
                 }
             } else {
                 CShell::say("Skipping the Browser Capabilities Project (BrowsCap).");
             }
         }
         // All the components have been processed. Unlock the operation.
         self::unsetLock($concurrLockFp);
         $date = CShell::currentDate();
         if ($numComponentsUpdated != 0) {
             // One or more third-party components have been updated. Put a time stamp on the directory where the
             // components are located.
             CFile::write($lastUpdateTimeFp, CString::fromInt(CTime::currentUTime()));
             if ($numComponentsUpdated == $totalNumComponents) {
                 CShell::speak("Success. All {$totalNumComponents} third-party component(s)");
             } else {
                 CShell::speak("Partial success. {$numComponentsUpdated} out of {$totalNumComponents} third-party component(s)");
             }
             CShell::say("have been updated. Completed on {$date}.");
         } else {
             CShell::say("No third-party components have been updated. Completed on {$date}.");
         }
         return $phpConfigNeedsReload;
     } else {
         return false;
     }
 }
Example #2
0
 public function testReFindDirectoriesOnNameRecursive()
 {
     $directoryPath = CFilePath::add(CSystem::temporaryFilesDp(), CFile::DEFAULT_TEMPORARY_FILE_PREFIX . self::$ms_tempDirName);
     if (CFile::exists($directoryPath)) {
         CFile::deleteDirectoryRecursive($directoryPath);
     }
     CFile::createDirectory($directoryPath);
     CFile::create(CFilePath::add($directoryPath, "file-a3"));
     CFile::create(CFilePath::add($directoryPath, "file-a20"));
     CFile::create(CFilePath::add($directoryPath, "file-a100"));
     CFile::create(CFilePath::add($directoryPath, "file-b3"));
     CFile::create(CFilePath::add($directoryPath, "file-b20"));
     CFile::create(CFilePath::add($directoryPath, "file-b100"));
     $directoryPathSub0 = CFilePath::add($directoryPath, "dir-a2");
     $directoryPathSub1 = CFilePath::add($directoryPath, "dir-a10");
     $directoryPathSub2 = CFilePath::add($directoryPath, "dir-b2");
     $directoryPathSub3 = CFilePath::add($directoryPath, "dir-b10");
     CFile::createDirectory($directoryPathSub0);
     CFile::createDirectory(CFilePath::add($directoryPathSub0, "dir-a2"));
     CFile::createDirectory(CFilePath::add($directoryPathSub0, "dir-a10"));
     CFile::createDirectory($directoryPathSub1);
     CFile::createDirectory(CFilePath::add($directoryPathSub1, "dir-a2"));
     CFile::createDirectory(CFilePath::add($directoryPathSub1, "dir-a10"));
     CFile::createDirectory($directoryPathSub2);
     CFile::createDirectory(CFilePath::add($directoryPathSub2, "dir-b2"));
     CFile::createDirectory(CFilePath::add($directoryPathSub2, "dir-b10"));
     CFile::createDirectory($directoryPathSub3);
     CFile::createDirectory(CFilePath::add($directoryPathSub3, "dir-b2"));
     CFile::createDirectory(CFilePath::add($directoryPathSub3, "dir-b10"));
     $paths = CFile::reFindDirectoriesOnNameRecursive($directoryPath, "/^.*-b/");
     $comparator = function ($arrayString, $findString) {
         return $arrayString->endsWith($findString);
     };
     $this->assertTrue($paths->length() == 6);
     $this->assertTrue($paths->find("/dir-b2", $comparator) && $paths->find("/dir-b10", $comparator) && $paths->find("/dir-b2/dir-b2", $comparator) && $paths->find("/dir-b2/dir-b10", $comparator) && $paths->find("/dir-b10/dir-b2", $comparator) && $paths->find("/dir-b10/dir-b10", $comparator));
     $paths = CFile::reFindDirectoriesOnNameRecursive($directoryPath, "/^.*-b/", true);
     $this->assertTrue($paths->length() == 6);
     $this->assertTrue($paths[0]->endsWith("/dir-b2") && $paths[1]->endsWith("/dir-b10") && $paths[2]->endsWith("/dir-b2/dir-b2") && $paths[3]->endsWith("/dir-b2/dir-b10") && $paths[4]->endsWith("/dir-b10/dir-b2") && $paths[5]->endsWith("/dir-b10/dir-b10"));
     CFile::deleteDirectoryRecursive($directoryPath);
 }
Example #3
0
 protected function finalize()
 {
     if (is_resource($this->m_multiCurl)) {
         curl_multi_close($this->m_multiCurl);
     }
     if (isset($this->m_cookiesFp) && CFile::exists($this->m_cookiesFp)) {
         CFile::delete($this->m_cookiesFp);
     }
     $this->m_done = true;
 }
Example #4
0
 protected function finalize()
 {
     if ($this->m_done) {
         return;
     }
     if (is_resource($this->m_curl)) {
         curl_close($this->m_curl);
     }
     if (isset($this->m_downloadFile)) {
         $this->m_downloadFile->done();
     }
     if (isset($this->m_uploadFile)) {
         $this->m_uploadFile->done();
     }
     if (isset($this->m_fileUploadTempFp) && CFile::exists($this->m_fileUploadTempFp)) {
         CFile::delete($this->m_fileUploadTempFp);
     }
     $this->m_done = true;
 }
Example #5
0
 public function testAbsolute()
 {
     $directoryPath = CFilePath::add(CSystem::temporaryFilesDp(), CFile::DEFAULT_TEMPORARY_FILE_PREFIX . self::$ms_tempDirName);
     if (CFile::exists($directoryPath)) {
         CFile::deleteDirectoryRecursive($directoryPath);
     }
     CFile::createDirectory($directoryPath);
     $filePathAbs = CFilePath::add($directoryPath, "file");
     CFile::create($filePathAbs);
     CSystem::cd($directoryPath);
     $this->assertTrue(CFilePath::absolute("file")->equals(CFilePath::absolute($filePathAbs)));
     $this->assertTrue(CFilePath::absolute("./file")->equals(CFilePath::absolute($filePathAbs)));
     CFile::deleteDirectoryRecursive($directoryPath);
 }