diff options
| author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
| commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
| tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/symfony/cache/Adapter/AbstractAdapter.php | |
| parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
| download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.tar.gz AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.tar.bz2 AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip | |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/symfony/cache/Adapter/AbstractAdapter.php')
| -rw-r--r-- | vendor/symfony/cache/Adapter/AbstractAdapter.php | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/vendor/symfony/cache/Adapter/AbstractAdapter.php b/vendor/symfony/cache/Adapter/AbstractAdapter.php new file mode 100644 index 0000000..5d6336e --- /dev/null +++ b/vendor/symfony/cache/Adapter/AbstractAdapter.php | |||
| @@ -0,0 +1,191 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | /* | ||
| 4 | * This file is part of the Symfony package. | ||
| 5 | * | ||
| 6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
| 7 | * | ||
| 8 | * For the full copyright and license information, please view the LICENSE | ||
| 9 | * file that was distributed with this source code. | ||
| 10 | */ | ||
| 11 | |||
| 12 | namespace Symfony\Component\Cache\Adapter; | ||
| 13 | |||
| 14 | use Psr\Log\LoggerAwareInterface; | ||
| 15 | use Psr\Log\LoggerInterface; | ||
| 16 | use Symfony\Component\Cache\CacheItem; | ||
| 17 | use Symfony\Component\Cache\Exception\InvalidArgumentException; | ||
| 18 | use Symfony\Component\Cache\ResettableInterface; | ||
| 19 | use Symfony\Component\Cache\Traits\AbstractAdapterTrait; | ||
| 20 | use Symfony\Component\Cache\Traits\ContractsTrait; | ||
| 21 | use Symfony\Contracts\Cache\CacheInterface; | ||
| 22 | |||
| 23 | /** | ||
| 24 | * @author Nicolas Grekas <p@tchwork.com> | ||
| 25 | */ | ||
| 26 | abstract class AbstractAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface | ||
| 27 | { | ||
| 28 | use AbstractAdapterTrait; | ||
| 29 | use ContractsTrait; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * @internal | ||
| 33 | */ | ||
| 34 | protected const NS_SEPARATOR = ':'; | ||
| 35 | |||
| 36 | private static bool $apcuSupported; | ||
| 37 | |||
| 38 | protected function __construct(string $namespace = '', int $defaultLifetime = 0) | ||
| 39 | { | ||
| 40 | $this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).static::NS_SEPARATOR; | ||
| 41 | $this->defaultLifetime = $defaultLifetime; | ||
| 42 | if (null !== $this->maxIdLength && \strlen($namespace) > $this->maxIdLength - 24) { | ||
| 43 | throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s").', $this->maxIdLength - 24, \strlen($namespace), $namespace)); | ||
| 44 | } | ||
| 45 | self::$createCacheItem ??= \Closure::bind( | ||
| 46 | static function ($key, $value, $isHit) { | ||
| 47 | $item = new CacheItem(); | ||
| 48 | $item->key = $key; | ||
| 49 | $item->value = $value; | ||
| 50 | $item->isHit = $isHit; | ||
| 51 | $item->unpack(); | ||
| 52 | |||
| 53 | return $item; | ||
| 54 | }, | ||
| 55 | null, | ||
| 56 | CacheItem::class | ||
| 57 | ); | ||
| 58 | self::$mergeByLifetime ??= \Closure::bind( | ||
| 59 | static function ($deferred, $namespace, &$expiredIds, $getId, $defaultLifetime) { | ||
| 60 | $byLifetime = []; | ||
| 61 | $now = microtime(true); | ||
| 62 | $expiredIds = []; | ||
| 63 | |||
| 64 | foreach ($deferred as $key => $item) { | ||
| 65 | $key = (string) $key; | ||
| 66 | if (null === $item->expiry) { | ||
| 67 | $ttl = 0 < $defaultLifetime ? $defaultLifetime : 0; | ||
| 68 | } elseif (!$item->expiry) { | ||
| 69 | $ttl = 0; | ||
| 70 | } elseif (0 >= $ttl = (int) (0.1 + $item->expiry - $now)) { | ||
| 71 | $expiredIds[] = $getId($key); | ||
| 72 | continue; | ||
| 73 | } | ||
| 74 | $byLifetime[$ttl][$getId($key)] = $item->pack(); | ||
| 75 | } | ||
| 76 | |||
| 77 | return $byLifetime; | ||
| 78 | }, | ||
| 79 | null, | ||
| 80 | CacheItem::class | ||
| 81 | ); | ||
| 82 | } | ||
| 83 | |||
| 84 | /** | ||
| 85 | * Returns the best possible adapter that your runtime supports. | ||
| 86 | * | ||
| 87 | * Using ApcuAdapter makes system caches compatible with read-only filesystems. | ||
| 88 | */ | ||
| 89 | public static function createSystemCache(string $namespace, int $defaultLifetime, string $version, string $directory, ?LoggerInterface $logger = null): AdapterInterface | ||
| 90 | { | ||
| 91 | $opcache = new PhpFilesAdapter($namespace, $defaultLifetime, $directory, true); | ||
| 92 | if (null !== $logger) { | ||
| 93 | $opcache->setLogger($logger); | ||
| 94 | } | ||
| 95 | |||
| 96 | if (!self::$apcuSupported ??= ApcuAdapter::isSupported()) { | ||
| 97 | return $opcache; | ||
| 98 | } | ||
| 99 | |||
| 100 | if ('cli' === \PHP_SAPI && !filter_var(\ini_get('apc.enable_cli'), \FILTER_VALIDATE_BOOL)) { | ||
| 101 | return $opcache; | ||
| 102 | } | ||
| 103 | |||
| 104 | $apcu = new ApcuAdapter($namespace, intdiv($defaultLifetime, 5), $version); | ||
| 105 | if (null !== $logger) { | ||
| 106 | $apcu->setLogger($logger); | ||
| 107 | } | ||
| 108 | |||
| 109 | return new ChainAdapter([$apcu, $opcache]); | ||
| 110 | } | ||
| 111 | |||
| 112 | public static function createConnection(#[\SensitiveParameter] string $dsn, array $options = []): mixed | ||
| 113 | { | ||
| 114 | if (str_starts_with($dsn, 'redis:') || str_starts_with($dsn, 'rediss:')) { | ||
| 115 | return RedisAdapter::createConnection($dsn, $options); | ||
| 116 | } | ||
| 117 | if (str_starts_with($dsn, 'memcached:')) { | ||
| 118 | return MemcachedAdapter::createConnection($dsn, $options); | ||
| 119 | } | ||
| 120 | if (str_starts_with($dsn, 'couchbase:')) { | ||
| 121 | if (class_exists('CouchbaseBucket') && CouchbaseBucketAdapter::isSupported()) { | ||
| 122 | return CouchbaseBucketAdapter::createConnection($dsn, $options); | ||
| 123 | } | ||
| 124 | |||
| 125 | return CouchbaseCollectionAdapter::createConnection($dsn, $options); | ||
| 126 | } | ||
| 127 | if (preg_match('/^(mysql|oci|pgsql|sqlsrv|sqlite):/', $dsn)) { | ||
| 128 | return PdoAdapter::createConnection($dsn, $options); | ||
| 129 | } | ||
| 130 | |||
| 131 | throw new InvalidArgumentException('Unsupported DSN: it does not start with "redis[s]:", "memcached:", "couchbase:", "mysql:", "oci:", "pgsql:", "sqlsrv:" nor "sqlite:".'); | ||
| 132 | } | ||
| 133 | |||
| 134 | public function commit(): bool | ||
| 135 | { | ||
| 136 | $ok = true; | ||
| 137 | $byLifetime = (self::$mergeByLifetime)($this->deferred, $this->namespace, $expiredIds, $this->getId(...), $this->defaultLifetime); | ||
| 138 | $retry = $this->deferred = []; | ||
| 139 | |||
| 140 | if ($expiredIds) { | ||
| 141 | try { | ||
| 142 | $this->doDelete($expiredIds); | ||
| 143 | } catch (\Exception $e) { | ||
| 144 | $ok = false; | ||
| 145 | CacheItem::log($this->logger, 'Failed to delete expired items: '.$e->getMessage(), ['exception' => $e, 'cache-adapter' => get_debug_type($this)]); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | foreach ($byLifetime as $lifetime => $values) { | ||
| 149 | try { | ||
| 150 | $e = $this->doSave($values, $lifetime); | ||
| 151 | } catch (\Exception $e) { | ||
| 152 | } | ||
| 153 | if (true === $e || [] === $e) { | ||
| 154 | continue; | ||
| 155 | } | ||
| 156 | if (\is_array($e) || 1 === \count($values)) { | ||
| 157 | foreach (\is_array($e) ? $e : array_keys($values) as $id) { | ||
| 158 | $ok = false; | ||
| 159 | $v = $values[$id]; | ||
| 160 | $type = get_debug_type($v); | ||
| 161 | $message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.'); | ||
| 162 | CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null, 'cache-adapter' => get_debug_type($this)]); | ||
| 163 | } | ||
| 164 | } else { | ||
| 165 | foreach ($values as $id => $v) { | ||
| 166 | $retry[$lifetime][] = $id; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | // When bulk-save failed, retry each item individually | ||
| 172 | foreach ($retry as $lifetime => $ids) { | ||
| 173 | foreach ($ids as $id) { | ||
| 174 | try { | ||
| 175 | $v = $byLifetime[$lifetime][$id]; | ||
| 176 | $e = $this->doSave([$id => $v], $lifetime); | ||
| 177 | } catch (\Exception $e) { | ||
| 178 | } | ||
| 179 | if (true === $e || [] === $e) { | ||
| 180 | continue; | ||
| 181 | } | ||
| 182 | $ok = false; | ||
| 183 | $type = get_debug_type($v); | ||
| 184 | $message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.'); | ||
| 185 | CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null, 'cache-adapter' => get_debug_type($this)]); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | return $ok; | ||
| 190 | } | ||
| 191 | } | ||
