generated from cerbero90/skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 changed file
with
72 additions
and
0 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,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cerbero\LazyJsonPages\Services; | ||
|
||
use GuzzleHttp\Client as Guzzle; | ||
use GuzzleHttp\RequestOptions; | ||
|
||
/** | ||
* The client singleton. | ||
*/ | ||
final class Client | ||
{ | ||
/** | ||
* The default options. | ||
* | ||
* @var array<string, mixed> | ||
*/ | ||
private static array $defaultOptions = [ | ||
RequestOptions::STREAM => true, | ||
RequestOptions::HEADERS => [ | ||
'Accept' => 'application/json', | ||
'Content-Type' => 'application/json', | ||
], | ||
]; | ||
|
||
/** | ||
* The custom options. | ||
*/ | ||
private static array $options = []; | ||
|
||
/** | ||
* The Guzzle client instance. | ||
*/ | ||
private static ?Guzzle $guzzle = null; | ||
|
||
/** | ||
* Instantiate the class. | ||
*/ | ||
private function __construct() | ||
{ | ||
// disable the constructor | ||
} | ||
|
||
/** | ||
* Set the Guzzle client options. | ||
*/ | ||
public static function configure(array $options): void | ||
{ | ||
self::$options = array_replace_recursive(self::$options, $options); | ||
} | ||
|
||
/** | ||
* Retrieve the Guzzle client instance. | ||
*/ | ||
public static function instance(): Guzzle | ||
{ | ||
return self::$guzzle ??= new Guzzle( | ||
array_replace_recursive(self::$defaultOptions, self::$options), | ||
); | ||
} | ||
|
||
/** | ||
* Clean up the static values. | ||
*/ | ||
public static function reset(): void | ||
{ | ||
self::$guzzle = null; | ||
self::$options = []; | ||
} | ||
} |