You are here

class vfsStreamFile in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/mikey179/vfsStream/src/main/php/org/bovigo/vfs/vfsStreamFile.php \org\bovigo\vfs\vfsStreamFile

File container.

@api

Hierarchy

Expanded class hierarchy of vfsStreamFile

5 files declare their use of vfsStreamFile
vfsStreamAbstractVisitorTestCase.php in vendor/mikey179/vfsStream/src/test/php/org/bovigo/vfs/visitor/vfsStreamAbstractVisitorTestCase.php
vfsStreamPrintVisitor.php in vendor/mikey179/vfsStream/src/main/php/org/bovigo/vfs/visitor/vfsStreamPrintVisitor.php
vfsStreamPrintVisitorTestCase.php in vendor/mikey179/vfsStream/src/test/php/org/bovigo/vfs/visitor/vfsStreamPrintVisitorTestCase.php
vfsStreamStructureVisitor.php in vendor/mikey179/vfsStream/src/main/php/org/bovigo/vfs/visitor/vfsStreamStructureVisitor.php
vfsStreamVisitor.php in vendor/mikey179/vfsStream/src/main/php/org/bovigo/vfs/visitor/vfsStreamVisitor.php

File

vendor/mikey179/vfsStream/src/main/php/org/bovigo/vfs/vfsStreamFile.php, line 18

Namespace

org\bovigo\vfs
View source
class vfsStreamFile extends vfsStreamAbstractContent {

  /**
   * content of the file
   *
   * @type  FileContent
   */
  private $content;

  /**
   * Resource id which exclusively locked this file
   *
   * @type  string
   */
  protected $exclusiveLock;

  /**
   * Resources ids which currently holds shared lock to this file
   *
   * @type  bool[string]
   */
  protected $sharedLock = array();

  /**
   * constructor
   *
   * @param  string  $name
   * @param  int     $permissions  optional
   */
  public function __construct($name, $permissions = null) {
    $this->content = new StringBasedFileContent(null);
    $this->type = vfsStreamContent::TYPE_FILE;
    parent::__construct($name, $permissions);
  }

  /**
   * returns default permissions for concrete implementation
   *
   * @return  int
   * @since   0.8.0
   */
  protected function getDefaultPermissions() {
    return 0666;
  }

  /**
   * checks whether the container can be applied to given name
   *
   * @param   string  $name
   * @return  bool
   */
  public function appliesTo($name) {
    return $name === $this->name;
  }

  /**
   * alias for withContent()
   *
   * @param   string  $content
   * @return  vfsStreamFile
   * @see     withContent()
   */
  public function setContent($content) {
    return $this
      ->withContent($content);
  }

  /**
   * sets the contents of the file
   *
   * Setting content with this method does not change the time when the file
   * was last modified.
   *
   * @param   string]FileContent  $content
   * @return  vfsStreamFile
   * @throws  \InvalidArgumentException
   */
  public function withContent($content) {
    if (is_string($content)) {
      $this->content = new StringBasedFileContent($content);
    }
    elseif ($content instanceof FileContent) {
      $this->content = $content;
    }
    else {
      throw new \InvalidArgumentException('Given content must either be a string or an instance of org\\bovigo\\vfs\\content\\FileContent');
    }
    return $this;
  }

  /**
   * returns the contents of the file
   *
   * Getting content does not change the time when the file
   * was last accessed.
   *
   * @return  string
   */
  public function getContent() {
    return $this->content
      ->content();
  }

  /**
   * simply open the file
   *
   * @since  0.9
   */
  public function open() {
    $this->content
      ->seek(0, SEEK_SET);
    $this->lastAccessed = time();
  }

  /**
   * open file and set pointer to end of file
   *
   * @since  0.9
   */
  public function openForAppend() {
    $this->content
      ->seek(0, SEEK_END);
    $this->lastAccessed = time();
  }

  /**
   * open file and truncate content
   *
   * @since  0.9
   */
  public function openWithTruncate() {
    $this
      ->open();
    $this->content
      ->truncate(0);
    $time = time();
    $this->lastAccessed = $time;
    $this->lastModified = $time;
  }

  /**
   * reads the given amount of bytes from content
   *
   * Using this method changes the time when the file was last accessed.
   *
   * @param   int     $count
   * @return  string
   */
  public function read($count) {
    $this->lastAccessed = time();
    return $this->content
      ->read($count);
  }

  /**
   * returns the content until its end from current offset
   *
   * Using this method changes the time when the file was last accessed.
   *
   * @return  string
   * @deprecated  since 1.3.0
   */
  public function readUntilEnd() {
    $this->lastAccessed = time();
    return $this->content
      ->readUntilEnd();
  }

  /**
   * writes an amount of data
   *
   * Using this method changes the time when the file was last modified.
   *
   * @param   string  $data
   * @return  amount of written bytes
   */
  public function write($data) {
    $this->lastModified = time();
    return $this->content
      ->write($data);
  }

  /**
   * Truncates a file to a given length
   *
   * @param   int  $size length to truncate file to
   * @return  bool
   * @since   1.1.0
   */
  public function truncate($size) {
    $this->content
      ->truncate($size);
    $this->lastModified = time();
    return true;
  }

