Issues (202)

Classes/System/Object/AbstractClassRegistry.php (1 issue)

1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Object;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\SingletonInterface;
18
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
19
20
/**
21
 * Abstract class to hold the logic to register and retrieve different classes
22
 * for a specific key.
23
 *
24
 * Can be used to retrieve different "strategies" for the same thing.
25
 *
26
 * @author Timo Hund <[email protected]>
27
 */
28
class AbstractClassRegistry implements SingletonInterface
29
{
30
    /**
31
     * Holds the mapping key => className
32
     * @var array
33
     */
34
    protected $classMap = [];
35
36
    /**
37
     * Name for the default implementation
38
     *
39
     * @var string
40
     */
41
    protected $defaultClass = \stdClass::class;
42
43
    /**
44
     * @var ObjectManagerInterface
45
     */
46
    protected $objectManager;
47
48
    /**
49
     * @param ObjectManagerInterface $objectManager
50
     */
51
    public function injectObjectManager(ObjectManagerInterface $objectManager)
52 75
    {
53
        $this->objectManager = $objectManager;
54 75
    }
55 75
56
    /**
57
     * Retrieves an instance for an registered type.
58
     *
59
     * @param string $type
60
     * @return object
61
     */
62
    public function getInstance($type)
63 70
    {
64
        $className = $this->resolveClassName($type);
65 70
        return $this->createInstance($className);
66 70
    }
67
68
    /**
69
     * @param string $type
70
     * @return string
71
     */
72
    protected function resolveClassName($type)
73 70
    {
74
        $className = $this->defaultClass;
75 70
        if (isset($this->classMap[$type])) {
76 70
            $className = $this->classMap[$type];
77 46
            return $className;
78 46
        }
79
        return $className;
80 55
    }
81
82
    /**
83
     * Create an instance of a certain class
84
     *
85
     * @param string $className
86
     * @return object
87
     */
88
    protected function createInstance($className)
89 67
    {
90
        return $this->objectManager->get($className);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object...ManagerInterface::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

90
        return /** @scrutinizer ignore-deprecated */ $this->objectManager->get($className);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
91 67
    }
92
93
    /**
94
     * Can be used to register an implementation in the classMap.
95
     *
96
     * @param string $className
97
     * @param string $type
98
     * @param string $requiredBaseClass
99
     */
100
    protected function register($className, $type, $requiredBaseClass) {
101 3
        // check if the class is available for TYPO3 before registering the driver
102
        if (!class_exists($className)) {
103 3
            throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1462883324);
104 1
        }
105
106
        if (!is_subclass_of($className, $requiredBaseClass)) {
107 2
            throw new \InvalidArgumentException('Parser ' . $className . ' needs to extend the ' . $requiredBaseClass . '.', 1462883325);
108 1
        }
109
110
        $this->classMap[$type] = $className;
111 1
    }
112 1
}
113
114