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
10 changed files
with
593 additions
and
27 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,126 @@ | ||
<?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 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 (\Exception $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.