Permalink
Browse files

Decouple vfsStreamWrapper from vfsStreamFile

- stream id is not generated in vfsStreamWrapper anymore
- using $resource instead of $stream should make things much more clear
  • Loading branch information...
1 parent 9cf6041 commit 39605745e82a10a3225ae06d9296d6bbe73ad8df @arvenil arvenil committed Aug 28, 2012
Showing with 41 additions and 57 deletions.
  1. +41 −40 src/main/php/org/bovigo/vfs/vfsStreamFile.php
  2. +0 −17 src/main/php/org/bovigo/vfs/vfsStreamWrapper.php
View
81 src/main/php/org/bovigo/vfs/vfsStreamFile.php
@@ -28,13 +28,13 @@ class vfsStreamFile extends vfsStreamAbstractContent
*/
protected $bytes_read = 0;
/**
- * Stream id which exclusively locked this file
+ * Resource id which exclusively locked this file
*
* @type string
*/
protected $exclusiveLock;
/**
- * Stream ids which currently holds shared lock to this file
+ * Resources ids which currently holds shared lock to this file
*
* @type bool[string]
*/
@@ -278,134 +278,135 @@ public function size()
/**
* locks file for
*
- * @param resource|vfsStreamWrapper $stream
+ * @param resource|vfsStreamWrapper $resource
* @param int $operation
- * @return vfsStreamFile
+ * @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($stream, $operation)
+ 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($stream);
+ $this->unlock($resource);
if (LOCK_EX === $operation) {
if ($this->isLocked()) {
return false;
}
- $this->setExclusiveLock($stream);
+ $this->setExclusiveLock($resource);
} elseif(LOCK_SH === $operation) {
if ($this->hasExclusiveLock()) {
return false;
}
- $this->addSharedLock($stream);
+ $this->addSharedLock($resource);
}
return true;
}
/**
- * Removes lock from file acquired by given stream
+ * Removes lock from file acquired by given resource
*
- * @param resource|vfsStreamWrapper $stream
+ * @param resource|vfsStreamWrapper $resource
* @see https://github.com/mikey179/vfsStream/issues/40
*/
- public function unlock($stream) {
- if ($this->hasExclusiveLock($stream)) {
+ public function unlock($resource) {
+ if ($this->hasExclusiveLock($resource)) {
$this->exclusiveLock = null;
}
- if ($this->hasSharedLock($stream)) {
- unset($this->sharedLock[$this->getStreamId($stream)]);
+ if ($this->hasSharedLock($resource)) {
+ unset($this->sharedLock[$this->getResourceId($resource)]);
}
}
/**
- * Set exlusive lock on file by given stream
+ * Set exlusive lock on file by given resource
*
- * @param resource|vfsStreamWrapper $stream
+ * @param resource|vfsStreamWrapper $resource
* @see https://github.com/mikey179/vfsStream/issues/40
*/
- protected function setExclusiveLock($stream) {
- $this->exclusiveLock = $this->getStreamId($stream);
+ protected function setExclusiveLock($resource) {
+ $this->exclusiveLock = $this->getResourceId($resource);
}
/**
- * Add shared lock on file by given stream
+ * Add shared lock on file by given resource
*
- * @param resource|vfsStreamWrapper $stream
+ * @param resource|vfsStreamWrapper $resource
* @see https://github.com/mikey179/vfsStream/issues/40
*/
- protected function addSharedLock($stream) {
- $this->sharedLock[$this->getStreamId($stream)] = true;
+ protected function addSharedLock($resource) {
+ $this->sharedLock[$this->getResourceId($resource)] = true;
}
/**
* checks whether file is locked
*
- * @param resource|vfsStreamWrapper $stream
+ * @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($stream = null)
+ public function isLocked($resource = null)
{
- return $this->hasSharedLock($stream) || $this->hasExclusiveLock($stream);
+ return $this->hasSharedLock($resource) || $this->hasExclusiveLock($resource);
}
/**
* checks whether file is locked in shared mode
*
- * @param resource|vfsStreamWrapper $stream
+ * @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($stream = null)
+ public function hasSharedLock($resource = null)
{
- if (null !== $stream) {
- return isset($this->sharedLock[$this->getStreamId($stream)]);
+ if (null !== $resource) {
+ return isset($this->sharedLock[$this->getResourceId($resource)]);
}
return !empty($this->sharedLock);
}
/**
- * Returns unique streamId
+ * Returns unique resource id
*
- * @param resource|vfsStreamWrapper $stream
+ * @param resource|vfsStreamWrapper $resource
+ * @return string
* @see https://github.com/mikey179/vfsStream/issues/40
*/
- public function getStreamId($stream) {
- if (is_resource($stream)) {
- $data = stream_get_meta_data($stream);
- $stream = $data['wrapper_data'];
+ public function getResourceId($resource) {
+ if (is_resource($resource)) {
+ $data = stream_get_meta_data($resource);
+ $resource = $data['wrapper_data'];
}
- return $stream->getStreamId();
+ return spl_object_hash($resource);
}
/**
* checks whether file is locked in exclusive mode
*
- * @param resource|vfsStreamWrapper $stream
+ * @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($stream = null)
+ public function hasExclusiveLock($resource = null)
{
- if (null !== $stream) {
- return $this->exclusiveLock === $this->getStreamId($stream);
+ if (null !== $resource) {
+ return $this->exclusiveLock === $this->getResourceId($resource);
}
return null !== $this->exclusiveLock;
View
17 src/main/php/org/bovigo/vfs/vfsStreamWrapper.php
@@ -90,13 +90,6 @@ class vfsStreamWrapper
protected $dirIterator;
/**
- * Uniq identifier for this file handler
- *
- * @type string
- */
- protected $streamId;
-
- /**
* method to register the stream wrapper
*
* Please be aware that a call to this method will reset the root element
@@ -145,15 +138,6 @@ public static function getRoot()
}
/**
- * returns unique stream id
- *
- * @return int
- */
- public function getStreamId() {
- return $this->streamId;
- }
-
- /**
* sets quota for disk space
*
* @param Quota $quota
@@ -255,7 +239,6 @@ protected function resolvePath($path)
*/
public function stream_open($path, $mode, $options, $opened_path)
{
- $this->streamId = spl_object_hash($this);
$extended = ((strstr($mode, '+') !== false) ? (true) : (false));
$mode = str_replace(array('b', '+'), '', $mode);
if (in_array($mode, array('r', 'w', 'a', 'x', 'c')) === false) {

0 comments on commit 3960574

Please sign in to comment.