/** * @covers Corgi\File\ContentManagers\DirectoryContents::addSubDirectory */ public function testAddSubDirectory() { $subDirectory = new Directory($this->directory . '/test/test/'); $this->directory->contents()->addSubDirectory($subDirectory); $this->assertTrue(FileSystemHelper::isDirectory($subDirectory)); $subDirectory->delete(); }
/** * Returns file contents as string * @return string * @throws NotReadableException */ public function toString() { if (!FileSystemHelper::isReadableFile($this->getFile())) { throw new NotReadableException($this->getFile()); } return file_get_contents($this->getFile()); }
/** * @covers Corgi\File\File::copy * @depends testTouch */ public function testCopy() { $file = new File($this->fullTestFilePath); $file->touch(); $copyPath = $this->getCopyPath($file); $this->assertInstanceOf(get_class($file), $file->copy($copyPath)); $this->assertTrue(FileSystemHelper::isReadableFile($copyPath)); }
/** * Renames a file/directory and returns a new file/directory * Also moves the file/directory if necessary * @param string $newPath * @param bool|false $overwrite * @return bool * @throws MoveException * @throws OverwriteException */ public function rename($newPath, $overwrite = false) { if (!$overwrite && FileSystemHelper::doesExist($newPath)) { throw new OverwriteException($newPath); } $success = rename($this->getPath(), $newPath); if ($success) { $this->filePath = FileSystemHelper::toFullPath($newPath); } else { throw new MoveException($this->getPath(), $newPath); } return $success; }
/** * Deletes an empty directory * @return bool * @throws DeleteException */ public function safeDelete() { if (!FileSystemHelper::isDirectory($this) || !rmdir($this)) { throw new DeleteException($this); } return true; }
/** * Updates access date and time on a file * If the file does not exist, it gets created * @return File * @throws CreateDirectoryException * @throws NotWritableException */ public function touch() { $baseDir = new Directory(dirname($this)); if (!FileSystemHelper::isDirectory($baseDir)) { try { $baseDir->create(false); } catch (CreateDirectoryException $e) { throw $e; } } if (!FileSystemHelper::isWritable($baseDir)) { throw new NotWritableException($this); } touch($this); return $this; }
/** * @covers Corgi\File\Helpers\FileSystemHelper::toFullPath */ public function testToFullPath() { $this->assertInternalType('string', FileSystemHelper::toFullPath('/arbitrary/file/path')); }
/** * Returns directory contents as an array of FileSystemNode objects * @return array * @throws NotReadableException */ public function toArray() { if (!FileSystemHelper::isReadableDirectory($this->directory)) { throw new NotReadableException($this->directory); } $contents = new \stdClass(); $contents->data = []; /** * @param \SplFileInfo $currentItem */ $closure = function ($currentItem) use($contents) { $contents->data[] = $currentItem->isDir() ? new Directory($currentItem->getRealPath()) : new File($currentItem->getRealPath()); }; $this->getDirectory()->iterate($closure, \RecursiveIteratorIterator::CHILD_FIRST); return $contents->data; }