mirror of
https://github.com/filp/whoops.git
synced 2026-08-30 12:07:25 +00:00
Implement serializable interface for FrameCollection and Frame objects
This commit is contained in:
@@ -6,8 +6,9 @@
|
||||
|
||||
namespace Whoops\Exception;
|
||||
use InvalidArgumentException;
|
||||
use Serializable;
|
||||
|
||||
class Frame
|
||||
class Frame implements Serializable
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
@@ -134,6 +135,17 @@ class Frame
|
||||
return $comments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the array containing the raw frame data from which
|
||||
* this Frame object was built
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRawFrame()
|
||||
{
|
||||
return $this->frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of the file for this frame as an
|
||||
* array of lines, and optionally as a clamped range of lines.
|
||||
@@ -177,4 +189,40 @@ class Frame
|
||||
return $lines;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements the Serializable interface, with special
|
||||
* steps to also save the existing comments.
|
||||
*
|
||||
* @see Serializable::serialize
|
||||
* @return string
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
$frames = $this->frames;
|
||||
if(!empty($this->comments)) {
|
||||
$frames['_comments'] = $this->comments;
|
||||
}
|
||||
|
||||
return serialize($frames);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unserializes the frame data, while also preserving
|
||||
* any existing comment data.
|
||||
*
|
||||
* @see Serializable::unserialize
|
||||
* @param string $serializedFrame
|
||||
*/
|
||||
public function unserialize($serializedFrame)
|
||||
{
|
||||
$frames = unserialize($serializedFrame);
|
||||
|
||||
if(!empty($frames['_comments'])) {
|
||||
$this->comments = $frames['_comments'];
|
||||
unset($frames['_comments']);
|
||||
}
|
||||
|
||||
$this->frames = $frames;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,13 +7,15 @@
|
||||
namespace Whoops\Exception;
|
||||
use Whoops\Exception\Frame;
|
||||
use IteratorAggregate;
|
||||
use ArrayIterator;
|
||||
use Serializable;
|
||||
|
||||
/**
|
||||
* Mostly just implements iterator methods, the only
|
||||
* notable aspects is that it is read-only, and instantiates
|
||||
* Frame objects on demand.
|
||||
*/
|
||||
class FrameCollection implements IteratorAggregate
|
||||
class FrameCollection implements IteratorAggregate, Serializable
|
||||
{
|
||||
/**
|
||||
* @var array[]
|
||||
@@ -32,10 +34,28 @@ class FrameCollection implements IteratorAggregate
|
||||
|
||||
/**
|
||||
* @see IteratorAggregate::getIterator
|
||||
* @return Whoops\Exception\Frame[]
|
||||
* @return ArrayIterator
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return $this->frames;
|
||||
return new ArrayIterator($this->frames);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Serializable::serialize
|
||||
* @return string
|
||||
*/
|
||||
public function serialize()
|
||||
{
|
||||
return serialize($this->frames);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Serializable::unserialize
|
||||
* @param string $serializedFrames
|
||||
*/
|
||||
public function unserialize($serializedFrames)
|
||||
{
|
||||
$this->frames = unserialize($serializedFrames);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user