-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
drafting event system and debugger to get look around requests and re…
…sponses
- Loading branch information
Showing
8 changed files
with
495 additions
and
3 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
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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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]); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
Oops, something went wrong.