Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add Souin as available provider to the existing system
chore: Rebase on main chore: Add the configuration chore: Update WIP Update configuration add default value for purger configuration declaration Update ApiPlatformExtension.php Bump tests Move files Fix CI Fix tests Update AddTagsListenerTest Update the tests based on the Cache-Tag header Add SouinPurgerTests Fix cs Add tests Fix cs Fix cs Add more tests Fix cs Fix Remove useless getters Throw valid exception Rewording parameters Fix cs
- Loading branch information
Showing
10 changed files
with
619 additions
and
39 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,127 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\HttpCache; | ||
|
||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* Purges Souin. | ||
* | ||
* @author Sylvain Combraque <darkweak@protonmail.com> | ||
* | ||
* @experimental | ||
*/ | ||
class SouinPurger implements PurgerInterface | ||
{ | ||
private const MAX_HEADER_SIZE_PER_BATCH = 1500; | ||
private const SEPARATOR = ', '; | ||
private const SOUIN_COOKIE_NAME = 'souin-authorization-token'; | ||
|
||
private $logger; | ||
|
||
// Clients to send cache invalidation | ||
private $clients; | ||
|
||
// Souin relative paths | ||
private $dsn; | ||
private $path; | ||
|
||
// Souin token if mandatory | ||
private $token; | ||
|
||
/** | ||
* @param ClientInterface[] $clients | ||
*/ | ||
public function __construct( | ||
array $clients, | ||
LoggerInterface $logger, | ||
string $dsn, | ||
string $path, | ||
string $token | ||
) { | ||
$this->clients = $clients; | ||
$this->logger = $logger; | ||
$this->dsn = $dsn; | ||
$this->path = $path; | ||
$this->token = $token; | ||
} | ||
|
||
private function getParametersFromIris(array $iris): string | ||
{ | ||
return implode(self::SEPARATOR, $iris); | ||
} | ||
|
||
/** | ||
* @param array|string[] $iris | ||
* | ||
* @return string[] | ||
*/ | ||
private function getChunkedRegex(array $iris): array | ||
{ | ||
$regex = $this->getParametersFromIris($iris); | ||
$batches = []; | ||
$separatorLength = \strlen(self::SEPARATOR); | ||
|
||
while (\strlen($regex) > self::MAX_HEADER_SIZE_PER_BATCH) { | ||
$splitPosition = strrpos(str_split($regex, self::MAX_HEADER_SIZE_PER_BATCH)[0], self::SEPARATOR); | ||
if ($splitPosition) { | ||
[$batches[], $regex] = str_split($regex, $splitPosition); | ||
$regex = substr($regex, $separatorLength); | ||
} | ||
} | ||
|
||
$batches[] = $regex; | ||
|
||
return $batches; | ||
} | ||
|
||
private function banRegex(string $regex): void | ||
{ | ||
foreach ($this->clients as $client) { | ||
try { | ||
$client->request( | ||
Request::METHOD_PURGE, | ||
$this->dsn.$this->path, | ||
['headers' => array_merge( | ||
[ | ||
'Surrogate-Key' => $regex, | ||
], | ||
$this->token ? | ||
['Cookie' => sprintf('%s=%s', self::SOUIN_COOKIE_NAME, $this->token)] : | ||
[] | ||
)] | ||
); | ||
} catch (GuzzleException $e) { | ||
$this->logger->warning($e->getMessage()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function purge(array $iris) | ||
{ | ||
if (!$iris) { | ||
return; | ||
} | ||
|
||
foreach ($this->getChunkedRegex($iris) as $chunkedRegex) { | ||
$this->banRegex($chunkedRegex); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.