Example #1
0
 public function testClone()
 {
     $filePath0 = CFile::createTemporary();
     $filePath1 = CFile::createTemporary();
     $filePath2 = CFile::createTemporary();
     $filePath3 = CFile::createTemporary();
     CFile::write($filePath0, "The quick");
     CFile::write($filePath1, " brown fox");
     CFile::write($filePath2, " jumps over");
     CFile::write($filePath3, " the lazy dog.");
     $hash0 = new CHash(CHash::SHA256);
     $hash0->computeMoreFromFile($filePath0);
     $hash0->computeMoreFromFile($filePath1);
     $hash0->computeMoreFromFile($filePath2);
     $hash0->computeMoreFromFile($filePath3);
     $hash1 = clone $hash0;
     $hash1->computeMoreFromFile($filePath0);
     $this->assertTrue($hash0->finalize()->equals("ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c"));
     $this->assertTrue($hash1->finalize()->equals("14dc40e99be202c4e59a0c6d1a8854bb50253624080435ed8c65bd6e5e880c95"));
     CFile::delete($filePath0);
     CFile::delete($filePath1);
     CFile::delete($filePath2);
     CFile::delete($filePath3);
 }
Example #2
0
 /**
  * @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();
 }
Example #3
0
 public function testIsPosPastEnd()
 {
     $filePath = CFile::createTemporary();
     CFile::write($filePath, "Hello");
     $file = new CFile($filePath, CFile::READ);
     $file->readBytes(5);
     $this->assertFalse($file->isPosPastEnd());
     $file->done();
     // only needed in these tests
     CFile::write($filePath, "Hello");
     $file = new CFile($filePath, CFile::READ);
     $file->setPosToEnd();
     $this->assertFalse($file->isPosPastEnd());
     $file->done();
     // only needed in these tests
     CFile::write($filePath, "Hello");
     $file = new CFile($filePath, CFile::READ);
     $file->readBytes(5);
     $data = $file->readAvailableBytes(5);
     $this->assertTrue($file->isPosPastEnd() && $data->equals(""));
     $file->done();
     // only needed in these tests
     CFile::delete($filePath);
 }
Example #4
0
 private static function writeCSVRecords($file, $request_id, $records)
 {
     $f = new CFile($file);
     $f->open('a');
     foreach ($records as $fields) {
         $f->write($request_id . self::csv_fields_delimiter);
         $f->write(implode(self::csv_fields_delimiter, $fields));
         $f->write(self::csv_lines_delimiter);
     }
     $f->close();
 }
Example #5
0
 /**
  * For any request that needs to upload a file, specifies the data to be uploaded and, for HTTP requests, the
  * file's metadata.
  *
  * For large data, `setUploadFile` method would be preferred to upload an already existing file.
  *
  * @param  data $data The data to be uploaded.
  * @param  string $mimeType **OPTIONAL.** *Required for `HTTP_UPLOAD` requests only.* The MIME type of the file.
  * @param  string $fileName **OPTIONAL.** *Required for `HTTP_UPLOAD` requests only.* The custom name of the file.
  * @param  string $fileId **OPTIONAL.** *Required for `HTTP_UPLOAD` requests only.* The ID under which the file is
  * to arrive to the destination server.
  *
  * @return void
  *
  * @link   #method_setUploadFile setUploadFile
  */
 public function setUploadData($data, $mimeType = null, $fileName = null, $fileId = null)
 {
     assert('is_cstring($data) && (!isset($mimeType) || is_cstring($mimeType)) && ' . '(!isset($fileName) || is_cstring($fileName)) && (!isset($fileId) || is_cstring($fileId))', vs(isset($this), get_defined_vars()));
     $this->m_fileUploadTempFp = CFile::createTemporary();
     CFile::write($this->m_fileUploadTempFp, $data);
     $this->setUploadFile($this->m_fileUploadTempFp, $mimeType, $fileName, $fileId);
 }