Completed
Push — master ( dfd5cb...5cac00 )
by Tim
09:24
created

Classes/Utility/ExtendedUtility.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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)
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 array  $locations
100
     * @param string $configuration
101
     */
102
    public static function addHooks(array $locations, $configuration)
103
    {
104
        foreach ($locations as $location) {
105
            self::addHook($location, $configuration);
106
        }
107
    }
108
109
    /**
110
     * Add a hook.
111
     *
112
     * @param string $location      The location of the hook separated bei pipes
113
     * @param string $configuration
114
     */
115
    public static function addHook($location, $configuration)
116
    {
117
        $location = \explode('|', $location);
118
        \array_push($location, 'via_autoloader_' . GeneralUtility::shortMD5($configuration));
119
        ArrayUtility::setNodes([\implode('|', $location) => $configuration], $GLOBALS);
120
    }
121
122
    /**
123
     * Create a StandaloneView for a extension context.
124
     *
125
     * @param string $extensionKey
126
     * @param string $templatePath
127
     *
128
     * @return \TYPO3\CMS\Fluid\View\StandaloneView
129
     */
130
    public static function createExtensionStandaloneView($extensionKey, $templatePath)
131
    {
132
        $templatePath = GeneralUtility::getFileAbsFileName($templatePath);
133
134
        /** @var \TYPO3\CMS\Fluid\View\StandaloneView $view */
135
        $view = self::create(StandaloneView::class);
136
        $view->setTemplatePathAndFilename($templatePath);
137
138
        // Get configuration
139
        $objectManager = new ObjectManager();
140
        /** @var ConfigurationManager $configurationManager */
141
        $configurationManager = $objectManager->get(ConfigurationManager::class);
142
        $configuration = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
143
        $viewConfiguration = [];
144
        if (isset($configuration['module.']['tx_autoloader.']['view.'])) {
145
            $viewConfiguration = $configuration['module.']['tx_autoloader.']['view.'];
146
        }
147
148
        $layoutRootPaths = \is_array($viewConfiguration['layoutRootPaths.']) ? $viewConfiguration['layoutRootPaths.'] : [];
149
        if (!isset($layoutRootPaths[5])) {
150
            $layoutRootPaths[5] = 'EXT:' . $extensionKey . '/Resources/Private/Layouts/';
151
        }
152
        $view->setLayoutRootPaths($layoutRootPaths);
153
154
        $partialRootPaths = \is_array($viewConfiguration['partialRootPaths.']) ? $viewConfiguration['partialRootPaths.'] : [];
155
        if (!isset($partialRootPaths[5])) {
156
            $partialRootPaths[5] = 'EXT:' . $extensionKey . '/Resources/Private/Partials/';
157
        }
158
        $view->setPartialRootPaths($partialRootPaths);
159
160
        return $view;
161
    }
162
}
163