Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,10 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
}

foreach ($iterator as $file) {
if (false === strpos($file->getPath(), $originDir)) {
throw new IOException(sprintf('Unable to mirror "%s" directory. If the origin directory is relative, try using "realpath" before calling the mirror method.', $originDir), 0, null, $originDir);
}

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

if ($copyOnWindows) {
Expand Down
40 changes: 40 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,46 @@ public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption()
$this->assertFileNotExists($targetPath.'target');
}

public function testMirrorWithCustomIterator()
{
$sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
mkdir($sourcePath);

$file = $sourcePath.DIRECTORY_SEPARATOR.'file';
file_put_contents($file, 'FILE');

$targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;

$splFile = new \SplFileInfo($file);
$iterator = new \ArrayObject(array($splFile));

$this->filesystem->mirror($sourcePath, $targetPath, $iterator);

$this->assertTrue(is_dir($targetPath));
$this->assertFileEquals($file, $targetPath.DIRECTORY_SEPARATOR.'file');
}

/**
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
* @expectedExceptionMessageRegExp /Unable to mirror "(.*)" directory/
*/
public function testMirrorWithCustomIteratorWithRelativePath()
{
$sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
$realSourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
mkdir($realSourcePath);

$file = $realSourcePath.'file';
file_put_contents($file, 'FILE');

$targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;

$splFile = new \SplFileInfo($file);
$iterator = new \ArrayObject(array($splFile));

$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
}

/**
* @dataProvider providePathsForIsAbsolutePath
*/
Expand Down