  /**
   * checks whether pointer is at end of file
   *
   * @return  bool
   */
  public function eof() {
    return $this->content
      ->eof();
  }

  /**
   * returns the current position within the file
   *
   * @return  int
   * @deprecated  since 1.3.0
   */
  public function getBytesRead() {
    return $this->content
      ->bytesRead();
  }

  /**
   * seeks to the given offset
   *
   * @param   int   $offset
   * @param   int   $whence
   * @return  bool
   */
  public function seek($offset, $whence) {
    return $this->content
      ->seek($offset, $whence);
  }

  /**
   * returns size of content
   *
   * @return  int
   */
  public function size() {
    return $this->content
      ->size();
  }

  /**
   * locks file for
   *
   * @param   resource|vfsStreamWrapper $resource
   * @param   int  $operation
   * @return  bool
   * @since   0.10.0
   * @see     https://github.com/mikey179/vfsStream/issues/6
   * @see     https://github.com/mikey179/vfsStream/issues/40
   */
  public function lock($resource, $operation) {
    if ((LOCK_NB & $operation) == LOCK_NB) {
      $operation = $operation - LOCK_NB;
    }

    // call to lock file on the same file handler firstly releases the lock
    $this
      ->unlock($resource);
    if (LOCK_EX === $operation) {
      if ($this
        ->isLocked()) {
        return false;
      }
      $this
        ->setExclusiveLock($resource);
    }
    elseif (LOCK_SH === $operation) {
      if ($this
        ->hasExclusiveLock()) {
        return false;
      }
      $this
        ->addSharedLock($resource);
    }
    return true;
  }

  /**
   * Removes lock from file acquired by given resource
   *
   * @param   resource|vfsStreamWrapper $resource
   * @see     https://github.com/mikey179/vfsStream/issues/40
   */
  public function unlock($resource) {
    if ($this
      ->hasExclusiveLock($resource)) {
      $this->exclusiveLock = null;
    }
    if ($this
      ->hasSharedLock($resource)) {
      unset($this->sharedLock[$this
        ->getResourceId($resource)]);
    }
  }

  /**
   * Set exlusive lock on file by given resource
   *
   * @param   resource|vfsStreamWrapper $resource
   * @see     https://github.com/mikey179/vfsStream/issues/40
   */
  protected function setExclusiveLock($resource) {
    $this->exclusiveLock = $this
      ->getResourceId($resource);
  }

  /**
   * Add shared lock on file by given resource
   *
   * @param   resource|vfsStreamWrapper $resource
   * @see     https://github.com/mikey179/vfsStream/issues/40
   */
  protected function addSharedLock($resource) {
    $this->sharedLock[$this
      ->getResourceId($resource)] = true;
  }

  /**
   * checks whether file is locked
   *
   * @param   resource|vfsStreamWrapper $resource
   * @return  bool
   * @since   0.10.0
   * @see     https://github.com/mikey179/vfsStream/issues/6
   * @see     https://github.com/mikey179/vfsStream/issues/40
   */
  public function isLocked($resource = null) {
    return $this
      ->hasSharedLock($resource) || $this
      ->hasExclusiveLock($resource);
  }

  /**
   * checks whether file is locked in shared mode
   *
   * @param   resource|vfsStreamWrapper $resource
   * @return  bool
   * @since   0.10.0
   * @see     https://github.com/mikey179/vfsStream/issues/6
   * @see     https://github.com/mikey179/vfsStream/issues/40
   */
  public function hasSharedLock($resource = null) {
    if (null !== $resource) {
      return isset($this->sharedLock[$this
        ->getResourceId($resource)]);
    }
    return !empty($this->sharedLock);
  }

  /**
   * Returns unique resource id
   *
   * @param   resource|vfsStreamWrapper $resource
   * @return  string
   * @see     https://github.com/mikey179/vfsStream/issues/40
   */
  public function getResourceId($resource) {
    if (is_resource($resource)) {
      $data = stream_get_meta_data($resource);
      $resource = $data['wrapper_data'];
    }
    return spl_object_hash($resource);
  }

