mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-20 00:58:48 +02:00
migration symfony 5 4 (#300)
* symfony 5.4 (diff dev) * symfony 5.4 (working) * symfony 5.4 (update autoload) * symfony 5.4 (remove swiftmailer mailer implementation) * symfony 5.4 (php doc and split Global accessor class) ### Impacted packages: composer require php:">=7.2.5 <8.0.0" symfony/console:5.4.* symfony/dotenv:5.4.* symfony/framework-bundle:5.4.* symfony/twig-bundle:5.4.* symfony/yaml:5.4.* --update-with-dependencies composer require symfony/stopwatch:5.4.* symfony/web-profiler-bundle:5.4.* --dev --update-with-dependencies
This commit is contained in:
@@ -25,22 +25,21 @@ use Symfony\Component\HttpFoundation\Response;
|
||||
class Store implements StoreInterface
|
||||
{
|
||||
protected $root;
|
||||
/** @var \SplObjectStorage<Request, string> */
|
||||
private $keyCache;
|
||||
private $locks;
|
||||
/** @var array<string, resource> */
|
||||
private $locks = [];
|
||||
|
||||
/**
|
||||
* @param string $root The path to the cache directory
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function __construct($root)
|
||||
public function __construct(string $root)
|
||||
{
|
||||
$this->root = $root;
|
||||
if (!file_exists($this->root) && !@mkdir($this->root, 0777, true) && !is_dir($this->root)) {
|
||||
if (!is_dir($this->root) && !@mkdir($this->root, 0777, true) && !is_dir($this->root)) {
|
||||
throw new \RuntimeException(sprintf('Unable to create the store directory (%s).', $this->root));
|
||||
}
|
||||
$this->keyCache = new \SplObjectStorage();
|
||||
$this->locks = [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,10 +67,10 @@ class Store implements StoreInterface
|
||||
|
||||
if (!isset($this->locks[$key])) {
|
||||
$path = $this->getPath($key);
|
||||
if (!file_exists(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) {
|
||||
if (!is_dir(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) {
|
||||
return $path;
|
||||
}
|
||||
$h = fopen($path, 'cb');
|
||||
$h = fopen($path, 'c');
|
||||
if (!flock($h, \LOCK_EX | \LOCK_NB)) {
|
||||
fclose($h);
|
||||
|
||||
@@ -112,11 +111,11 @@ class Store implements StoreInterface
|
||||
return true; // shortcut if lock held by this process
|
||||
}
|
||||
|
||||
if (!file_exists($path = $this->getPath($key))) {
|
||||
if (!is_file($path = $this->getPath($key))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$h = fopen($path, 'rb');
|
||||
$h = fopen($path, 'r');
|
||||
flock($h, \LOCK_EX | \LOCK_NB, $wouldBlock);
|
||||
flock($h, \LOCK_UN); // release the lock we just acquired
|
||||
fclose($h);
|
||||
@@ -127,7 +126,7 @@ class Store implements StoreInterface
|
||||
/**
|
||||
* Locates a cached Response for the Request provided.
|
||||
*
|
||||
* @return Response|null A Response instance, or null if no cache entry was found
|
||||
* @return Response|null
|
||||
*/
|
||||
public function lookup(Request $request)
|
||||
{
|
||||
@@ -168,7 +167,7 @@ class Store implements StoreInterface
|
||||
* Existing entries are read and any that match the response are removed. This
|
||||
* method calls write with the new list of cache entries.
|
||||
*
|
||||
* @return string The key under which the response is stored
|
||||
* @return string
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
@@ -209,7 +208,7 @@ class Store implements StoreInterface
|
||||
$entry[1]['vary'] = [''];
|
||||
}
|
||||
|
||||
if ($entry[1]['vary'][0] != $vary || !$this->requestsMatch($vary, $entry[0], $storedEnv)) {
|
||||
if ($entry[1]['vary'][0] != $vary || !$this->requestsMatch($vary ?? '', $entry[0], $storedEnv)) {
|
||||
$entries[] = $entry;
|
||||
}
|
||||
}
|
||||
@@ -267,13 +266,11 @@ class Store implements StoreInterface
|
||||
* Determines whether two Request HTTP header sets are non-varying based on
|
||||
* the vary response header value provided.
|
||||
*
|
||||
* @param string $vary A Response vary header
|
||||
* @param array $env1 A Request HTTP header array
|
||||
* @param array $env2 A Request HTTP header array
|
||||
*
|
||||
* @return bool true if the two environments match, false otherwise
|
||||
* @param string|null $vary A Response vary header
|
||||
* @param array $env1 A Request HTTP header array
|
||||
* @param array $env2 A Request HTTP header array
|
||||
*/
|
||||
private function requestsMatch($vary, $env1, $env2)
|
||||
private function requestsMatch(?string $vary, array $env1, array $env2): bool
|
||||
{
|
||||
if (empty($vary)) {
|
||||
return true;
|
||||
@@ -281,8 +278,8 @@ class Store implements StoreInterface
|
||||
|
||||
foreach (preg_split('/[\s,]+/', $vary) as $header) {
|
||||
$key = str_replace('_', '-', strtolower($header));
|
||||
$v1 = isset($env1[$key]) ? $env1[$key] : null;
|
||||
$v2 = isset($env2[$key]) ? $env2[$key] : null;
|
||||
$v1 = $env1[$key] ?? null;
|
||||
$v2 = $env2[$key] ?? null;
|
||||
if ($v1 !== $v2) {
|
||||
return false;
|
||||
}
|
||||
@@ -295,18 +292,14 @@ class Store implements StoreInterface
|
||||
* Gets all data associated with the given key.
|
||||
*
|
||||
* Use this method only if you know what you are doing.
|
||||
*
|
||||
* @param string $key The store key
|
||||
*
|
||||
* @return array An array of data associated with the key
|
||||
*/
|
||||
private function getMetadata($key)
|
||||
private function getMetadata(string $key): array
|
||||
{
|
||||
if (!$entries = $this->load($key)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return unserialize($entries);
|
||||
return unserialize($entries) ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -314,11 +307,9 @@ class Store implements StoreInterface
|
||||
*
|
||||
* This method purges both the HTTP and the HTTPS version of the cache entry.
|
||||
*
|
||||
* @param string $url A URL
|
||||
*
|
||||
* @return bool true if the URL exists with either HTTP or HTTPS scheme and has been purged, false otherwise
|
||||
*/
|
||||
public function purge($url)
|
||||
public function purge(string $url)
|
||||
{
|
||||
$http = preg_replace('#^https:#', 'http:', $url);
|
||||
$https = preg_replace('#^http:#', 'https:', $url);
|
||||
@@ -331,12 +322,8 @@ class Store implements StoreInterface
|
||||
|
||||
/**
|
||||
* Purges data for the given URL.
|
||||
*
|
||||
* @param string $url A URL
|
||||
*
|
||||
* @return bool true if the URL exists and has been purged, false otherwise
|
||||
*/
|
||||
private function doPurge($url)
|
||||
private function doPurge(string $url): bool
|
||||
{
|
||||
$key = $this->getCacheKey(Request::create($url));
|
||||
if (isset($this->locks[$key])) {
|
||||
@@ -345,7 +332,7 @@ class Store implements StoreInterface
|
||||
unset($this->locks[$key]);
|
||||
}
|
||||
|
||||
if (file_exists($path = $this->getPath($key))) {
|
||||
if (is_file($path = $this->getPath($key))) {
|
||||
unlink($path);
|
||||
|
||||
return true;
|
||||
@@ -356,28 +343,18 @@ class Store implements StoreInterface
|
||||
|
||||
/**
|
||||
* Loads data for the given key.
|
||||
*
|
||||
* @param string $key The store key
|
||||
*
|
||||
* @return string|null The data associated with the key
|
||||
*/
|
||||
private function load($key)
|
||||
private function load(string $key): ?string
|
||||
{
|
||||
$path = $this->getPath($key);
|
||||
|
||||
return file_exists($path) && false !== ($contents = file_get_contents($path)) ? $contents : null;
|
||||
return is_file($path) && false !== ($contents = @file_get_contents($path)) ? $contents : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save data for the given key.
|
||||
*
|
||||
* @param string $key The store key
|
||||
* @param string $data The data to store
|
||||
* @param bool $overwrite Whether existing data should be overwritten
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function save($key, $data, $overwrite = true)
|
||||
private function save(string $key, string $data, bool $overwrite = true): bool
|
||||
{
|
||||
$path = $this->getPath($key);
|
||||
|
||||
@@ -396,12 +373,12 @@ class Store implements StoreInterface
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!file_exists(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) {
|
||||
if (!is_dir(\dirname($path)) && false === @mkdir(\dirname($path), 0777, true) && !is_dir(\dirname($path))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$tmpFile = tempnam(\dirname($path), basename($path));
|
||||
if (false === $fp = @fopen($tmpFile, 'wb')) {
|
||||
if (false === $fp = @fopen($tmpFile, 'w')) {
|
||||
@unlink($tmpFile);
|
||||
|
||||
return false;
|
||||
@@ -427,7 +404,7 @@ class Store implements StoreInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getPath($key)
|
||||
public function getPath(string $key)
|
||||
{
|
||||
return $this->root.\DIRECTORY_SEPARATOR.substr($key, 0, 2).\DIRECTORY_SEPARATOR.substr($key, 2, 2).\DIRECTORY_SEPARATOR.substr($key, 4, 2).\DIRECTORY_SEPARATOR.substr($key, 6);
|
||||
}
|
||||
@@ -442,7 +419,7 @@ class Store implements StoreInterface
|
||||
* headers, use a Vary header to indicate them, and each representation will
|
||||
* be stored independently under the same cache key.
|
||||
*
|
||||
* @return string A key for the given Request
|
||||
* @return string
|
||||
*/
|
||||
protected function generateCacheKey(Request $request)
|
||||
{
|
||||
@@ -451,10 +428,8 @@ class Store implements StoreInterface
|
||||
|
||||
/**
|
||||
* Returns a cache key for the given Request.
|
||||
*
|
||||
* @return string A key for the given Request
|
||||
*/
|
||||
private function getCacheKey(Request $request)
|
||||
private function getCacheKey(Request $request): string
|
||||
{
|
||||
if (isset($this->keyCache[$request])) {
|
||||
return $this->keyCache[$request];
|
||||
@@ -465,20 +440,16 @@ class Store implements StoreInterface
|
||||
|
||||
/**
|
||||
* Persists the Request HTTP headers.
|
||||
*
|
||||
* @return array An array of HTTP headers
|
||||
*/
|
||||
private function persistRequest(Request $request)
|
||||
private function persistRequest(Request $request): array
|
||||
{
|
||||
return $request->headers->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Persists the Response HTTP headers.
|
||||
*
|
||||
* @return array An array of HTTP headers
|
||||
*/
|
||||
private function persistResponse(Response $response)
|
||||
private function persistResponse(Response $response): array
|
||||
{
|
||||
$headers = $response->headers->all();
|
||||
$headers['X-Status'] = [$response->getStatusCode()];
|
||||
@@ -488,13 +459,8 @@ class Store implements StoreInterface
|
||||
|
||||
/**
|
||||
* Restores a Response from the HTTP headers and body.
|
||||
*
|
||||
* @param array $headers An array of HTTP headers for the Response
|
||||
* @param string $path Path to the Response body
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
private function restoreResponse($headers, $path = null)
|
||||
private function restoreResponse(array $headers, string $path = null): Response
|
||||
{
|
||||
$status = $headers['X-Status'][0];
|
||||
unset($headers['X-Status']);
|
||||
|
||||
Reference in New Issue
Block a user