-
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.
Create a static cache class to share cache pools between tests (#21)
- Loading branch information
1 parent
2688316
commit dd4843b
Showing
3 changed files
with
73 additions
and
32 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
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
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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Gertjuhh\SymfonyOpenapiValidator; | ||
|
||
use League\OpenAPIValidation\PSR7\ValidatorBuilder; | ||
use Nyholm\Psr7\Factory\Psr17Factory; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; | ||
|
||
final class StaticOpenApiValidatorCache | ||
{ | ||
private static PsrHttpFactory | null $psrHttpFactory = null; | ||
|
||
/** @var array<string, ValidatorBuilder> */ | ||
private static array $validatorBuilder = []; | ||
|
||
/** | ||
* Install a cache pool for the OpenAPI Validator | ||
* | ||
* @see https://github.com/thephpleague/openapi-psr7-validator#caching-layer--psr-6-support | ||
*/ | ||
public static CacheItemPoolInterface|null $validatorCache = null; | ||
|
||
public static function getPsrHttpFactory(): PsrHttpFactory | ||
{ | ||
if (null === self::$psrHttpFactory) { | ||
$psr17Factory = new Psr17Factory(); | ||
self::$psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory); | ||
} | ||
|
||
return self::$psrHttpFactory; | ||
} | ||
|
||
public static function getValidatorBuilder(string $schema): ValidatorBuilder | ||
{ | ||
if (!\array_key_exists($schema, self::$validatorBuilder)) { | ||
self::$validatorBuilder[$schema] = (new ValidatorBuilder()) | ||
->fromYamlFile($schema); | ||
|
||
if (self::$validatorCache !== null) { | ||
self::$validatorBuilder[$schema]->setCache(self::$validatorCache); | ||
} | ||
} | ||
|
||
return self::$validatorBuilder[$schema]; | ||
} | ||
} |