  /**
   * checks whether file is locked in exclusive mode
   *
   * @param   resource|vfsStreamWrapper $resource
   * @return  bool
   * @since   0.10.0
   * @see     https://github.com/mikey179/vfsStream/issues/6
   * @see     https://github.com/mikey179/vfsStream/issues/40
   */
  public function hasExclusiveLock($resource = null) {
    if (null !== $resource) {
      return $this->exclusiveLock === $this
        ->getResourceId($resource);
    }
    return null !== $this->exclusiveLock;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
vfsStreamAbstractContent::$group protected property owner group of the file
vfsStreamAbstractContent::$lastAccessed protected property timestamp of last access
vfsStreamAbstractContent::$lastAttributeModified protected property timestamp of last attribute modification
vfsStreamAbstractContent::$lastModified protected property timestamp of last modification
vfsStreamAbstractContent::$name protected property name of the container
vfsStreamAbstractContent::$parentPath private property path to to this content
vfsStreamAbstractContent::$permissions protected property permissions for content
vfsStreamAbstractContent::$type protected property type of the container
vfsStreamAbstractContent::$user protected property owner of the file
vfsStreamAbstractContent::at public function adds content to given container Overrides vfsStreamContent::at
vfsStreamAbstractContent::chgrp public function change owner group of file to given group Overrides vfsStreamContent::chgrp
vfsStreamAbstractContent::chmod public function change file mode to given permissions Overrides vfsStreamContent::chmod
vfsStreamAbstractContent::chown public function change owner of file to given user Overrides vfsStreamContent::chown
vfsStreamAbstractContent::fileatime public function returns the last access time of the stream content
vfsStreamAbstractContent::filectime public function returns the last attribute modification time of the stream content
vfsStreamAbstractContent::filemtime public function returns the last modification time of the stream content Overrides vfsStreamContent::filemtime
vfsStreamAbstractContent::getGroup public function returns owner group of file Overrides vfsStreamContent::getGroup
vfsStreamAbstractContent::getName public function returns the file name of the content Overrides vfsStreamContent::getName
vfsStreamAbstractContent::getPermissions public function returns permissions Overrides vfsStreamContent::getPermissions
vfsStreamAbstractContent::getType public function returns the type of the container Overrides vfsStreamContent::getType
vfsStreamAbstractContent::getUser public function returns owner of file Overrides vfsStreamContent::getUser
vfsStreamAbstractContent::isExecutable public function checks whether content is executable Overrides vfsStreamContent::isExecutable
vfsStreamAbstractContent::isOwnedByGroup public function checks whether file is owned by group Overrides vfsStreamContent::isOwnedByGroup
vfsStreamAbstractContent::isOwnedByUser public function checks whether file is owned by given user Overrides vfsStreamContent::isOwnedByUser
vfsStreamAbstractContent::isReadable public function checks whether content is readable Overrides vfsStreamContent::isReadable
vfsStreamAbstractContent::isWritable public function checks whether content is writable Overrides vfsStreamContent::isWritable
vfsStreamAbstractContent::lastAccessed public function sets last access time of the stream content
vfsStreamAbstractContent::lastAttributeModified public function sets the last attribute modification time of the stream content
vfsStreamAbstractContent::lastModified public function sets the last modification time of the stream content Overrides vfsStreamContent::lastModified
vfsStreamAbstractContent::path public function returns path to this content Overrides vfsStreamContent::path
vfsStreamAbstractContent::rename public function renames the content Overrides vfsStreamContent::rename 1
vfsStreamAbstractContent::setParentPath public function sets parent path Overrides vfsStreamContent::setParentPath 1
vfsStreamAbstractContent::url public function returns complete vfsStream url for this content Overrides vfsStreamContent::url
vfsStreamContent::TYPE_BLOCK constant stream content type: block
vfsStreamContent::TYPE_DIR constant stream content type: directory
vfsStreamContent::TYPE_FILE constant stream content type: file
vfsStreamFile::$content private property content of the file
vfsStreamFile::$exclusiveLock protected property Resource id which exclusively locked this file
vfsStreamFile::$sharedLock protected property Resources ids which currently holds shared lock to this file
vfsStreamFile::addSharedLock protected function Add shared lock on file by given resource
vfsStreamFile::appliesTo public function checks whether the container can be applied to given name Overrides vfsStreamAbstractContent::appliesTo
vfsStreamFile::eof public function checks whether pointer is at end of file
vfsStreamFile::getBytesRead Deprecated public function returns the current position within the file
vfsStreamFile::getContent public function returns the contents of the file
vfsStreamFile::getDefaultPermissions protected function returns default permissions for concrete implementation Overrides vfsStreamAbstractContent::getDefaultPermissions
vfsStreamFile::getResourceId public function Returns unique resource id
vfsStreamFile::hasExclusiveLock public function checks whether file is locked in exclusive mode
vfsStreamFile::hasSharedLock public function checks whether file is locked in shared mode
vfsStreamFile::isLocked public function checks whether file is locked
vfsStreamFile::lock public function locks file for
vfsStreamFile::open public function simply open the file
vfsStreamFile::openForAppend public function open file and set pointer to end of file
vfsStreamFile::openWithTruncate public function open file and truncate content
vfsStreamFile::read public function reads the given amount of bytes from content
vfsStreamFile::readUntilEnd Deprecated public function returns the content until its end from current offset
vfsStreamFile::seek public function seeks to the given offset
vfsStreamFile::setContent public function alias for withContent()
vfsStreamFile::setExclusiveLock protected function Set exlusive lock on file by given resource
vfsStreamFile::size public function returns size of content Overrides vfsStreamContent::size
vfsStreamFile::truncate public function Truncates a file to a given length
vfsStreamFile::unlock public function Removes lock from file acquired by given resource
vfsStreamFile::withContent public function sets the contents of the file
vfsStreamFile::write public function writes an amount of data
vfsStreamFile::__construct public function constructor Overrides vfsStreamAbstractContent::__construct 1