Completed
Push — feature/middleware ( f1bcd3...4630a4 )
by Romain
11:52
created

Core::getDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Core;
15
16
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
17
use TYPO3\CMS\Core\SingletonInterface;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use TYPO3\CMS\Extbase\Object\ObjectManager;
20
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
21
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
22
23
/**
24
 * Class containing general functions.
25
 */
26
class Core implements SingletonInterface
27
{
28
    /**
29
     * @var Core
30
     */
31
    protected static $instance;
32
33
    /**
34
     * @var ObjectManagerInterface
35
     */
36
    protected $objectManager;
37
38
    /**
39
     * @return Core
40
     */
41
    public static function get()
42
    {
43
        if (null === self::$instance) {
44
            /** @var ObjectManager $objectManager */
45
            $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
46
47
            self::$instance = $objectManager->get(self::class);
48
        }
49
50
        return self::$instance;
51
    }
52
53
    /**
54
     * Shortcut for object manager `get()` function.
55
     *
56
     * @param string $className
57
     * @return object
58
     */
59
    public static function instantiate($className)
60
    {
61
        $objectManager = self::get()->getObjectManager();
62
        $args = func_get_args();
63
64
        if (1 === count($args)) {
65
            return $objectManager->get($className);
66
        }
67
68
        return call_user_func_array([$objectManager, 'get'], $args);
69
    }
70
71
    /**
72
     * @return ObjectManagerInterface
73
     */
74
    public function getObjectManager()
75
    {
76
        return $this->objectManager;
77
    }
78
79
    /**
80
     * @param ObjectManagerInterface $objectManager
81
     */
82
    public function injectObjectManager(ObjectManagerInterface $objectManager)
83
    {
84
        $this->objectManager = $objectManager;
85
    }
86
87
    /**
88
     * @return TypoScriptFrontendController
89
     */
90
    public function getPageController()
91
    {
92
        return $GLOBALS['TSFE'];
93
    }
94
95
    /**
96
     * @return BackendUserAuthentication
97
     */
98
    public function getBackendUser()
99
    {
100
        return $GLOBALS['BE_USER'];
101
    }
102
}
103