/** * {@inheritdoc} */ public function getFile($id) { $file_upload = \Drupal::request()->files->get("files[{$id}]", NULL, TRUE); // Make sure there's an upload to process. if (!empty($file_upload)) { $out = new ReadableStreamBackupFile($file_upload->getRealPath()); $out->setFullName($file_upload->getClientOriginalName()); return $out; } }
/** * Update the file time and size when the file is closed. */ function close() { parent::close(); // If the file has been modified, update the stats from disk. if ($this->dirty) { $this->_loadFileStats(); $this->dirty = FALSE; } }
/** * @covers ::open * @covers ::isOpen * @covers ::read * @covers ::close */ public function testOpenForRead() { // Not open yet $this->assertFalse($this->file->isOpen()); // Open for reading. $handle = $this->file->openForRead(); // Read a limited number of bytes $this->assertEquals($this->file->readBytes(5), 'Hello'); // Skip ', ' $this->file->readBytes(2); $this->assertEquals($this->file->readBytes(6), 'World!'); $this->assertEmpty($this->file->readLine()); $this->assertEmpty($this->file->readBytes(100)); // Reset the file handle $this->file->rewind(); // Read the entire file $this->assertEquals($this->file->readLine(), 'Hello, World!'); // Read all resets automatically $this->assertEquals($this->file->readAll(), 'Hello, World!'); $this->assertEquals($this->file->readAll(), 'Hello, World!'); // Close the file again. $this->file->close(); $this->assertFalse($this->file->isOpen()); $this->assertFalse(is_resource($handle)); // Test implicit file open and close. $new_file = new ReadableStreamBackupFile($this->fileURI); $this->assertEquals('Hello, World!', $new_file->readAll()); unset($new_file); // Not sure how to test that the handle has been closed since we don't get direct access to it. // Multiline file read: $message = "First Line\nSecond Line"; vfsStream::create(['multiline.txt' => "First Line\nSecond Line"]); $file = new ReadableStreamBackupFile('vfs://dir/multiline.txt'); $this->assertEquals($message, $file->readAll()); $file->rewind(); $this->assertEquals('First Line', trim($file->readLine())); $this->assertEquals('Second Line', $file->readLine()); }