mirror of
https://github.com/filp/whoops.git
synced 2026-09-18 13:46:27 +00:00
Move FrameIterator to FrameCollection
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* Whoops - php errors for cool kids
|
||||
* @author Filipe Dobreira <http://github.com/filp>
|
||||
*/
|
||||
|
||||
namespace Whoops\Exception;
|
||||
use Whoops\Exception\Frame;
|
||||
use IteratorAggregate;
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
/**
|
||||
* @var array[]
|
||||
*/
|
||||
private $frames;
|
||||
|
||||
/**
|
||||
* @param array $frames
|
||||
*/
|
||||
public function __construct(array $frames)
|
||||
{
|
||||
$this->frames = array_map(function($frame) {
|
||||
return new Frame($frame);
|
||||
}, $frames);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see IteratorAggregate::getIterator
|
||||
* @return Whoops\Exception\Frame[]
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return $this->frames;
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Whoops - php errors for cool kids
|
||||
* @author Filipe Dobreira <http://github.com/filp>
|
||||
*/
|
||||
|
||||
namespace Whoops\Exception;
|
||||
use Whoops\Exception\Frame;
|
||||
use Iterator;
|
||||
use Countable;
|
||||
|
||||
/**
|
||||
* Mostly just implements iterator methods, the only
|
||||
* notable aspects is that it is read-only, and instantiates
|
||||
* Frame objects on demand.
|
||||
*/
|
||||
class FrameIterator implements Iterator, Countable
|
||||
{
|
||||
/**
|
||||
* @var array[]
|
||||
*/
|
||||
private $frames;
|
||||
|
||||
/**
|
||||
* @return Whoops\Exception\Frame|false
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
$current = current($this->frames);
|
||||
|
||||
if($current === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!$current instanceof Frame) {
|
||||
$current = $this->frames[$this->key()] = new Frame($current);
|
||||
}
|
||||
|
||||
return $current;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array[]
|
||||
*/
|
||||
public function __construct(array $frames)
|
||||
{
|
||||
$this->frames = $frames;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return $this->current() !== false;
|
||||
}
|
||||
|
||||
public function next()
|
||||
{
|
||||
next($this->frames);
|
||||
}
|
||||
|
||||
public function rewind()
|
||||
{
|
||||
reset($this->frames);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return key($this->frames);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->frames);
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
namespace Whoops\Exception;
|
||||
use Whoops\Exception\FrameIterator;
|
||||
use Whoops\Exception\FrameCollection;
|
||||
use Whoops\Exception\ErrorException;
|
||||
use Exception;
|
||||
|
||||
@@ -17,9 +17,9 @@ class Inspector
|
||||
private $exception;
|
||||
|
||||
/**
|
||||
* @var Whoops\Exception\FrameIterator
|
||||
* @var Whoops\Exception\FrameCollection
|
||||
*/
|
||||
private $framesIterator;
|
||||
private $frames;
|
||||
|
||||
/**
|
||||
* @param Exception $exception The exception to inspect
|
||||
@@ -56,12 +56,12 @@ class Inspector
|
||||
/**
|
||||
* Returns an iterator for the inspected exception's
|
||||
* frames.
|
||||
* @return Whoops\Exception\FrameIterator
|
||||
* @return Whoops\Exception\FrameCollection
|
||||
*/
|
||||
public function getFrames()
|
||||
{
|
||||
if($this->framesIterator === null) {
|
||||
$frames = $this->exception->getTrace();
|
||||
if($this->frames === null) {
|
||||
$frames = $this->exception->getTrace();
|
||||
|
||||
// If we're handling an ErrorException thrown by Whoops,
|
||||
// get rid of the last frame, which matches the handleError method,
|
||||
@@ -74,10 +74,10 @@ class Inspector
|
||||
$firstFrame = $this->getFrameFromException($this->exception);
|
||||
array_unshift($frames, $firstFrame);
|
||||
}
|
||||
$this->framesIterator = new FrameIterator($frames);
|
||||
$this->frames = new FrameCollection($frames);
|
||||
}
|
||||
|
||||
return $this->framesIterator;
|
||||
return $this->frames;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -57,11 +57,11 @@ class InspectorTest extends TestCase
|
||||
/**
|
||||
* @covers Whoops\Exception\Inspector::getFrames
|
||||
*/
|
||||
public function testGetFramesReturnsIterator()
|
||||
public function testGetFramesReturnsCollection()
|
||||
{
|
||||
$exception = $this->getException();
|
||||
$inspector = $this->getInspectorInstance($exception);
|
||||
|
||||
$this->assertInstanceOf('Whoops\\Exception\\FrameIterator', $inspector->getFrames());
|
||||
$this->assertInstanceOf('Whoops\\Exception\\FrameCollection', $inspector->getFrames());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user