/** * @param string $path * @param string|null $contents * * @return FileNode */ protected function makeFile($path, $contents = null) { $file = new FileNode($this->filesystem, $path); if (!is_null($contents)) { $file->write($contents); } return $file; }
/** * Create the directory specified by the $file if it does not exist * * @param FileNode $file * @param string|null $visibility public or private visibility * * @return LocalFile The original file inputted * @throws MakeDirectoryFailedException */ public function makeDirectory(FileNode $file, $visibility = null) { $madeDirectory = $file->getFilesystem()->createDir($file->getDirectory(), ['visibility' => $visibility ?: static::VISIBILITY_PUBLIC]); if (!$madeDirectory) { throw new MakeDirectoryFailedException($file, error_get_last()['message']); } return $file; }
/** * @param FileNode $from * @param FileNode $to * * @return FileNode * @throws TransferFailedException */ public function moveTo(FileNode $from, FileNode $to) { $mountManager = new MountManager(['from' => $from->getFilesystem(), 'to' => $to->getFilesystem()]); $this->log(LogLevel::INFO, "Moving file from: '{from}', to: '{to}'", ['from' => $from, 'to' => $to]); if (!$mountManager->move('from://' . $from->getPath(), 'to://' . $to->getPath())) { throw new TransferFailedException($from, $to); } return $to; }
public function testWhenCreateDirReturnsFalseAnExceptionIsthrown() { $fileSystem = m::mock(FilesystemInterface::class); $directory = new FileNode($fileSystem, 'random/path'); $fileSystem->shouldReceive('createDir')->with($directory->getDirectory(), ['visibility' => 'public'])->andReturn(false); $this->expectException(MakedirectoryFailedException::class); $maker = new MakeDirectory(); $maker->makeDirectory($directory); }
public function testSetFileSystemUpdatesTheFileSystem() { $first = $this->getFilesystem(); $second = $this->getFilesystem(); $file = new FileNode($first, 'file/check'); $first->shouldReceive('has')->with('file/check')->once()->andReturn(true); static::assertTrue($file->getFilesystem()->has('file/check')); $second->shouldReceive('has')->with('file/check')->once()->andReturn(false); static::assertSame($file, $file->setFilesystem($second)); static::assertFalse($file->getFilesystem()->has('file/check')); }
/** * @param NodeInterface $node * * @return FileNode */ public function flow(NodeInterface $node) { if (!$node instanceof FileNode) { throw new InvalidArgumentException("Node: {$node} should be an instance of FileNode"); } if (substr($this->target->getPath(), -1) == '/') { $target = $this->target->getClone()->setPath($this->target->getPath() . $node->getFilename()); } else { $target = $this->target; } return $this->moveTo($node, $target); }
/** * {@inheritdoc} */ public function getContents() { if ($this->exists() && !in_array($this->getCompression(), [CompressionFactory::TYPE_NONE, CompressionFactory::TYPE_UNKNOWN])) { /** @var CompressionFactory $factory */ $factory = $this->getBuilder()->build(CompressionFactory::class); $compressor = $factory->getDeCompressor($this->getCompression()); $uncompressed = $compressor->decompress($this); $content = $uncompressed->getContents(); $uncompressed->delete(); return $content; } else { return parent::getContents(); } }
public function testMoveWhenFilesystemDoesNotReadStreamThrowsAnException() { $filesystem = m::mock(FilesystemInterface::class)->makePartial(); $fromFile = new FileNode($filesystem, 'some/file'); $toFile = new LocalFile(static::$dir . 'fail_move_file.text'); $filesystem->shouldReceive('readStream')->with($fromFile->getPath())->andReturn(false); $this->expectException(TransferFailedException::class); $this->transfer->moveTo($fromFile, $toFile); }