• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 7293561159

21 Dec 2023 09:55PM UTC coverage: 85.237% (+0.004%) from 85.233%
7293561159

push

github

web-flow
Merge pull request #8355 from paulbalandan/replace

Add `replace` to composer.json

18597 of 21818 relevant lines covered (85.24%)

199.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.78
/system/Cache/CacheFactory.php
1
<?php
2

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

12
namespace CodeIgniter\Cache;
13

14
use CodeIgniter\Cache\Exceptions\CacheException;
15
use CodeIgniter\Exceptions\CriticalError;
16
use CodeIgniter\Test\Mock\MockCache;
17
use Config\Cache;
18

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

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

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

54
        if (! isset($config->handler) || ! isset($config->backupHandler)) {
1,683✔
55
            throw CacheException::forNoBackup();
×
56
        }
57

58
        $handler ??= $config->handler;
1,683✔
59
        $backup ??= $config->backupHandler;
1,683✔
60

61
        if (! array_key_exists($handler, $config->validHandlers) || ! array_key_exists($backup, $config->validHandlers)) {
1,683✔
62
            throw CacheException::forHandlerNotFound();
1✔
63
        }
64

65
        $adapter = new $config->validHandlers[$handler]($config);
1,682✔
66

67
        if (! $adapter->isSupported()) {
1,682✔
68
            $adapter = new $config->validHandlers[$backup]($config);
1✔
69

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

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

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

87
        return $adapter;
1,682✔
88
    }
89
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc