Skip to content

Commit

Permalink
Add Symfony2 stopwatch support
Browse files Browse the repository at this point in the history
  • Loading branch information
GeLoLabs committed Nov 11, 2014
1 parent 723ec15 commit e4f6efb
Show file tree
Hide file tree
Showing 13 changed files with 1,177 additions and 149 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"nategood/httpful": "Allows you to use the httpful adapter",
"zendframework/zendframework1": "Allows you to use the Zend 1 adapter",
"zendframework/zend-http": "Allows you to use the Zend 2 adapter",
"psr/log": "Allows you to use the logger event subscriber"
"psr/log": "Allows you to use the logger event subscriber",
"symfony/stopwatch": "Allows you to use the stopwatch event subscriber"
},
"autoload": {
"psr-4": { "Ivory\\HttpAdapter\\": "src/" }
Expand Down
20 changes: 20 additions & 0 deletions doc/adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,23 @@ $zend2HttpAdapter = new Zend2HttpAdapter();
// or
$zend2HttpAdapter = new Zend2HttpAdapter(new Client());
```

## Stopwatch

The stopwatch http adapter allows you to time the http adaper process (including subscribers, etc) through the Symfony2
stopwatch component.

``` php
use Ivory\HttpAdapter\CurlHttpAdapter;
use Ivory\HttpAdapter\SocketHttpAdapter;
use Ivory\HttpAdapter\StopwatchHttpAdapter;
use Symfony\Component\Stopwatch\Stopwatch;

$httpAdapter = new CurlHttpAdapter();
// or
$httpAdapter = new SocketHttpAdpater();

$stopwatch = new Stopwatch();

$stopwatchHttpAdapter = new StopwatchHttpAdapter($httpAdapter, $stopwatch);
```
23 changes: 23 additions & 0 deletions doc/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -750,12 +750,35 @@ $time = $entry->getTime();
$entry->setTime($time);
```

### Stopwatch

The stopwatch subscriber is defined by the `Ivory\HttpAdapter\Event\Subscriber\StopwatchSubscriber` and allows you to
time the http adapter sending through the Symfony2 Stopwatch component. To use it:

``` php
use Ivory\HttpAdapter\Event\Subscriber\StopwatchSubscriber;
use Symfony\Component\Stopwatch\Stopwatch;

$stopwatch = new Stopwatch();
$stopwatchSubscriber = new StopwatchSubscriber($stopwatch);

$httpAdapter->getEventDispatcher()->addSubscriber($stopwatchSubscriber);
```

You can also change the stopwatch at runtime:

``` php
$stopwatch = $stopwatchSubscriber->getStopwatch();
$stopwatchSubscriber->setStopwatch($stopwatch);
```

## Event Subscriber Priorities

All event subscribers can work together (thanks to the event priorities). Here, the summary:

| Event Subscriber | Pre Send Event | Post Send Event | Exception Event |
| ---------------- | :------------: | :-------------: | :-------------: |
| Stopwatch | 10000 | 10000 | 10000 |
| Basic Auth | 300 | - | - |
| Cookie | 300 | 300 | - |
| Status Code | - | 200 | - |
Expand Down
66 changes: 1 addition & 65 deletions src/AbstractHttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @author GeLo <geloen.eric@gmail.com>
*/
abstract class AbstractHttpAdapter implements HttpAdapterInterface
abstract class AbstractHttpAdapter extends AbstractHttpAdapterTemplate
{
/** @var \Ivory\HttpAdapter\ConfigurationInterface */
private $configuration;
Expand Down Expand Up @@ -55,70 +55,6 @@ public function setConfiguration(ConfigurationInterface $configuration)
$this->configuration = $configuration;
}

/**
* {@inheritdoc}
*/
public function get($url, array $headers = array())
{
return $this->send($url, InternalRequestInterface::METHOD_GET, $headers);
}

/**
* {@inheritdoc}
*/
public function head($url, array $headers = array())
{
return $this->send($url, InternalRequestInterface::METHOD_HEAD, $headers);
}

/**
* {@inheritdoc}
*/
public function trace($url, array $headers = array())
{
return $this->send($url, InternalRequestInterface::METHOD_TRACE, $headers);
}

/**
* {@inheritdoc}
*/
public function post($url, array $headers = array(), $datas = array(), array $files = array())
{
return $this->send($url, InternalRequestInterface::METHOD_POST, $headers, $datas, $files);
}

/**
* {@inheritdoc}
*/
public function put($url, array $headers = array(), $datas = array(), array $files = array())
{
return $this->send($url, InternalRequestInterface::METHOD_PUT, $headers, $datas, $files);
}

/**
* {@inheritdoc}
*/
public function patch($url, array $headers = array(), $datas = array(), array $files = array())
{
return $this->send($url, InternalRequestInterface::METHOD_PATCH, $headers, $datas, $files);
}

/**
* {@inheritdoc}
*/
public function delete($url, array $headers = array(), $datas = array(), array $files = array())
{
return $this->send($url, InternalRequestInterface::METHOD_DELETE, $headers, $datas, $files);
}

/**
* {@inheritdoc}
*/
public function options($url, array $headers = array(), $datas = array(), array $files = array())
{
return $this->send($url, InternalRequestInterface::METHOD_OPTIONS, $headers, $datas, $files);
}

/**
* {@inheritdoc}
*/
Expand Down
86 changes: 86 additions & 0 deletions src/AbstractHttpAdapterTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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;

use Ivory\HttpAdapter\Message\RequestInterface;

/**
* Abstract http adapter template.
*
* @author GeLo <geloen.eric@gmail.com>
*/
abstract class AbstractHttpAdapterTemplate implements HttpAdapterInterface
{
/**
* {@inheritdoc}
*/
public function get($url, array $headers = array())
{
return $this->send($url, RequestInterface::METHOD_GET, $headers);
}

/**
* {@inheritdoc}
*/
public function head($url, array $headers = array())
{
return $this->send($url, RequestInterface::METHOD_HEAD, $headers);
}

/**
* {@inheritdoc}
*/
public function trace($url, array $headers = array())
{
return $this->send($url, RequestInterface::METHOD_TRACE, $headers);
}

/**
* {@inheritdoc}
*/
public function post($url, array $headers = array(), $datas = array(), array $files = array())
{
return $this->send($url, RequestInterface::METHOD_POST, $headers, $datas, $files);
}

/**
* {@inheritdoc}
*/
public function put($url, array $headers = array(), $datas = array(), array $files = array())
{
return $this->send($url, RequestInterface::METHOD_PUT, $headers, $datas, $files);
}

/**
* {@inheritdoc}
*/
public function patch($url, array $headers = array(), $datas = array(), array $files = array())
{
return $this->send($url, RequestInterface::METHOD_PATCH, $headers, $datas, $files);
}

/**
* {@inheritdoc}
*/
public function delete($url, array $headers = array(), $datas = array(), array $files = array())
{
return $this->send($url, RequestInterface::METHOD_DELETE, $headers, $datas, $files);
}

/**
* {@inheritdoc}
*/
public function options($url, array $headers = array(), $datas = array(), array $files = array())
{
return $this->send($url, RequestInterface::METHOD_OPTIONS, $headers, $datas, $files);
}
}
117 changes: 117 additions & 0 deletions src/Event/Subscriber/AbstractDebuggerSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?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\ExceptionEvent;
use Ivory\HttpAdapter\Event\PostSendEvent;
use Ivory\HttpAdapter\HttpAdapterException;
use Ivory\HttpAdapter\Message\InternalRequestInterface;
use Ivory\HttpAdapter\Message\ResponseInterface;

/**
* Abstract debugger subscriber.
*
* @author GeLo <geloen.eric@gmail.com>
*/
abstract class AbstractDebuggerSubscriber extends AbstractTimerSubscriber
{
/**
* On post send event.
*
* @param \Ivory\HttpAdapter\Event\PostSendEvent $event The post send event.
*
* @return array The formatted event.
*/
public function onPostSend(PostSendEvent $event)
{
return array(
'time' => parent::onPostSend($event),
'adapter' => $event->getHttpAdapter()->getName(),
'request' => $this->formatRequest($event->getRequest()),
'response' => $this->formatResponse($event->getResponse()),
);
}

/**
* On exception event.
*
* @param \Ivory\HttpAdapter\Event\ExceptionEvent $event The exception event.
*
* @return array The formatted event.
*/
public function onException(ExceptionEvent $event)
{
return array(
'time' => parent::onException($event),
'adapter' => $event->getHttpAdapter()->getName(),
'request' => $this->formatRequest($event->getRequest()),
'exception' => $this->formatException($event->getException()),
);
}

/**
* Formats the request.
*
* @param \Ivory\HttpAdapter\Message\InternalRequestInterface $request The request.
*
* @return array The formatted request.
*/
private function formatRequest(InternalRequestInterface $request)
{
return array(
'protocol_version' => $request->getProtocolVersion(),
'url' => (string) $request->getUrl(),
'method' => $request->getMethod(),
'headers' => $request->getHeaders(),
'raw_datas' => $request->getRawDatas(),
'datas' => $request->getDatas(),
'files' => $request->getFiles(),
'parameters' => $request->getParameters(),
);
}

/**
* Formats the response.
*
* @param \Ivory\HttpAdapter\Message\ResponseInterface $response The response.
*
* @return array The formatted response.
*/
private function formatResponse(ResponseInterface $response)
{
return array(
'protocol_version' => $response->getProtocolVersion(),
'status_code' => $response->getStatusCode(),
'reason_phrase' => $response->getReasonPhrase(),
'headers' => $response->getHeaders(),
'body' => (string) $response->getBody(),
'parameters' => $response->getParameters(),
);
}

/**
* Formats the exception.
*
* @param \Ivory\HttpAdapter\HttpAdapterException $exception The exception.
*
* @return array The formatted exception.
*/
private function formatException(HttpAdapterException $exception)
{
return array(
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'line' => $exception->getLine(),
'file' => $exception->getFile(),
);
}
}

0 comments on commit e4f6efb

Please sign in to comment.