|
83 | 83 | throw new InvalidArgumentException("Supplied Magento root directory '$rootDir' does not exist");
|
84 | 84 | }
|
85 | 85 |
|
86 |
| - $tempDir = findUnusedFilename("$rootDir/temp_project"); |
| 86 | + $tempDir = findUnusedFilename($rootDir, "temp_project"); |
87 | 87 |
|
88 | 88 | // The composer command uses the Magento root as the working directory so this script can be run from anywhere
|
89 | 89 | $cmd = (!empty($opts['composer']) ? $opts['composer'] : 'composer') . " --working-dir='$rootDir'";
|
|
176 | 176 |
|
177 | 177 | /**** Execute Updates ****/
|
178 | 178 |
|
179 |
| - $composerBackup = findUnusedFilename("$rootDir/composer.json.bak"); |
| 179 | + $composerBackup = findUnusedFilename($rootDir, "composer.json.bak"); |
180 | 180 | output("Backing up $rootDir/composer.json to $composerBackup");
|
181 | 181 | copy("$rootDir/composer.json", $composerBackup);
|
182 | 182 |
|
|
185 | 185 | $repoLabels = array_map('strtolower',array_keys($composer['repositories']));
|
186 | 186 | $newLabel = 'magento';
|
187 | 187 | if (in_array($newLabel, $repoLabels)) {
|
188 |
| - $i = 1; |
189 |
| - while (in_array("$newLabel-$i", $repoLabels)) $i++; |
190 |
| - $newLabel = "$newLabel-$i"; |
| 188 | + $count = count($repoLabels); |
| 189 | + for ($i = 1; $i <= $count; $i++) { |
| 190 | + if (!in_array("$newLabel-$i", $repoLabels)) { |
| 191 | + $newLabel = "$newLabel-$i"; |
| 192 | + break; |
| 193 | + } |
| 194 | + } |
191 | 195 | }
|
192 | 196 | output("Adding $repo to composer repositories under label '$newLabel'");
|
193 | 197 | runComposer("config repositories.$newLabel composer $repo");
|
|
226 | 230 |
|
227 | 231 | // Update Magento/Updater if it's installed
|
228 | 232 | if (file_exists("$rootDir/update")) {
|
229 |
| - $updateBackup = findUnusedFilename("$rootDir/update.bak"); |
| 233 | + $updateBackup = findUnusedFilename($rootDir, "update.bak"); |
230 | 234 | output("Backing up Magento/Updater directory $rootDir/update to $updateBackup");
|
231 | 235 | rename("$rootDir/update", $updateBackup);
|
232 | 236 | output('Updating Magento/Updater');
|
|
272 | 276 | /**
|
273 | 277 | * Gets a variant of a filename that doesn't already exist so we don't overwrite anything
|
274 | 278 | *
|
| 279 | + * @param string $dir |
275 | 280 | * @param string $filename
|
276 | 281 | * @return string
|
277 | 282 | */
|
278 |
| -function findUnusedFilename($filename) { |
279 |
| - if (file_exists($filename)) { |
280 |
| - $i = 1; |
281 |
| - while (file_exists($filename . "_$i")) $i++; |
282 |
| - $filename = $filename . "_$i"; |
| 283 | +function findUnusedFilename($dir, $filename) { |
| 284 | + $unique = "$dir/$filename"; |
| 285 | + if (file_exists($unique)) { |
| 286 | + $unique = tempnam($dir, "$filename."); |
| 287 | + unlink($unique); |
283 | 288 | }
|
284 |
| - return $filename; |
| 289 | + return $unique; |
285 | 290 | }
|
286 | 291 |
|
287 | 292 | /**
|
|
0 commit comments