Completed
Push — unit-test-form-view-helper ( 21ce8b...04fc41 )
by Romain
04:37
created

FacadeService::getInstance()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
rs 9.6666
c 1
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Formz project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Service;
15
16
use Romm\Formz\Core\Core;
17
use TYPO3\CMS\Core\SingletonInterface;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20
class FacadeService implements SingletonInterface
21
{
22
    /**
23
     * @var FacadeService
24
     */
25
    private static $instance;
26
27
    /**
28
     * @var array
29
     */
30
    protected $facadeInstances = [];
31
32
    /**
33
     * @return FacadeService
34
     */
35
    public static function get()
36
    {
37
        if (null === self::$instance) {
38
            self::$instance = new self;
39
        }
40
41
        return self::$instance;
42
    }
43
44
    /**
45
     * @param string $className
46
     * @param bool   $useObjectManager
47
     * @return object
48
     */
49
    public function getInstance($className, $useObjectManager = false)
50
    {
51
        if (null === $this->facadeInstances[$className]) {
52
            $this->facadeInstances[$className] = $useObjectManager
53
                ? Core::instantiate($className)
54
                : GeneralUtility::makeInstance($className);
55
        }
56
        return $this->facadeInstances[$className];
57
    }
58
59
    /**
60
     * Used in unit tests.
61
     */
62
    public function reset()
63
    {
64
        foreach (array_keys($this->facadeInstances) as $className) {
65
            if ($className !== self::class) {
66
                unset($this->facadeInstances[$className]);
67
            }
68
        }
69
    }
70
71
    /**
72
     * Used in unit tests.
73
     *
74
     * @param string $className
75
     * @param object $instance
76
     */
77
    public function forceInstance($className, $instance)
78
    {
79
        $this->facadeInstances[$className] = $instance;
80
    }
81
}
82