Delete a file or directory.
public remove ( string | array | Traversable $files, boolean $retryWithChmod = false ) : boolean | ||
$files | string | array | Traversable | A filename, an array of files, or a \Traversable instance to delete. |
$retryWithChmod | boolean | Whether to retry deleting on error, after recursively changing file modes to add read/write/exec permissions. A bit like 'rm -rf'. |
리턴 | boolean |
/** * Test FilesystemHelper::remove() on directories. */ public function testRemoveDir() { // Create a test directory containing some files in several levels. $testDir = $this->tempDir(true); // Check that the directory can be removed. $this->assertTrue($this->filesystemHelper->remove($testDir)); $this->assertFileNotExists($testDir); }
/** * Process the defined special destinations. */ protected function symLinkSpecialDestinations() { foreach ($this->specialDestinations as $sourcePattern => $relDestination) { $matched = glob($this->appRoot . '/' . $sourcePattern, GLOB_NOSORT); if (!$matched) { continue; } // On Platform these replacements would be a bit different. $absDestination = str_replace(array('{webroot}', '{approot}'), $this->buildDir, $relDestination); foreach ($matched as $source) { // Ignore the source if it's in ignoredFiles. $relSource = str_replace($this->appRoot . '/', '', $source); if (in_array($relSource, $this->ignoredFiles)) { continue; } $this->output->writeln("Symlinking {$relSource} to {$relDestination}"); $destination = $absDestination; // Do not overwrite directories with files. if (!is_dir($source) && is_dir($destination)) { $destination = $destination . '/' . basename($source); } // Delete existing files, emitting a warning. if (file_exists($destination)) { $this->output->writeln(sprintf("Overriding existing path '%s' in destination", str_replace($this->buildDir . '/', '', $destination))); $this->fsHelper->remove($destination); } $this->fsHelper->symlink($source, $destination); } } }
/** * Clone the app to the build directory via Git. * * @param string $buildDir */ private function cloneToBuildDir($buildDir) { $gitRoot = $this->gitHelper->getRoot($this->appRoot, true); $ref = $this->gitHelper->execute(['rev-parse', 'HEAD'], $gitRoot, true); $cloneArgs = ['--recursive', '--shared']; $tmpRepo = $buildDir . '-repo'; if (file_exists($tmpRepo)) { $this->fsHelper->remove($tmpRepo, true); } $this->gitHelper->cloneRepo($gitRoot, $tmpRepo, $cloneArgs, true); $this->gitHelper->checkOut($ref, $tmpRepo, true, true); $this->fsHelper->remove($tmpRepo . '/.git'); $appDir = $tmpRepo . '/' . substr($this->appRoot, strlen($gitRoot)); if (!rename($appDir, $buildDir)) { throw new \RuntimeException(sprintf('Failed to move app from %s to %s', $appDir, $buildDir)); } $this->fsHelper->remove($tmpRepo); }
/** * Remove old files from a directory. * * @param string $directory * @param int $maxAge * @param int $keepMax * @param array $blacklist * @param bool $quiet * * @return int[] */ protected function cleanDirectory($directory, $maxAge = null, $keepMax = 5, array $blacklist = [], $quiet = true) { if (!is_dir($directory)) { return [0, 0]; } $files = glob($directory . '/*'); if (!$files) { return [0, 0]; } // Sort files by modified time (descending). usort($files, function ($a, $b) { return filemtime($a) < filemtime($b); }); $now = time(); $numDeleted = 0; $numKept = 0; foreach ($files as $filename) { if (in_array($filename, $blacklist)) { $numKept++; continue; } if ($keepMax !== null && $numKept >= $keepMax || $maxAge !== null && $now - filemtime($filename) > $maxAge) { if (!$quiet) { $this->output->writeln("Deleting: " . basename($filename)); } if ($this->fsHelper->remove($filename)) { $numDeleted++; } elseif (!$quiet) { $this->output->writeln("Failed to delete: <error>" . basename($filename) . "</error>"); } } else { $numKept++; } } return [$numDeleted, $numKept]; }
/** * Clean up old cache directories from previous versions. * * @todo remove in version 3 */ private function _deleteOldCaches() { if (self::$cleanedOldCaches) { return; } self::$cleanedOldCaches = true; $sessionsDir = $this->getSessionsDir(); if (!is_dir($sessionsDir)) { return; } foreach (scandir($sessionsDir) as $filename) { if (strpos($filename, 'sess-') !== 0) { continue; } $sessionDir = $sessionsDir . DIRECTORY_SEPARATOR . $filename; foreach (scandir($sessionDir) as $filename2) { if ($filename2[0] === '.' || strpos($filename2, '.json') !== false) { continue; } if (!isset($fs)) { $fs = new FilesystemHelper(); } $fs->remove($sessionDir . DIRECTORY_SEPARATOR . $filename2); } } }