mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 10:38:45 +02:00
N°6934 - Symfony 6.4 - upgrade Symfony bundles to 6.4 (#580)
* Update Symfony lib to version ~6.4.0 * Update code missing return type * Add an iTop general configuration entry to store application secret (Symfony mandatory parameter) * Use dependency injection in ExceptionListener & UserProvider classes
This commit is contained in:
83
lib/symfony/cache/Adapter/MemcachedAdapter.php
vendored
83
lib/symfony/cache/Adapter/MemcachedAdapter.php
vendored
@@ -29,19 +29,11 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
*/
|
||||
private const RESERVED_MEMCACHED = " \n\r\t\v\f\0";
|
||||
private const RESERVED_PSR6 = '@()\{}/';
|
||||
private const MAX_KEY_LENGTH = 250;
|
||||
|
||||
protected $maxIdLength = 250;
|
||||
|
||||
private const DEFAULT_CLIENT_OPTIONS = [
|
||||
'persistent_id' => null,
|
||||
'username' => null,
|
||||
'password' => null,
|
||||
\Memcached::OPT_SERIALIZER => \Memcached::SERIALIZER_PHP,
|
||||
];
|
||||
|
||||
private $marshaller;
|
||||
private $client;
|
||||
private $lazyClient;
|
||||
private MarshallerInterface $marshaller;
|
||||
private \Memcached $client;
|
||||
private \Memcached $lazyClient;
|
||||
|
||||
/**
|
||||
* Using a MemcachedAdapter with a TagAwareAdapter for storing tags is discouraged.
|
||||
@@ -56,9 +48,11 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
public function __construct(\Memcached $client, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
|
||||
{
|
||||
if (!static::isSupported()) {
|
||||
throw new CacheException('Memcached '.(\PHP_VERSION_ID >= 80100 ? '> 3.1.5' : '>= 2.2.0').' is required.');
|
||||
throw new CacheException('Memcached > 3.1.5 is required.');
|
||||
}
|
||||
if ('Memcached' === \get_class($client)) {
|
||||
$this->maxIdLength = self::MAX_KEY_LENGTH;
|
||||
|
||||
if ('Memcached' === $client::class) {
|
||||
$opt = $client->getOption(\Memcached::OPT_SERIALIZER);
|
||||
if (\Memcached::SERIALIZER_PHP !== $opt && \Memcached::SERIALIZER_IGBINARY !== $opt) {
|
||||
throw new CacheException('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
|
||||
@@ -74,9 +68,12 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
$this->marshaller = $marshaller ?? new DefaultMarshaller();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported()
|
||||
{
|
||||
return \extension_loaded('memcached') && version_compare(phpversion('memcached'), \PHP_VERSION_ID >= 80100 ? '3.1.6' : '2.2.0', '>=');
|
||||
return \extension_loaded('memcached') && version_compare(phpversion('memcached'), '3.1.6', '>=');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,26 +87,21 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
*
|
||||
* @param array[]|string|string[] $servers An array of servers, a DSN, or an array of DSNs
|
||||
*
|
||||
* @return \Memcached
|
||||
*
|
||||
* @throws \ErrorException When invalid options or servers are provided
|
||||
*/
|
||||
public static function createConnection($servers, array $options = [])
|
||||
public static function createConnection(#[\SensitiveParameter] array|string $servers, array $options = []): \Memcached
|
||||
{
|
||||
if (\is_string($servers)) {
|
||||
$servers = [$servers];
|
||||
} elseif (!\is_array($servers)) {
|
||||
throw new InvalidArgumentException(sprintf('MemcachedAdapter::createClient() expects array or string as first argument, "%s" given.', get_debug_type($servers)));
|
||||
}
|
||||
if (!static::isSupported()) {
|
||||
throw new CacheException('Memcached '.(\PHP_VERSION_ID >= 80100 ? '> 3.1.5' : '>= 2.2.0').' is required.');
|
||||
throw new CacheException('Memcached > 3.1.5 is required.');
|
||||
}
|
||||
set_error_handler(function ($type, $msg, $file, $line) { throw new \ErrorException($msg, 0, $type, $file, $line); });
|
||||
set_error_handler(static fn ($type, $msg, $file, $line) => throw new \ErrorException($msg, 0, $type, $file, $line));
|
||||
try {
|
||||
$options += static::DEFAULT_CLIENT_OPTIONS;
|
||||
$client = new \Memcached($options['persistent_id']);
|
||||
$username = $options['username'];
|
||||
$password = $options['password'];
|
||||
$client = new \Memcached($options['persistent_id'] ?? null);
|
||||
$username = $options['username'] ?? null;
|
||||
$password = $options['password'] ?? null;
|
||||
|
||||
// parse any DSN in $servers
|
||||
foreach ($servers as $i => $dsn) {
|
||||
@@ -117,7 +109,7 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
continue;
|
||||
}
|
||||
if (!str_starts_with($dsn, 'memcached:')) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s" does not start with "memcached:".', $dsn));
|
||||
throw new InvalidArgumentException('Invalid Memcached DSN: it does not start with "memcached:".');
|
||||
}
|
||||
$params = preg_replace_callback('#^memcached:(//)?(?:([^@]*+)@)?#', function ($m) use (&$username, &$password) {
|
||||
if (!empty($m[2])) {
|
||||
@@ -127,7 +119,7 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
return 'file:'.($m[1] ?? '');
|
||||
}, $dsn);
|
||||
if (false === $params = parse_url($params)) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
|
||||
throw new InvalidArgumentException('Invalid Memcached DSN.');
|
||||
}
|
||||
$query = $hosts = [];
|
||||
if (isset($params['query'])) {
|
||||
@@ -135,7 +127,7 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
|
||||
if (isset($query['host'])) {
|
||||
if (!\is_array($hosts = $query['host'])) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
|
||||
throw new InvalidArgumentException('Invalid Memcached DSN: query parameter "host" must be an array.');
|
||||
}
|
||||
foreach ($hosts as $host => $weight) {
|
||||
if (false === $port = strrpos($host, ':')) {
|
||||
@@ -154,7 +146,7 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
}
|
||||
}
|
||||
if (!isset($params['host']) && !isset($params['path'])) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid Memcached DSN: "%s".', $dsn));
|
||||
throw new InvalidArgumentException('Invalid Memcached DSN: missing host or path.');
|
||||
}
|
||||
if (isset($params['path']) && preg_match('#/(\d+)$#', $params['path'], $m)) {
|
||||
$params['weight'] = $m[1];
|
||||
@@ -199,7 +191,7 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
$options[\constant('Memcached::OPT_'.$name)] = $value;
|
||||
}
|
||||
}
|
||||
$client->setOptions($options);
|
||||
$client->setOptions($options + [\Memcached::OPT_SERIALIZER => \Memcached::SERIALIZER_PHP]);
|
||||
|
||||
// set client's servers, taking care of persistent connections
|
||||
if (!$client->isPristine()) {
|
||||
@@ -239,10 +231,7 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doSave(array $values, int $lifetime)
|
||||
protected function doSave(array $values, int $lifetime): array|bool
|
||||
{
|
||||
if (!$values = $this->marshaller->marshall($values, $failed)) {
|
||||
return $failed;
|
||||
@@ -260,10 +249,7 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
return $this->checkResultCode($this->getClient()->setMulti($encodedValues, $lifetime)) ? $failed : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doFetch(array $ids)
|
||||
protected function doFetch(array $ids): iterable
|
||||
{
|
||||
try {
|
||||
$encodedIds = array_map([__CLASS__, 'encodeKey'], $ids);
|
||||
@@ -281,18 +267,12 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doHave(string $id)
|
||||
protected function doHave(string $id): bool
|
||||
{
|
||||
return false !== $this->getClient()->get(self::encodeKey($id)) || $this->checkResultCode(\Memcached::RES_SUCCESS === $this->client->getResultCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doDelete(array $ids)
|
||||
protected function doDelete(array $ids): bool
|
||||
{
|
||||
$ok = true;
|
||||
$encodedIds = array_map([__CLASS__, 'encodeKey'], $ids);
|
||||
@@ -305,15 +285,12 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
return $ok;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doClear(string $namespace)
|
||||
protected function doClear(string $namespace): bool
|
||||
{
|
||||
return '' === $namespace && $this->getClient()->flush();
|
||||
}
|
||||
|
||||
private function checkResultCode($result)
|
||||
private function checkResultCode(mixed $result): mixed
|
||||
{
|
||||
$code = $this->client->getResultCode();
|
||||
|
||||
@@ -326,7 +303,7 @@ class MemcachedAdapter extends AbstractAdapter
|
||||
|
||||
private function getClient(): \Memcached
|
||||
{
|
||||
if ($this->client) {
|
||||
if (isset($this->client)) {
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user