Permalink
Browse files

Adding rewind functionality to entity bodies

  • Loading branch information...
1 parent 4ee3fb7 commit 9d5cb1a1e83a472677c7f32df252ab32ce767350 @mtdowling mtdowling committed Oct 30, 2012
Showing with 61 additions and 0 deletions.
  1. +18 −0 AbstractEntityBodyDecorator.php
  2. +32 −0 EntityBody.php
  3. +11 −0 EntityBodyInterface.php
@@ -48,6 +48,24 @@ public function __call($method, array $args = null)
* {@inheritdoc}
* @codeCoverageIgnore
*/
+ public function setRewindFunction($callable)
+ {
+ return $this->body->setRewindFunction($callable);
+ }
+
+ /**
+ * {@inheritdoc}
+ * @codeCoverageIgnore
+ */
+ public function rewind()
+ {
+ return $this->body->rewind();
+ }
+
+ /**
+ * {@inheritdoc}
+ * @codeCoverageIgnore
+ */
public function compress($filter = 'zlib.deflate')
{
return $this->body->compress($filter);
View
@@ -16,6 +16,11 @@ class EntityBody extends Stream implements EntityBodyInterface
protected $contentEncoding = false;
/**
+ * @var callable Method to invoke for rewinding a stream
+ */
+ protected $rewindFunction;
+
+ /**
* Create a new EntityBody based on the input type
*
* @param resource|string|EntityBody $resource Entity body data
@@ -48,6 +53,30 @@ public static function factory($resource = '', $size = null)
}
/**
+ * {@inheritdoc}
+ */
+ public function setRewindFunction($callable)
+ {
+ if (!is_callable($callable)) {
+ throw new InvalidArgumentException('Must specify a callable');
+ }
+
+ $this->rewindFunction = $callable;
+
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function rewind()
+ {
+ return $this->rewindFunction
+ ? call_user_func($this->rewindFunction, $this)
+ : parent::rewind();
+ }
+
+ /**
* Create a new EntityBody from a string
*
* @param string $string String of data
@@ -208,6 +237,9 @@ protected function handleCompression($filter, $offsetStart = 0)
$this->rebuildCache();
$this->seek(0);
+ // Remove any existing rewind function as the underlying stream has been replaced
+ $this->rewindFunction = null;
+
return true;
}
}
View
@@ -10,6 +10,17 @@
interface EntityBodyInterface extends StreamInterface
{
/**
+ * Specify a custom callback used to rewind a non-seekable stream. This can be useful entity enclosing requests
+ * that are redirected.
+ *
+ * @param mixed $callable Callable to invoke to rewind a non-seekable stream. The callback must accept an
+ * EntityBodyInterface object, perform the rewind if possible, and return a boolean
+ * representing whether or not the rewind was successful.
+ * @return self
+ */
+ public function setRewindFunction($callable);
+
+ /**
* If the stream is readable, compress the data in the stream using deflate compression. The uncompressed stream is
* then closed, and the compressed stream then becomes the wrapped stream.
*

0 comments on commit 9d5cb1a

Please sign in to comment.