ExtendedUtility::addHooks()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * Utility functions for the Autoloader.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Utility;
9
10
use TYPO3\CMS\Core\Utility\GeneralUtility;
11
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
12
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
13
use TYPO3\CMS\Extbase\Object\ObjectManager;
14
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
15
use TYPO3\CMS\Fluid\View\StandaloneView;
16
17
/**
18
 * Utility functions for the Autoloader.
19
 */
20
class ExtendedUtility
21
{
22
    /**
23
     * Create a object with the given class name.
24
     *
25
     * @param string $className
26
     *
27
     * @return object
28
     */
29
    public static function create($className)
0 ignored issues
show
Unused Code introduced by
The parameter $className is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
    {
31
        $arguments = \func_get_args();
32
        $objManager = GeneralUtility::makeInstance(ObjectManager::class);
33
34
        return \call_user_func_array([
35
            $objManager,
36
            'get',
37
        ], $arguments);
38
    }
39
40
    /**
41
     * Get the query for the given class name oder object.
42
     *
43
     * @param string|object $objectName
44
     *
45
     * @return \TYPO3\CMS\Extbase\Persistence\QueryInterface
46
     */
47
    public static function getQuery($objectName)
48
    {
49
        $objectName = \is_object($objectName) ? \get_class($objectName) : $objectName;
50
        /** @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface $manager */
51
        static $manager = null;
52
        if (null === $manager) {
53
            $manager = self::create(PersistenceManagerInterface::class);
54
        }
55
56
        return $manager->createQueryForType($objectName);
57
    }
58
59
    /**
60
     * Add a xclass/object replacement.
61
     *
62
     * @param $source
63
     * @param $target
64
     *
65
     * @return bool
66
     */
67
    public static function addXclass($source, $target)
68
    {
69
        if (isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][$source])) {
70
            $message = 'Double registration of Xclass for ' . $source;
71
            $message .= ' (' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][$source]['className'] . ' and ' . $target . ')';
72
            self::log($message);
73
74
            return false;
75
        }
76
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][$source] = [
77
            'className' => $target,
78
        ];
79
80
        return true;
81
    }
82
83
    /**
84
     * Log into the TYPO3_CONF_VARS to get more information in the backend.
85
     *
86
     * @param $message
87
     */
88
    public static function log($message): void
89
    {
90
        if (!\is_array($GLOBALS['TYPO3_CONF_VARS']['AUTOLOADER']['Log'])) {
91
            $GLOBALS['TYPO3_CONF_VARS']['AUTOLOADER']['Log'] = [];
92
        }
93
        $GLOBALS['TYPO3_CONF_VARS']['AUTOLOADER']['Log'][] = $message;
94
    }
95
96
    /**
97
     * Add a hooks.
98
     *
99
     * @param string $configuration
100
     */
101
    public static function addHooks(array $locations, $configuration): void
102
    {
103
        foreach ($locations as $location) {
104
            self::addHook($location, $configuration);
105
        }
106
    }
107
108
    /**
109
     * Add a hook.
110
     *
111
     * @param string $location      The location of the hook separated bei pipes
112
     * @param string $configuration
113
     */
114
    public static function addHook($location, $configuration): void
115
    {
116
        $location = explode('|', $location);
117
        array_push($location, 'via_autoloader_' . GeneralUtility::shortMD5($configuration));
118
        ArrayUtility::setNodes([implode('|', $location) => $configuration], $GLOBALS);
119
    }
120
121
    /**
122
     * Create a StandaloneView for a extension context.
123
     *
124
     * @param string $extensionKey
125
     * @param string $templatePath
126
     *
127
     * @return \TYPO3\CMS\Fluid\View\StandaloneView
128
     */
129
    public static function createExtensionStandaloneView($extensionKey, $templatePath)
130
    {
131
        $templatePath = GeneralUtility::getFileAbsFileName($templatePath);
132
133
        /** @var \TYPO3\CMS\Fluid\View\StandaloneView $view */
134
        $view = self::create(StandaloneView::class);
135
        $view->setTemplatePathAndFilename($templatePath);
136
137
        // Get configuration
138
        $objectManager = new ObjectManager();
139
        /** @var ConfigurationManager $configurationManager */
140
        $configurationManager = $objectManager->get(ConfigurationManager::class);
141
        $configuration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
142
        $viewConfiguration = [];
143
        if (isset($configuration['module.']['tx_autoloader.']['view.'])) {
144
            $viewConfiguration = $configuration['module.']['tx_autoloader.']['view.'];
145
        }
146
147
        $layoutRootPaths = \is_array($viewConfiguration['layoutRootPaths.']) ? $viewConfiguration['layoutRootPaths.'] : [];
148
        if (!isset($layoutRootPaths[5])) {
149
            $layoutRootPaths[5] = 'EXT:' . $extensionKey . '/Resources/Private/Layouts/';
150
        }
151
        $view->setLayoutRootPaths($layoutRootPaths);
152
153
        $partialRootPaths = \is_array($viewConfiguration['partialRootPaths.']) ? $viewConfiguration['partialRootPaths.'] : [];
154
        if (!isset($partialRootPaths[5])) {
155
            $partialRootPaths[5] = 'EXT:' . $extensionKey . '/Resources/Private/Partials/';
156
        }
157
        $view->setPartialRootPaths($partialRootPaths);
158
159
        return $view;
160
    }
161
}
162