コード例 #1
0
 public function clean($baseDir, $initDir = false, $filename = false)
 {
     if (strlen($filename)) {
         $documentRoot = \Bitrix\Main\Application::getDocumentRoot();
         $fn = IO\Path::combine($documentRoot, $baseDir, $initDir, $filename);
         $file = new IO\File($fn);
         //This checks for Zend Server CE in order to supress warnings
         if (function_exists('accelerator_reset')) {
             try {
                 $file->markWritable();
                 if ($file->delete()) {
                     \Bitrix\Main\Application::resetAccelerator();
                     return true;
                 }
             } catch (\Exception $ex) {
             }
         } else {
             if ($file->isExists()) {
                 $file->markWritable();
                 if ($file->delete()) {
                     \Bitrix\Main\Application::resetAccelerator();
                     return true;
                 }
             }
         }
         return false;
     } else {
         static $bAgentAdded = false;
         $bDelayedDelete = false;
         $source = IO\Path::combine($baseDir, $initDir);
         $sourceDir = new IO\Directory(IO\Path::convertRelativeToAbsolute($source));
         if ($sourceDir->isExists()) {
             $target = $source . ".~";
             for ($i = 0; $i < 9; $i++) {
                 $suffix = rand(0, 999999);
                 $targetDir = new IO\Directory(IO\Path::convertRelativeToAbsolute($target . $suffix));
                 if (!$targetDir->isExists()) {
                     $con = \Bitrix\Main\Application::getDbConnection();
                     $res = $con->queryExecute("INSERT INTO b_cache_tag (SITE_ID, CACHE_SALT, RELATIVE_PATH, TAG)\n\t\t\t\t\t\t\tVALUES ('*', '*', '" . $con->getSqlHelper()->forSql($target . $suffix) . "', '*')");
                     if ($res) {
                         if ($sourceDir->rename($targetDir->getPath())) {
                             $bDelayedDelete = true;
                         }
                     }
                     break;
                 }
             }
         }
         if ($bDelayedDelete) {
             if (!$bAgentAdded) {
                 $bAgentAdded = true;
                 /* TODO: јгенты */
                 $rsAgents = \CAgent::getList(array("ID" => "DESC"), array("NAME" => "CacheEngineFiles::delayedDelete(%"));
                 if (!$rsAgents->fetch()) {
                     $res = \CAgent::addAgent("CacheEngineFiles::delayedDelete();", "main", "Y", 1);
                 }
             }
         } else {
             $sourceDir->delete();
         }
         \Bitrix\Main\Application::resetAccelerator();
     }
 }