-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
903 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ivory Http Adapter package. | ||
* | ||
* (c) Eric GELOEN <geloen.eric@gmail.com> | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ivory\HttpAdapter\Event\History; | ||
|
||
use Ivory\HttpAdapter\Message\InternalRequestInterface; | ||
use Ivory\HttpAdapter\Message\ResponseInterface; | ||
|
||
/** | ||
* Journal. | ||
* | ||
* @author GeLo <geloen.eric@gmail.com> | ||
*/ | ||
class Journal implements \Countable, \IteratorAggregate | ||
{ | ||
/** @var integer */ | ||
protected $limit; | ||
|
||
/** @var array */ | ||
protected $entries = array(); | ||
|
||
/** | ||
* Creates a journal. | ||
* | ||
* @param integer $limit The limit. | ||
*/ | ||
public function __construct($limit = 10) | ||
{ | ||
$this->setLimit($limit); | ||
} | ||
|
||
/** | ||
* Gets the limit. | ||
* | ||
* @return integer The limit. | ||
*/ | ||
public function getLimit() | ||
{ | ||
return $this->limit; | ||
} | ||
|
||
/** | ||
* Sets the limit. | ||
* | ||
* @param integer $limit The limit. | ||
*/ | ||
public function setLimit($limit) | ||
{ | ||
$this->limit = $limit; | ||
} | ||
|
||
/** | ||
* Clears the journal. | ||
*/ | ||
public function clear() | ||
{ | ||
$this->entries = array(); | ||
} | ||
|
||
/** | ||
* Records an entry. | ||
* | ||
* @param \Ivory\HttpAdapter\Message\InternalRequestInterface $request The request. | ||
* @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response. | ||
* @param float $time The time. | ||
*/ | ||
public function record(InternalRequestInterface $request, ResponseInterface $response, $time) | ||
{ | ||
$this->addEntry(new JournalEntry($request, $response, $time)); | ||
} | ||
|
||
/** | ||
* Checks if there are entries. | ||
* | ||
* @return boolean TRUE if there are entries else FALSE. | ||
*/ | ||
public function hasEntries() | ||
{ | ||
return !empty($this->entries); | ||
} | ||
|
||
/** | ||
* Gets the entries. | ||
* | ||
* @return array The entries. | ||
*/ | ||
public function getEntries() | ||
{ | ||
return $this->entries; | ||
} | ||
|
||
/** | ||
* Gets the last entry. | ||
* | ||
* @return \Ivory\HttpAdapter\Event\History\JournalEntry|boolen The last entry or false if there is no entry. | ||
*/ | ||
public function getLastEntry() | ||
{ | ||
return end($this->entries); | ||
} | ||
|
||
/** | ||
* Adds an entry. | ||
* | ||
* @param \Ivory\HttpAdapter\Event\History\JournalEntry $entry The entry. | ||
*/ | ||
public function addEntry(JournalEntry $entry) | ||
{ | ||
$this->entries[] = $entry; | ||
$this->entries = array_slice($this->entries, $this->limit * -1); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function count() | ||
{ | ||
return count($this->entries); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getIterator() | ||
{ | ||
return new \ArrayIterator(array_reverse($this->entries)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ivory Http Adapter package. | ||
* | ||
* (c) Eric GELOEN <geloen.eric@gmail.com> | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ivory\HttpAdapter\Event\History; | ||
|
||
use Ivory\HttpAdapter\Message\InternalRequestInterface; | ||
use Ivory\HttpAdapter\Message\ResponseInterface; | ||
|
||
/** | ||
* Journal entry. | ||
* | ||
* @author GeLo <geloen.eric@gmail.com> | ||
*/ | ||
class JournalEntry | ||
{ | ||
/** @var \Ivory\HttpAdapter\Message\InternalRequestInterface */ | ||
protected $request; | ||
|
||
/** @var \Ivory\HttpAdapter\Message\ResponseInterface */ | ||
protected $response; | ||
|
||
/** @var float */ | ||
protected $time; | ||
|
||
/** | ||
* Creates a journal entry. | ||
* | ||
* @param \Ivory\HttpAdapter\Message\InternalRequestInterface $request The request. | ||
* @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response. | ||
* @param float $time The time. | ||
*/ | ||
public function __construct(InternalRequestInterface $request, ResponseInterface $response, $time) | ||
{ | ||
$this->request = $request; | ||
$this->response = $response; | ||
$this->time = $time; | ||
} | ||
|
||
/** | ||
* Gets the request. | ||
* | ||
* @return \Ivory\HttpAdapter\Message\InternalRequestInterface The request. | ||
*/ | ||
public function getRequest() | ||
{ | ||
return $this->request; | ||
} | ||
|
||
/** | ||
* Gets the response. | ||
* | ||
* @return \Ivory\HttpAdapter\Message\ResponseInterface The response. | ||
*/ | ||
public function getResponse() | ||
{ | ||
return $this->response; | ||
} | ||
|
||
/** | ||
* Gets the time. | ||
* | ||
* @return float The time. | ||
*/ | ||
public function getTime() | ||
{ | ||
return $this->time; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Ivory/HttpAdapter/Event/Subscriber/AbstractTimerSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ivory Http Adapter package. | ||
* | ||
* (c) Eric GELOEN <geloen.eric@gmail.com> | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ivory\HttpAdapter\Event\Subscriber; | ||
|
||
use Ivory\HttpAdapter\Event\Events; | ||
use Ivory\HttpAdapter\Event\PostSendEvent; | ||
use Ivory\HttpAdapter\Event\PreSendEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Abstract timer subscriber. | ||
* | ||
* @author GeLo <geloen.eric@gmail.com> | ||
*/ | ||
abstract class AbstractTimerSubscriber implements EventSubscriberInterface | ||
{ | ||
/** @var float */ | ||
protected $start; | ||
|
||
/** @var float */ | ||
protected $time; | ||
|
||
/** | ||
* On pre send event. | ||
* | ||
* @param \Ivory\HttpAdapter\Event\PreSendEvent $event The pre send event. | ||
*/ | ||
public function onPreSend(PreSendEvent $event) | ||
{ | ||
$this->start = microtime(true); | ||
} | ||
|
||
/** | ||
* On post send event. | ||
* | ||
* @param \Ivory\HttpAdapter\Event\PostSendEvent $event The post send event. | ||
*/ | ||
public function onPostSend(PostSendEvent $event) | ||
{ | ||
$this->time = microtime(true) - $this->start; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
Events::PRE_SEND => 'onPreSend', | ||
Events::POST_SEND => 'onPostSend', | ||
); | ||
} | ||
} |
Oops, something went wrong.