コード例 #1
0
 /**
  * @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();
 }
コード例 #2
0
ファイル: FileContents.php プロジェクト: corgi-php/corgi-file
 /**
  * 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());
 }
コード例 #3
0
ファイル: FileTest.php プロジェクト: corgi-php/corgi-file
 /**
  * @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));
 }
コード例 #4
0
 /**
  * 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;
 }
コード例 #5
0
ファイル: Directory.php プロジェクト: corgi-php/corgi-file
 /**
  * Deletes an empty directory
  * @return bool
  * @throws DeleteException
  */
 public function safeDelete()
 {
     if (!FileSystemHelper::isDirectory($this) || !rmdir($this)) {
         throw new DeleteException($this);
     }
     return true;
 }
コード例 #6
0
ファイル: File.php プロジェクト: corgi-php/corgi-file
 /**
  * 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;
 }
コード例 #7
0
 /**
  * @covers Corgi\File\Helpers\FileSystemHelper::toFullPath
  */
 public function testToFullPath()
 {
     $this->assertInternalType('string', FileSystemHelper::toFullPath('/arbitrary/file/path'));
 }
コード例 #8
0
 /**
  * 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;
 }