Passed
Push — master ( c82647...c877fa )
by Georges
11:01
created

ExtensionManager::autoloadExtensions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * This file is part of Phpfastcache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache;
18
19
use Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException;
20
use Phpfastcache\Exceptions\PhpfastcacheExtensionNotFoundException;
21
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
22
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
23
use Phpfastcache\Exceptions\PhpfastcacheUnsupportedOperationException;
24
use Phpfastcache\Helper\UninstanciableObjectTrait;
25
26
final class ExtensionManager
27
{
28
    use UninstanciableObjectTrait;
29
30
    /**
31
     * @var array<string, string>
32
     */
33
    protected static array $registeredExtensions = [];
34
35
    public static function registerExtension(string $extensionName, string $driverClassName): void
36
    {
37
        self::$registeredExtensions[$extensionName] = $driverClassName;
38
    }
39
40
    /**
41
     * Autoload all the discoverable extensions.
42
     *
43
     * @return void
44
     * @throws PhpfastcacheExtensionNotFoundException
45
     * @throws PhpfastcacheInvalidArgumentException
46
     * @throws PhpfastcacheLogicException
47
     * @throws PhpfastcacheUnsupportedOperationException
48
     */
49
    public static function autoloadExtensions(): void
50
    {
51
        foreach (self::$registeredExtensions as $extension) {
52
            self::loadExtension($extension);
53
        }
54
    }
55
56
    /**
57
     * @param string $name
58
     * @return void
59
     * @throws PhpfastcacheExtensionNotFoundException
60
     * @throws PhpfastcacheInvalidArgumentException
61
     * @throws PhpfastcacheLogicException
62
     * @throws PhpfastcacheUnsupportedOperationException
63
     */
64
    public static function loadExtension(string $name): void
65
    {
66
        if (!CacheManager::customDriverExists($name)) {
67
            if (isset(self::$registeredExtensions[$name])) {
68
                CacheManager::addCustomDriver($name, self::$registeredExtensions[$name]);
69
            } else {
70
                throw new PhpfastcacheExtensionNotFoundException(
71
                    sprintf(
72
                        'Unable too find the %s extension. Make sure that you you added through composer: `composer require phpfastcache/%s-extension`',
73
                        $name,
74
                        strtolower($name)
75
                    )
76
                );
77
            }
78
        }
79
    }
80
}
81