Passed
Pull Request — task/3376-TYPO3_12_compatibili... (#3456)
by Rafael
43:06
created

AbstractClassRegistry::injectObjectManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\System\Object;
19
20
use InvalidArgumentException;
21
use stdClass;
22
use TYPO3\CMS\Core\SingletonInterface;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * Abstract class to hold the logic to register and retrieve different classes
27
 * for a specific key.
28
 *
29
 * Can be used to retrieve different "strategies" for the same thing.
30
 *
31
 * @author Timo Hund <[email protected]>
32
 */
33
class AbstractClassRegistry implements SingletonInterface
34
{
35
    /**
36
     * Holds the mapping key => className
37
     * @var array
38
     */
39
    protected array $classMap = [];
40
41
    /**
42
     * Name for the default implementation
43
     *
44
     * @var string
45
     */
46
    protected string $defaultClass = stdClass::class;
47
48
    /**
49
     * Retrieves an instance for a registered type.
50
     *
51
     * @param string $type
52
     * @return object
53
     */
54
    public function getInstance(string $type): object
55
    {
56
        $className = $this->resolveClassName($type);
57
        return $this->createInstance($className);
58
    }
59
60
    /**
61
     * @param string $type
62
     * @return string
63
     */
64
    protected function resolveClassName(string $type): string
65
    {
66
        $className = $this->defaultClass;
67
        if (isset($this->classMap[$type])) {
68
            return $this->classMap[$type];
69
        }
70
        return $className;
71
    }
72
73
    /**
74
     * Create an instance of a certain class
75
     *
76
     * @param string $className
77
     * @return object
78
     */
79
    protected function createInstance(string $className): object
80
    {
81
        return GeneralUtility::makeInstance($className);
82
    }
83
84
    /**
85
     * Can be used to register an implementation in the classMap.
86
     *
87
     * @param string $className
88
     * @param string $type
89
     * @param string $requiredBaseClass
90
     */
91
    protected function register(string $className, string $type, string $requiredBaseClass): void
92
    {
93
        // check if the class is available for TYPO3 before registering the driver
94
        if (!class_exists($className)) {
95
            throw new InvalidArgumentException('Class ' . $className . ' does not exist.', 1462883324);
96
        }
97
98
        if (!is_subclass_of($className, $requiredBaseClass)) {
99
            throw new InvalidArgumentException('Parser ' . $className . ' needs to extend the ' . $requiredBaseClass . '.', 1462883325);
100
        }
101
102
        $this->classMap[$type] = $className;
103
    }
104
}
105