Exemple #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;
     }
 }
Exemple #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);
 }
 /**
  * @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;
 }
Exemple #4
0
 protected static function recurseFindItems($directoryPath, $wc, $sort)
 {
     $items = self::findItems(CFilePath::add($directoryPath, $wc), $sort);
     $dirs = self::findDirectories(CFilePath::add($directoryPath, "*"), $sort);
     $dirQty = CArray::length($dirs);
     for ($i = 0; $i < $dirQty; $i++) {
         CArray::pushArray($items, self::recurseFindItems($dirs[$i], $wc, $sort));
     }
     return $items;
 }
 public function testFrameworkPath()
 {
     $this->assertTrue(CFilePath::absolute(CFilePath::frameworkPath("{{PHRED_PATH_TO_APP}}/Tests"))->equals(CFilePath::absolute(CFilePath::add($GLOBALS["PHRED_PATH_TO_APP"], "/Tests"))));
     $this->assertTrue(CFilePath::absolute(CFilePath::frameworkPath("{{PHRED_PATH_TO_FRAMEWORK_ROOT}}/Application/Tests"))->equals(CFilePath::absolute(CFilePath::add($GLOBALS["PHRED_PATH_TO_FRAMEWORK_ROOT"], "/Application/Tests"))));
     $this->assertTrue(CFilePath::absolute(CFilePath::frameworkPath("{{PHRED_PATH_TO_APP}}Tests"))->equals(CFilePath::absolute(CFilePath::add($GLOBALS["PHRED_PATH_TO_APP"], "/Tests"))));
 }