Passed
Push — master ( 58f77c...e96391 )
by Georges
02:04
created

ExtensionManager::extensionExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
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\PhpfastcacheExtensionNotFoundException;
20
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
21
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
22
use Phpfastcache\Exceptions\PhpfastcacheUnsupportedOperationException;
23
use Phpfastcache\Helper\UninstanciableObjectTrait;
24
25
/**
26
 * @internal This extension manager is meant to manager officials Phpfastcache's extensions.
27
 * @see \Phpfastcache\CacheManager::addCustomDriver() to add you own drivers.
28
 */
29
final class ExtensionManager
30
{
31
    use UninstanciableObjectTrait;
32
33
    public const KNOWN_EXTENSION_NAMES = [
34
        'Arangodb',
35
        'Couchbasev4',
36
        'Couchdb',
37
        'Dynamodb',
38
        'Firestore',
39
        'Mongodb',
40
        'Solr'
41
    ];
42
43
    /**
44
     * @var array<string, string>
45
     */
46
    protected static array $registeredExtensions = [];
47
48
    public static function registerExtension(string $extensionName, string $driverClassName): void
49
    {
50
        if (!str_starts_with($driverClassName, ltrim('Phpfastcache\\Extensions\\', '\\'))) {
51
            throw new PhpfastcacheInvalidArgumentException(
52
                'Only extensions from "\\Phpfastcache\\Extensions\\" namespace are allowed. Use CacheManager::addCustomDriver() to create your own extensions.'
53
            );
54
        }
55
        self::$registeredExtensions[$extensionName] = $driverClassName;
56
    }
57
58
    public static function extensionExists(string $extensionName): bool
59
    {
60
        return isset(self::$registeredExtensions[$extensionName]);
61
    }
62
63
    /**
64
     * @param string $name
65
     * @return string
66
     * @throws PhpfastcacheExtensionNotFoundException
67
     */
68
    public static function getExtension(string $name): string
69
    {
70
        if (isset(self::$registeredExtensions[$name])) {
71
            return self::$registeredExtensions[$name];
72
        } else {
73
            throw new PhpfastcacheExtensionNotFoundException(
74
                sprintf(
75
                    'Unable too find the %s extension. Make sure that you you added through composer: `composer require phpfastcache/%s-extension`',
76
                    $name,
77
                    strtolower($name)
78
                )
79
            );
80
        }
81
    }
82
}
83