2015-12-31 69 views
0

有一些類似的問題,但他們都沒有幫助我的案例。所有其他問題都在討論版本號,但我需要的只是增加內部版本號。如何在PHP中自動增加內部版本號

我需要一個腳本,將檢查是否有任何改變的文件/創建/刪除,並以1

增加版本號我找不到的答案,這個問題在網上和編寫了一個腳本自己。我在問這個問題,所以我可以分享我的腳本作爲答案。

這是我想出的腳本。隨意進一步改進和修改我的答案或張貼自己的答案:

// Opening the json file that holds the file paths and file modification dates 
$jsonArray = json_decode(file_get_contents('files.json'), true); 
$jsonFileArray = array(); 

// Putting the values into a local array 
foreach ($jsonArray as $filePath => $modifiedDate) { 
    $jsonFileArray[$filePath] = $modifiedDate; 
} 


// Iterating through the directories and putting the file paths and modification dates into a local array 
$filesArray = array(); 
$dir_iterator = new RecursiveDirectoryIterator("."); 
$recursive_iterator = new RecursiveIteratorIterator($dir_iterator); 
foreach ($recursive_iterator as $file) { 
    if ($file->isDir()) { 
     continue; 
    } 
    if (substr($file, -9) != 'error_log') { 
     $fileName = $file->getPathname(); 
     $fileModifiedDate = date('m/d/y H:i:s', $file->getMTime()); 
     $filesArray[$fileName] = $fileModifiedDate; 
    } 
} 


// Checking if there are any files that are modified/created/deleted 
if ($jsonFileArray != $filesArray) { 

    // If there are any changes, the build number is increased by 1 and saved into 'build' file 
    $buildFile = "build"; 
    file_put_contents($buildFile, file_get_contents($buildFile) + 1); 
} 


// Updating the json file with the latest modifiedDates 
$jsonFile = fopen('files.json', 'w'); 
fwrite($jsonFile, json_encode($filesArray, JSON_UNESCAPED_SLASHES)); 
fclose($jsonFile); 
+2

哇,這是一個快速downvote(2秒內)。我認爲你甚至不可能在2秒內閱讀這個問題。 –

+0

我相信你被低估了,因爲你的問題沒有表現出什麼努力,而且非常寬泛。你也有幾個密切的選票,這兩個都表明你的問題太廣泛了。 –

+0

遞減投票是好的,我不反對這一點。但是,即使沒有閱讀這個問題,但是下調是荒謬的。他/她應該增加評論,所以我可以改善這個問題。我在問題中添加了我的代碼,並試圖在文本中進一步闡明它。我試圖讓這是一個質量問題,但降價並不是很有幫助。如果你想改進它,請添加評論或自己編輯問題。 –

回答

1

下面的代碼獲取目錄中的所有文件,並將它們放在一個陣列,並將其保存爲一個JSON文件。當腳本再次運行時,它會再次獲取所有文件和修改日期,並將其與JSON文件進行比較。如果有任何更改(例如文件被修改/創建/刪除),它將內部版本號增加1.

// Opening the json file that holds the file paths and file modification dates 
$jsonArray = json_decode(file_get_contents('files.json'), true); 
$jsonFileArray = array(); 

// Putting the values into a local array 
foreach ($jsonArray as $filePath => $modifiedDate) { 
    $jsonFileArray[$filePath] = $modifiedDate; 
} 


// Iterating through the directories and putting the file paths and modification dates into a local array 
$filesArray = array(); 
$dir_iterator = new RecursiveDirectoryIterator("."); 
$recursive_iterator = new RecursiveIteratorIterator($dir_iterator); 
foreach ($recursive_iterator as $file) { 
    if ($file->isDir()) { 
     continue; 
    } 
    if (substr($file, -9) != 'error_log') { 
     $fileName = $file->getPathname(); 
     $fileModifiedDate = date('m/d/y H:i:s', $file->getMTime()); 
     $filesArray[$fileName] = $fileModifiedDate; 
    } 
} 


// Checking if there are any files that are modified/created/deleted 
if ($jsonFileArray != $filesArray) { 

    // If there are any changes, the build number is increased by 1 and saved into 'build' file 
    $buildFile = "build"; 
    file_put_contents($buildFile, file_get_contents($buildFile) + 1); 
} 


// Updating the json file with the latest modifiedDates 
$jsonFile = fopen('files.json', 'w'); 
fwrite($jsonFile, json_encode($filesArray, JSON_UNESCAPED_SLASHES)); 
fclose($jsonFile); 
+0

請編輯您的原始文章,以將任何信息(例如此代碼)添加到您的問題中。 –

+1

我修改了它。我相信這個問題非常清楚。我問了這個問題,所以我可以分享我的知識。據我所知,這是SO等論壇的重點:所以我們可以分享我們的知識。 –

+0

是的,沒有。 SO不是(討論)論壇,它是一個問答網站。一個好的答案***將總是解釋所做的事情以及爲什麼這樣做,不僅是爲了OP,而且是爲了將來SO的訪問者。目前尚不清楚你是否正在回答你自己的問題。 –

相關問題