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

codeigniter4 / CodeIgniter4 / 7298596047

22 Dec 2023 10:01AM UTC coverage: 85.049% (+0.004%) from 85.045%
7298596047

push

github

web-flow
Merge pull request #8359 from kenjis/docs-FileLocatorInterface

[4.5] fix: merge mistake

1 of 1 new or added line in 1 file covered. (100.0%)

147 existing lines in 11 files now uncovered.

19358 of 22761 relevant lines covered (85.05%)

193.82 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
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 === []) {
1,694✔
53
            throw CacheException::forInvalidHandlers();
1✔
54
        }
55

56
        if (! isset($config->handler) || ! isset($config->backupHandler)) {
1,693✔
UNCOV
57
            throw CacheException::forNoBackup();
×
58
        }
59

60
        $handler ??= $config->handler;
1,693✔
61
        $backup ??= $config->backupHandler;
1,693✔
62

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

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

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

72
            if (! $adapter->isSupported()) {
1✔
73
                // Fall back to the dummy adapter.
74
                $adapter = new $config->validHandlers['dummy']();
1✔
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();
1,692✔
UNCOV
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)
UNCOV
86
            $adapter = self::getHandler($config, $backup, 'dummy');
×
87
        }
88

89
        return $adapter;
1,692✔
90
    }
91
}
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