Skip to content

Commit

Permalink
drafting event system and debugger to get look around requests and re…
Browse files Browse the repository at this point in the history
…sponses
  • Loading branch information
rafrsr committed Jun 29, 2016
1 parent 97b7f35 commit 0065498
Show file tree
Hide file tree
Showing 8 changed files with 495 additions and 3 deletions.
70 changes: 70 additions & 0 deletions src/Debug/ApiDebugger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* This file is part of the GenericApi package.
*
* (c) RafaelSR <https://github.com/rafrsr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Rafrsr\GenericApi\Debug;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class ApiDebugger
{
/**
* @var RequestProcessStack
*/
protected $requestStack;

/**
* makeRequestProcess
*
* @param RequestInterface $request
*
* @return RequestProcess
*/
public function beginRequestProcess(RequestInterface $request)
{
return new RequestProcess($request);
}

/**
* finishRequestProcess
*
* @param RequestProcess $process
* @param ResponseInterface $response
*/
public function finishRequestProcess(RequestProcess $process, ResponseInterface $response)
{
$process->setResponse($response);
$this->requestStack[] = $process;
}

/**
* Request stack keep all request and responses during a API session for debugging purposes
* Requests and responses are saved in native http format
*
* @return RequestProcessStack|array|RequestProcess[]
*/
public function getRequestStack()
{
return $this->requestStack;
}

/**
* Clear the current debug session with all requests and responses
*
* @return $this
*/
public function flushRequestStack()
{
$this->requestStack = [];

return $this;
}
}
80 changes: 80 additions & 0 deletions src/Debug/RequestProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* This file is part of the GenericApi package.
*
* (c) RafaelSR <https://github.com/rafrsr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Rafrsr\GenericApi\Debug;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class RequestProcess
{
/**
* @var RequestInterface
*/
protected $request;

/**
* @var ResponseInterface
*/
protected $response;

/**
* RequestProcess constructor.
*
* @param RequestInterface $request
* @param ResponseInterface $response
*/
public function __construct(RequestInterface $request = null, ResponseInterface $response = null)
{
$this->request = $request;
$this->response = $response;
}

/**
* @return RequestInterface
*/
public function getRequest()
{
return $this->request;
}

/**
* @param RequestInterface $request
*
* @return $this
*/
public function setRequest(RequestInterface $request)
{
$this->request = $request;

return $this;
}

/**
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}

/**
* @param ResponseInterface $response
*
* @return $this
*/
public function setResponse(ResponseInterface $response)
{
$this->response = $response;

return $this;
}
}
58 changes: 58 additions & 0 deletions src/Debug/RequestProcessStack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* This file is part of the GenericApi package.
*
* (c) RafaelSR <https://github.com/rafrsr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Rafrsr\GenericApi\Debug;

class RequestProcessStack implements \ArrayAccess, \IteratorAggregate, \Countable
{
/**
* @var array|RequestProcess[]
*/
protected $stack;

/**
* @param array|RequestProcess[] $requests
*/
public function __construct(array $requests = [])
{
$this->stack = $requests;
}

public function count()
{
return count($this->stack);
}

public function getIterator()
{
return new \ArrayIterator($this->stack);
}

public function offsetExists($offset)
{
return isset($this->stack[$offset]);
}

public function offsetGet($offset)
{
return isset($this->stack[$offset]) ? $this->stack[$offset] : null;
}

public function offsetSet($offset, $value)
{
$this->stack[$offset] = $value;
}

public function offsetUnset($offset)
{
unset($this->stack[$offset]);
}
}
57 changes: 57 additions & 0 deletions src/Event/ApiEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* This file is part of the GenericApi package.
*
* (c) RafaelSR <https://github.com/rafrsr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Rafrsr\GenericApi\Event;

use Rafrsr\GenericApi\ApiInterface;
use Rafrsr\GenericApi\ApiServiceInterface;
use Symfony\Component\EventDispatcher\Event;

abstract class ApiEvent extends Event
{
/**
* @var ApiInterface
*/
protected $api;

/**
* @var ApiServiceInterface
*/
protected $service;

/**
* ApiEvent constructor.
*
* @param ApiInterface $api
* @param ApiServiceInterface $service
*/
public function __construct(ApiInterface $api, ApiServiceInterface $service)
{
$this->api = $api;
$this->service = $service;
}

/**
* @return ApiInterface
*/
public function getApi()
{
return $this->api;
}

/**
* @return ApiServiceInterface
*/
public function getService()
{
return $this->service;
}
}
45 changes: 45 additions & 0 deletions src/Event/OnResponseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of the GenericApi package.
*
* (c) RafaelSR <https://github.com/rafrsr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Rafrsr\GenericApi\Event;

use Psr\Http\Message\ResponseInterface;
use Rafrsr\GenericApi\ApiInterface;
use Rafrsr\GenericApi\ApiServiceInterface;

class OnResponseEvent extends ApiEvent
{
/**
* @var ResponseInterface
*/
protected $response;

/**
* PreBuildRequestEvent constructor.
*
* @param ApiInterface $api
* @param ApiServiceInterface $service
* @param ResponseInterface $response
*/
public function __construct(ApiInterface $api, ApiServiceInterface $service, ResponseInterface $response)
{
parent::__construct($api, $service);
$this->response = $response;
}

/**
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}
}
45 changes: 45 additions & 0 deletions src/Event/PreBuildRequestEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* This file is part of the GenericApi package.
*
* (c) RafaelSR <https://github.com/rafrsr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Rafrsr\GenericApi\Event;

use Rafrsr\GenericApi\ApiInterface;
use Rafrsr\GenericApi\ApiRequestBuilder;
use Rafrsr\GenericApi\ApiServiceInterface;

class PreBuildRequestEvent extends ApiEvent
{
/**
* @var ApiRequestBuilder
*/
protected $requestBuilder;

/**
* PreBuildRequestEvent constructor.
*
* @param ApiInterface $api
* @param ApiServiceInterface $service
* @param ApiRequestBuilder $requestBuilder
*/
public function __construct(ApiInterface $api, ApiServiceInterface $service, ApiRequestBuilder $requestBuilder)
{
parent::__construct($api, $service);
$this->requestBuilder = $requestBuilder;
}

/**
* @return ApiRequestBuilder
*/
public function getRequestBuilder()
{
return $this->requestBuilder;
}
}
Loading

0 comments on commit 0065498

Please sign in to comment.