Skip to content

Commit

Permalink
Implement supported sources
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Jan 22, 2024
1 parent e5ced02 commit c3fb216
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 45 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ A source is any mean that can point to a paginated JSON API. A number of sources
- **PSR-7 requests**, i.e. any instance of `Psr\Http\Message\RequestInterface`
- **Laravel HTTP client requests**, i.e. any instance of `Illuminate\Http\Client\Request`
- **Laravel HTTP client responses**, i.e. any instance of `Illuminate\Http\Client\Response`
- **Laravel HTTP requests**, i.e. any instance of `Illuminate\Http\Request`
- **Symfony requests**, i.e. any instance of `Symfony\Component\HttpFoundation\Request`
- **user-defined sources**, i.e. any instance of `Cerbero\LazyJsonPages\Sources\Source`

Here are some examples of sources:
Expand Down
17 changes: 17 additions & 0 deletions src/Exceptions/RequestNotSentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Cerbero\LazyJsonPages\Exceptions;

/**
* The exception thrown when a source did not send any HTTP request.
*/
class RequestNotSentException extends LazyJsonPagesException
{
/**
* Instantiate the class.
*/
public function __construct()
{
parent::__construct("The source did not send any HTTP request.");
}
}
24 changes: 12 additions & 12 deletions src/Services/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ final class Client
private static ?Guzzle $guzzle = null;

/**
* Instantiate the class.
* Retrieve the Guzzle client instance.
*/
private function __construct()
public static function instance(): Guzzle
{
// disable the constructor
return self::$guzzle ??= new Guzzle(
array_replace_recursive(self::$defaultOptions, self::$options),
);
}

/**
Expand All @@ -52,21 +54,19 @@ public static function configure(array $options): void
}

/**
* Retrieve the Guzzle client instance.
* Clean up the static values.
*/
public static function instance(): Guzzle
public static function reset(): void
{
return self::$guzzle ??= new Guzzle(
array_replace_recursive(self::$defaultOptions, self::$options),
);
self::$guzzle = null;
self::$options = [];
}

/**
* Clean up the static values.
* Instantiate the class.
*/
public static function reset(): void
private function __construct()
{
self::$guzzle = null;
self::$options = [];
// disable the constructor
}
}
19 changes: 5 additions & 14 deletions src/Sources/AnySource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,19 @@ class AnySource extends Source
* @var class-string<Source>[]
*/
protected array $supportedSources = [
// CustomSource::class,
CustomSource::class,
Endpoint::class,
// LaravelClientRequest::class,
// LaravelClientResponse::class,
// LaravelRequest::class,
// Psr7Request::class,
// SymfonyRequest::class,
LaravelClientRequest::class,
LaravelClientResponse::class,
Psr7Request::class,
SymfonyRequest::class,
];

/**
* The matching source.
*/
protected ?Source $matchingSource;

/**
* Determine whether this class can handle the source.
*/
public function matches(): bool
{
return true;
}

/**
* Retrieve the HTTP request.
*/
Expand Down
42 changes: 42 additions & 0 deletions src/Sources/CustomSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Cerbero\LazyJsonPages\Sources;

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

/**
* The user-defined source.
*
* @property-read Source $source
*/
class CustomSource extends Source
{
/**
* Determine whether this class can handle the source.
*/
public function matches(): bool
{
return $this->source instanceof Source;
}

/**
* Retrieve the HTTP request.
*/
public function request(): RequestInterface
{
return $this->source->request();
}

/**
* Retrieve the HTTP response.
*
* @return ResponseInterface
*/
public function response(): ResponseInterface
{
return $this->source->response();
}
}
2 changes: 1 addition & 1 deletion src/Sources/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Endpoint extends Source
public function matches(): bool
{
return $this->source instanceof UriInterface
|| $this->isEndpoint($this->source);
|| (is_string($this->source) && $this->isEndpoint($this->source));
}

