| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- /*
- * This file is part of the overtrue/wechat.
- *
- * (c) overtrue <i@overtrue.me>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace Bytedance\Kernel\Traits;
- use Bytedance\Kernel\Exceptions\InvalidArgumentException;
- use Bytedance\Kernel\ServiceContainer;
- use Psr\Cache\CacheItemPoolInterface;
- use Psr\SimpleCache\CacheInterface as SimpleCacheInterface;
- use Symfony\Component\Cache\Adapter\FilesystemAdapter;
- use Symfony\Component\Cache\Psr16Cache;
- use Symfony\Component\Cache\Simple\FilesystemCache;
- /**
- * Trait InteractsWithCache.
- *
- * @author overtrue <i@overtrue.me>
- */
- trait InteractsWithCache
- {
- /**
- * @var \Psr\SimpleCache\CacheInterface
- */
- protected $cache;
- /**
- * Get cache instance.
- *
- * @return \Psr\SimpleCache\CacheInterface
- *
- * @throws \Bytedance\Kernel\Exceptions\InvalidArgumentException
- */
- public function getCache()
- {
- if ($this->cache) {
- return $this->cache;
- }
- if (property_exists($this, 'app') && $this->app instanceof ServiceContainer && isset($this->app['cache'])) {
- $this->setCache($this->app['cache']);
- // Fix PHPStan error
- assert($this->cache instanceof \Psr\SimpleCache\CacheInterface);
- return $this->cache;
- }
- return $this->cache = $this->createDefaultCache();
- }
- /**
- * Set cache instance.
- *
- * @param \Psr\SimpleCache\CacheInterface|\Psr\Cache\CacheItemPoolInterface $cache
- *
- * @return $this
- *
- * @throws \Bytedance\Kernel\Exceptions\InvalidArgumentException
- */
- public function setCache($cache)
- {
- if (empty(\array_intersect([SimpleCacheInterface::class, CacheItemPoolInterface::class], \class_implements($cache)))) {
- throw new InvalidArgumentException(\sprintf('The cache instance must implements %s or %s interface.', SimpleCacheInterface::class, CacheItemPoolInterface::class));
- }
- if ($cache instanceof CacheItemPoolInterface) {
- if (!$this->isSymfony43OrHigher()) {
- throw new InvalidArgumentException(sprintf('The cache instance must implements %s', SimpleCacheInterface::class));
- }
- $cache = new Psr16Cache($cache);
- }
- $this->cache = $cache;
- return $this;
- }
- /**
- * @return \Psr\SimpleCache\CacheInterface
- */
- protected function createDefaultCache()
- {
- if ($this->isSymfony43OrHigher()) {
- return new Psr16Cache(new FilesystemAdapter('bytedance', 1500));
- }
- return new FilesystemCache();
- }
- /**
- * @return bool
- */
- protected function isSymfony43OrHigher(): bool
- {
- return \class_exists('Symfony\Component\Cache\Psr16Cache');
- }
- }
|