Completed
Branch master (d17104)
by Christian
21:20
created

ResourceUtility   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getJsTag() 0 9 1
A getCssTag() 0 9 1
A getResources() 0 10 1
A getAdditionalResourcesForModules() 0 22 6
A getAdminPanelStylesheet() 0 9 2
A getAdditionalResourcesForModule() 0 13 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Adminpanel\Utility;
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License, either version 2
11
 * of the License, or any later version.
12
 *
13
 * For the full copyright and license information, please read the
14
 * LICENSE.txt file that was distributed with this source code.
15
 *
16
 * The TYPO3 project - inspiring people to share!
17
 */
18
19
use TYPO3\CMS\Adminpanel\ModuleApi\ResourceProviderInterface;
20
use TYPO3\CMS\Adminpanel\ModuleApi\SubmoduleProviderInterface;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Core\Utility\PathUtility;
23
24
class ResourceUtility
25
{
26
    /**
27
     * Get additional resources (css, js) from modules and merge it to
28
     * one array - returns an array of full html tags
29
     *
30
     * @param \TYPO3\CMS\Adminpanel\ModuleApi\ResourceProviderInterface[] $modules
31
     * @return array
32
     */
33
    public static function getAdditionalResourcesForModules(array $modules): array
34
    {
35
        $result = [
36
            'js' => '',
37
            'css' => '',
38
        ];
39
        foreach ($modules as $module) {
40
            if ($module instanceof ResourceProviderInterface) {
41
                foreach ($module->getJavaScriptFiles() as $file) {
42
                    $result['js'] .= static::getJsTag($file);
43
                }
44
                foreach ($module->getCssFiles() as $file) {
45
                    $result['css'] .= static::getCssTag($file);
46
                }
47
            }
48
            if ($module instanceof SubmoduleProviderInterface) {
49
                $subResult = self::getAdditionalResourcesForModules($module->getSubModules());
50
                $result['js'] .= $subResult['js'];
51
                $result['css'] .= $subResult['css'];
52
            }
53
        }
54
        return $result;
55
    }
56
57
    public static function getAdditionalResourcesForModule(ResourceProviderInterface $module): array
58
    {
59
        $result = [
60
            'js' => '',
61
            'css' => '',
62
        ];
63
        foreach ($module->getJavaScriptFiles() as $file) {
64
            $result['js'] .= static::getJsTag($file);
65
        }
66
        foreach ($module->getCssFiles() as $file) {
67
            $result['css'] .= static::getCssTag($file);
68
        }
69
        return $result;
70
    }
71
72
    /**
73
     * Returns a link tag with the admin panel stylesheet
74
     * defined using TBE_STYLES
75
     *
76
     * @return string
77
     */
78
    protected static function getAdminPanelStylesheet(): string
79
    {
80
        $result = '';
81
        if (!empty($GLOBALS['TBE_STYLES']['stylesheets']['admPanel'])) {
82
            $stylesheet = GeneralUtility::locationHeaderUrl($GLOBALS['TBE_STYLES']['stylesheets']['admPanel']);
83
            $result = '<link rel="stylesheet" type="text/css" href="' .
84
                      htmlspecialchars($stylesheet, ENT_QUOTES | ENT_HTML5) . '" />';
85
        }
86
        return $result;
87
    }
88
89
    /**
90
     * Get a css tag for file - with absolute web path resolving
91
     *
92
     * @param string $cssFileLocation
93
     * @return string
94
     */
95
    protected static function getCssTag(string $cssFileLocation): string
96
    {
97
        $css = '<link type="text/css" rel="stylesheet" href="' .
98
               htmlspecialchars(
99
                   PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($cssFileLocation)),
100
                   ENT_QUOTES | ENT_HTML5
101
               ) .
102
               '" media="all" />';
103
        return $css;
104
    }
105
106
    /**
107
     * Get a script tag for JavaScript with absolute paths
108
     *
109
     * @param string $jsFileLocation
110
     * @return string
111
     */
112
    protected static function getJsTag(string $jsFileLocation): string
113
    {
114
        $js = '<script type="text/javascript" src="' .
115
              htmlspecialchars(
116
                  PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($jsFileLocation)),
117
                  ENT_QUOTES | ENT_HTML5
118
              ) .
119
              '"></script>';
120
        return $js;
121
    }
122
123
    /**
124
     * Return a string with tags for main admin panel resources
125
     *
126
     * @return array
127
     */
128
    public static function getResources(): array
129
    {
130
        $jsFileLocation = 'EXT:adminpanel/Resources/Public/JavaScript/AdminPanel.js';
131
        $js = ResourceUtility::getJsTag($jsFileLocation);
132
        $cssFileLocation = 'EXT:adminpanel/Resources/Public/Css/adminpanel.css';
133
        $css = ResourceUtility::getCssTag($cssFileLocation);
134
135
        return [
136
            'css' => $css . ResourceUtility::getAdminPanelStylesheet(),
137
            'js' => $js,
138
        ];
139
    }
140
}
141