8000 Handling relative/absolute path · symfony/symfony@e174f37 · GitHub
[go: up one dir, main page]

Skip to content

Commit e174f37

Browse files
author
Anthony MARTIN
committed
Handling relative/absolute path
1 parent c1aab93 commit e174f37

File tree

2 files changed

+23
-65
lines changed

2 files changed

+23
-65
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,10 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
536536
$originDir = rtrim($originDir, '/\\');
537537
$originDirLen = \strlen($originDir);
538538

539+
if (!$this->exists($originDir)) {
540+
throw new IOException(sprintf('The origin directory specified "%s" was not found.', $originDir), 0, null, $originDir);
541+
}
542+
539543
// Iterate in destination folder to remove obsolete entries
540544
if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
541545
$deleteIterator = $iterator;
@@ -562,35 +566,24 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
562566
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
563567
}
564568

565-
if ($this->exists($originDir)) {
566-
$this->mkdir($targetDir);
567-
}
569+
$this->mkdir($targetDir);
570+
$targetDirInfo = new \SplFileInfo($targetDir);
568571

569572
foreach ($iterator as $file) {
570-
if ($file->getPathName() === $targetDir) {
573+
if ($file->getPathName() === $targetDir || $file->getRealPath() === $targetDir || 0 === strpos($file->getRealPath(), $targetDirInfo->getRealPath())) {
571574
continue;
572575
}
573576

574577
$target = $targetDir.substr($file->getPathname(), $originDirLen);
575578

576-
if ($copyOnWindows) {
577-
if (is_file($file)) {
578-
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
579-
} elseif (is_dir($file)) {
580-
$this->mkdir($target);
581-
} else {
582-
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
583-
}
579+
if (!$copyOnWindows && is_link($file)) {
580+
$this->symlink($file->getLinkTarget(), $target);
581+
} elseif (is_dir($file)) {
582+
$this->mkdir($target);
583+
} elseif (is_file($file)) {
584+
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
584585
} else {
585-
if (is_link($file)) {
586-
$this->symlink($file->getLinkTarget(), $target);
587-
} elseif (is_dir($file)) {
588-
$this->mkdir($target);
589-
} elseif (is_file($file)) {
590-
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
591-
} else {
592-
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
593-
}
586+
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
594587
}
595588
}
596589
}

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,46 +1348,6 @@ public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption()
13481348
$this->assertFileNotExists($targetPath.'target');
13491349
}
13501350

1351-
public function testMirrorWithCustomIterator()
1352-
{
1353-
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
1354-
mkdir($sourcePath);
1355-
1356-
$file = $sourcePath.\DIRECTORY_SEPARATOR.'file';
1357-
file_put_contents($file, 'FILE');
1358-
1359-
$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
1360-
1361-
$splFile = new \SplFileInfo($file);
1362-
$iterator = new \ArrayObject([$splFile]);
1363-
1364-
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
1365-
1366-
$this->assertTrue(is_dir($targetPath));
1367-
$this->assertFileEquals($file, $targetPath.\DIRECTORY_SEPARATOR.'file');
1368-
}
1369-
1370-
/**
1371-
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
1372-
* @expectedExceptionMessageRegExp /Unable to mirror "(.*)" directory/
1373-
*/
1374-
public function testMirrorWithCustomIteratorWithRelativePath()
1375-
{
1376-
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
1377-
$realSourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
1378-
mkdir($realSourcePath);
1379-
1380-
$file = $realSourcePath.'file';
1381-
file_put_contents($file, 'FILE');
1382-
1383-
$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
1384-
1385-
$splFile = new \SplFileInfo($file);
1386-
$iterator = new \ArrayObject([$splFile]);
1387-
1388-
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
1389-
}
1390-
13911351
public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory()
13921352
{
13931353
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
@@ -1402,15 +1362,20 @@ public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory()
14021362

14031363
$targetPath = $sourcePath.'target'.\DIRECTORY_SEPARATOR;
14041364

1405-
$this->filesystem->mirror($sourcePath, $targetPath);
1365+
if ('\\' !== \DIRECTORY_SEPARATOR) {
1366+
$this->filesystem->symlink($targetPath, $sourcePath . 'target_simlink');
1367+
}
14061368

1407-
$this->assertTrue(is_dir($targetPath));
1408-
$this->assertTrue(is_dir($targetPath.'directory'));
1369+
$this->filesystem->mirror($sourcePath, $targetPath, null, ['delete' => true]);
1370+
1371+
$this->assertTrue($this->filesystem->exists($targetPath));
1372+
$this->assertTrue($this->filesystem->exists($targetPath.'directory'));
14091373

14101374
$this->assertFileEquals($file1, $targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1');
14111375
$this->assertFileEquals($file2, $targetPath.'file2');
14121376

1413-
$this->assertFileNotExists($targetPath.'target');
1377+
$this->assertFalse($this->filesystem->exists($targetPath.'target_simlink'));
1378+
$this->assertFalse($this->filesystem->exists($targetPath.'target'));
14141379
}
14151380

14161381
/**

0 commit comments

Comments
 (0)
0