Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1e1a9e
commit bb05e0a
Showing
1 changed file
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PhpTuf\ComposerStager\Internal\FileSyncer\Service; | ||
|
||
use PhpTuf\ComposerStager\API\Environment\Service\EnvironmentInterface; | ||
use PhpTuf\ComposerStager\API\Exception\LogicException; | ||
use PhpTuf\ComposerStager\API\FileSyncer\Service\FileSyncerInterface; | ||
use PhpTuf\ComposerStager\API\Filesystem\Service\FilesystemInterface; | ||
use PhpTuf\ComposerStager\API\Finder\Service\ExecutableFinderInterface; | ||
use PhpTuf\ComposerStager\API\Path\Value\PathInterface; | ||
use PhpTuf\ComposerStager\API\Path\Value\PathListInterface; | ||
use PhpTuf\ComposerStager\API\Process\Service\OutputCallbackInterface; | ||
use PhpTuf\ComposerStager\API\Process\Service\ProcessInterface; | ||
use PhpTuf\ComposerStager\API\Process\Service\RsyncProcessRunnerInterface; | ||
use PhpTuf\ComposerStager\API\Translation\Factory\TranslatableFactoryInterface; | ||
use PhpTuf\ComposerStager\Internal\Helper\PathHelper; | ||
use PhpTuf\ComposerStager\Internal\Path\Factory\PathFactory; | ||
use PhpTuf\ComposerStager\Internal\Path\Value\PathList; | ||
use PhpTuf\ComposerStager\Internal\Translation\Factory\TranslatableAwareTrait; | ||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* @package FileSyncer | ||
* | ||
* @internal Don't depend directly on this class. It may be changed or removed at any time without notice. | ||
*/ | ||
final class RobocopyFileSyncer | ||
{ | ||
use TranslatableAwareTrait; | ||
|
||
public function __construct( | ||
private readonly EnvironmentInterface $environment, | ||
private readonly ExecutableFinderInterface $executableFinder, | ||
private readonly FilesystemInterface $filesystem, | ||
private readonly RsyncProcessRunnerInterface $rsync, | ||
TranslatableFactoryInterface $translatableFactory, | ||
) { | ||
$this->setTranslatableFactory($translatableFactory); | ||
} | ||
|
||
public function sync( | ||
PathInterface $source, | ||
PathInterface $destination, | ||
?PathListInterface $exclusions = null, | ||
?OutputCallbackInterface $callback = null, | ||
int $timeout = ProcessInterface::DEFAULT_TIMEOUT, | ||
): void { | ||
$this->environment->setTimeLimit($timeout); | ||
|
||
$exclusions ??= new PathList(); | ||
|
||
$this->assertRobocopyIsAvailable(); | ||
Check failure on line 52 in src/Internal/FileSyncer/Service/RobocopyFileSyncer.php GitHub Actions / Static Analysis
|
||
$this->assertSourceAndDestinationAreDifferent($source, $destination); | ||
Check failure on line 53 in src/Internal/FileSyncer/Service/RobocopyFileSyncer.php GitHub Actions / Static Analysis
|
||
$this->assertSourceExists($source); | ||
Check failure on line 54 in src/Internal/FileSyncer/Service/RobocopyFileSyncer.php GitHub Actions / Static Analysis
|
||
|
||
$this->runCommand($exclusions, $source->absolute(), $destination->absolute(), $destination, $callback); | ||
Check failure on line 56 in src/Internal/FileSyncer/Service/RobocopyFileSyncer.php GitHub Actions / Static Analysis
|
||
} | ||
|
||
/** @throws \PhpTuf\ComposerStager\API\Exception\LogicException */ | ||
private function assertRobocopyIsAvailable(): void | ||
{ | ||
$this->executableFinder->find('robocopy'); | ||
} | ||
|
||
/** @throws \PhpTuf\ComposerStager\API\Exception\LogicException */ | ||
private function assertSourceAndDestinationAreDifferent(PathInterface $source, PathInterface $destination): void | ||
{ | ||
if ($source->absolute() === $destination->absolute()) { | ||
throw new LogicException( | ||
$this->t( | ||
'The source and destination directories cannot be the same at %path', | ||
$this->p(['%path' => $source->absolute()]), | ||
$this->d()->exceptions(), | ||
), | ||
); | ||
} | ||
} | ||
|
||
/** @throws \PhpTuf\ComposerStager\API\Exception\LogicException */ | ||
private function assertSourceExists(PathInterface $source): void | ||
{ | ||
if (!$this->filesystem->fileExists($source)) { | ||
throw new LogicException($this->t( | ||
'The source directory does not exist at %path', | ||
$this->p(['%path' => $source->absolute()]), | ||
$this->d()->exceptions(), | ||
)); | ||
} | ||
|
||
if (!$this->filesystem->isDir($source)) { | ||
throw new LogicException($this->t( | ||
'The source directory is not actually a directory at %path', | ||
$this->p(['%path' => $source->absolute()]), | ||
$this->d()->exceptions(), | ||
)); | ||
} | ||
} | ||
|
||
/** @throws \PhpTuf\ComposerStager\API\Exception\IOException */ | ||
private function runCommand( | ||
?PathListInterface $exclusions, | ||
string $sourceAbsolute, | ||
string $destinationAbsolute, | ||
PathInterface $destination, | ||
?OutputCallbackInterface $callback, | ||
): void { | ||
$sourceAbsolute = PathHelper::canonicalize($sourceAbsolute); | ||
$destinationAbsolute = PathHelper::canonicalize($destinationAbsolute); | ||
|
||
$this->ensureDestinationDirectoryExists($destination); | ||
$command = $this->buildCommand($exclusions, $sourceAbsolute, $destinationAbsolute); | ||
|
||
$process = new Process($command); | ||
|
||
$process->run(); | ||
} | ||
|
||
/** | ||
* Ensures that the destination directory exists. This has no effect if it already does. | ||
* | ||
* @throws \PhpTuf\ComposerStager\API\Exception\IOException | ||
*/ | ||
private function ensureDestinationDirectoryExists(PathInterface $destination): void | ||
{ | ||
$this->filesystem->mkdir($destination); | ||
} | ||
|
||
/** @return array<string> */ | ||
private function buildCommand( | ||
?PathListInterface $exclusions, | ||
string $sourceAbsolute, | ||
string $destinationAbsolute, | ||
): array { | ||
$exclusions ??= new PathList(); | ||
/** @noinspection CallableParameterUseCaseInTypeContextInspection */ | ||
$exclusions = $exclusions->getAll(); | ||
|
||
$command = [ | ||
'robocopy', | ||
$sourceAbsolute, | ||
$destinationAbsolute, | ||
'/MIR', | ||
]; | ||
|
||
// Prevent infinite recursion if the source is inside the destination. | ||
if ($this->isDescendant($sourceAbsolute, $destinationAbsolute)) { | ||
$exclusions[] = self::getRelativePath($destinationAbsolute, $sourceAbsolute); | ||
} | ||
|
||
$pathFactory = new PathFactory(); | ||
|
||
if ($exclusions !== []) { | ||
$command[] = '/XD'; | ||
$command[] = implode(' ', $exclusions); | ||
$command[] = '/XF'; | ||
$command[] = implode(' ', $exclusions); | ||
} | ||
|
||
return $command; | ||
} | ||
|
||
private function isDescendant(string $descendant, string $ancestor): bool | ||
{ | ||
$ancestor .= DIRECTORY_SEPARATOR; | ||
|
||
return str_starts_with($descendant, $ancestor); | ||
} | ||
|
||
private static function getRelativePath(string $ancestor, string $path): string | ||
{ | ||
$ancestor .= DIRECTORY_SEPARATOR; | ||
|
||
if (str_starts_with($path, $ancestor)) { | ||
return substr($path, strlen($ancestor)); | ||
} | ||
|
||
return $path; | ||
} | ||
} |