/**
Expand Down
44 changes: 44 additions & 0 deletions src/Sources/LaravelClientRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Cerbero\LazyJsonPages\Sources;

use Cerbero\LazyJsonPages\Services\Client;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Illuminate\Http\Client\Request;

/**
* The Laravel HTTP client source.
*
* @property-read Request $source
*/
class LaravelClientRequest extends Source
{
/**
* Determine whether this class can handle the source.
*/
public function matches(): bool
{
return $this->source instanceof Request;
}

/**
* Retrieve the HTTP request.
*/
public function request(): RequestInterface
{
return $this->source->toPsrRequest();
}

/**
* Retrieve the HTTP response.
*
* @return ResponseInterface
*/
public function response(): ResponseInterface
{
return Client::instance()->send($this->request());
}
}
46 changes: 46 additions & 0 deletions src/Sources/LaravelClientResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Cerbero\LazyJsonPages\Sources;

use Cerbero\LazyJsonPages\Exceptions\RequestNotSentException;
use Cerbero\LazyJsonPages\Services\Client;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Illuminate\Http\Client\Response;

/**
* The Laravel HTTP client source.
*
* @property-read Response $source
*/
class LaravelClientResponse extends Source
{
/**
* Determine whether this class can handle the source.
*/
public function matches(): bool
{
return $this->source instanceof Response;
}

/**
* Retrieve the HTTP request.
*/
public function request(): RequestInterface
{
return $this->source->transferStats?->getRequest()
?: throw new RequestNotSentException();
}

/**
* Retrieve the HTTP response.
*
* @return ResponseInterface
*/
public function response(): ResponseInterface
{
return $this->source->toPsrResponse();
}
}
43 changes: 43 additions & 0 deletions src/Sources/Psr7Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Cerbero\LazyJsonPages\Sources;

use Cerbero\LazyJsonPages\Services\Client;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* The PSR-7 request source.
*
* @property-read RequestInterface $source
*/
class Psr7Request extends Source
{
/**
* Determine whether this class can handle the source.
*/
public function matches(): bool
{
return $this->source instanceof RequestInterface;
}

/**
* Retrieve the HTTP request.
*/
public function request(): RequestInterface
{
return $this->source;
}

/**
* Retrieve the HTTP response.
*
* @return ResponseInterface
*/
public function response(): ResponseInterface
{
return Client::instance()->send($this->source);
}
}
21 changes: 12 additions & 9 deletions src/Sources/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@
*/
abstract class Source
{
final public function __construct(
protected readonly mixed $source,
) {}

/**
* Determine whether this class can handle the source.
*/
abstract public function matches(): bool;

/**
* Retrieve the HTTP request.
*/
Expand All @@ -32,4 +23,16 @@ abstract public function request(): RequestInterface;
* @return ResponseInterface
*/
abstract public function response(): ResponseInterface;

final public function __construct(
protected readonly mixed $source,
) {}

/**
* Determine whether this class can handle the source.
*/
public function matches(): bool
{
return true;
}
}
50 changes: 50 additions & 0 deletions src/Sources/SymfonyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Cerbero\LazyJsonPages\Sources;

use Cerbero\LazyJsonPages\Services\Client;
use GuzzleHttp\Psr7\Request as Psr7Request;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* The Symfony request source.
*
* @property-read Request $source
*/
class SymfonyRequest extends Source
{
/**
* Determine whether this class can handle the source.
*/
public function matches(): bool
{
return $this->source instanceof Request;
}

/**
* Retrieve the HTTP request.
*/
public function request(): RequestInterface
{
return new Psr7Request(
$this->source->getMethod(),
$this->source->getUri(),
$this->source->headers->all(),
$this->source->getContent() ?: null,
);
}

/**
* Retrieve the HTTP response.
*
* @return ResponseInterface
*/
public function response(): ResponseInterface
{
return Client::instance()->send($this->request());
}
}
Loading

0 comments on commit c3fb216

Please sign in to comment.