ci4

Форк
0
/
CacheFactory.php 
91 строка · 2.6 Кб
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Cache;
15

16
use CodeIgniter\Cache\Exceptions\CacheException;
17
use CodeIgniter\Exceptions\CriticalError;
18
use CodeIgniter\Test\Mock\MockCache;
19
use Config\Cache;
20

21
/**
22
 * A factory for loading the desired
23
 *
24
 * @see \CodeIgniter\Cache\CacheFactoryTest
25
 */
26
class CacheFactory
27
{
28
    /**
29
     * The class to use when mocking
30
     *
31
     * @var string
32
     */
33
    public static $mockClass = MockCache::class;
34

35
    /**
36
     * The service to inject the mock as
37
     *
38
     * @var string
39
     */
40
    public static $mockServiceName = 'cache';
41

42
    /**
43
     * Attempts to create the desired cache handler, based upon the
44
     *
45
     * @param non-empty-string|null $handler
46
     * @param non-empty-string|null $backup
47
     *
48
     * @return CacheInterface
49
     */
50
    public static function getHandler(Cache $config, ?string $handler = null, ?string $backup = null)
51
    {
52
        if (! isset($config->validHandlers) || $config->validHandlers === []) {
53
            throw CacheException::forInvalidHandlers();
54
        }
55

56
        if (! isset($config->handler) || ! isset($config->backupHandler)) {
57
            throw CacheException::forNoBackup();
58
        }
59

60
        $handler ??= $config->handler;
61
        $backup ??= $config->backupHandler;
62

63
        if (! array_key_exists($handler, $config->validHandlers) || ! array_key_exists($backup, $config->validHandlers)) {
64
            throw CacheException::forHandlerNotFound();
65
        }
66

67
        $adapter = new $config->validHandlers[$handler]($config);
68

69
        if (! $adapter->isSupported()) {
70
            $adapter = new $config->validHandlers[$backup]($config);
71

72
            if (! $adapter->isSupported()) {
73
                // Fall back to the dummy adapter.
74
                $adapter = new $config->validHandlers['dummy']();
75
            }
76
        }
77

78
        // If $adapter->initialization throws a CriticalError exception, we will attempt to
79
        // use the $backup handler, if that also fails, we resort to the dummy handler.
80
        try {
81
            $adapter->initialize();
82
        } catch (CriticalError $e) {
83
            log_message('critical', $e . ' Resorting to using ' . $backup . ' handler.');
84

85
            // get the next best cache handler (or dummy if the $backup also fails)
86
            $adapter = self::getHandler($config, $backup, 'dummy');
87
        }
88

89
        return $adapter;
90
    }
91
}
92

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.