IconUtility::addTcaTypeIcon()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

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 14
ccs 0
cts 10
cp 0
rs 9.7998
cc 2
nc 2
nop 3
crap 6
1
<?php
2
3
/**
4
 * Icon helper.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Utility;
9
10
use TYPO3\CMS\Core\Imaging\IconProvider\BitmapIconProvider;
11
use TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider;
12
use TYPO3\CMS\Core\Imaging\IconRegistry;
13
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Core\Utility\PathUtility;
16
use TYPO3\CMS\Core\Utility\StringUtility;
17
18
/**
19
 * Icon helper.
20
 */
21
class IconUtility
22
{
23
    /**
24
     * Add the given icon to the TCA table type.
25
     *
26
     * @param string $table
27
     * @param string $type
28
     * @param string $icon
29
     */
30
    public static function addTcaTypeIcon($table, $type, $icon): void
31
    {
32
        $fullIconPath = mb_substr(PathUtility::getAbsoluteWebPath($icon), 1);
33
        if (StringUtility::endsWith(mb_strtolower($fullIconPath), 'svg')) {
34
            $iconProviderClass = SvgIconProvider::class;
35
        } else {
36
            $iconProviderClass = BitmapIconProvider::class;
37
        }
38
        /** @var IconRegistry $iconRegistry */
39
        $iconRegistry = GeneralUtility::makeInstance(IconRegistry::class);
40
        $iconIdentifier = 'tcarecords-' . $table . '-' . $type;
41
        $iconRegistry->registerIcon($iconIdentifier, $iconProviderClass, ['source' => $fullIconPath]);
42
        $GLOBALS['TCA']['tt_content']['ctrl']['typeicon_classes'][$type] = $iconIdentifier;
43
    }
44
45
    /**
46
     * Get the relative path of the extension icon.
47
     *
48
     * @param string $extensionKey
49
     * @param bool   $extSyntax    Get the EXT: Syntax instead of a rel Path
50
     *
51
     * @return string
52
     */
53
    public static function getByExtensionKey($extensionKey, $extSyntax = false)
54
    {
55
        $fileExtension = self::getIconFileExtension(ExtensionManagementUtility::extPath($extensionKey) . 'Resources/Public/Icons/Extension.');
56
        if ($fileExtension) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fileExtension of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
57
            return self::returnRelativeIconPath($extensionKey, 'Resources/Public/Icons/Extension.' . $fileExtension, $extSyntax);
58
        }
59
        $fileExtension = self::getIconFileExtension(ExtensionManagementUtility::extPath($extensionKey) . 'ext_icon.');
60
        if ($fileExtension) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fileExtension of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
61
            return self::returnRelativeIconPath($extensionKey, 'ext_icon.' . $fileExtension, $extSyntax);
62
        }
63
64
        return self::getByExtensionKey('autoloader');
65
    }
66
67
    /**
68
     * Get the absolute table icon for the given model name.
69
     *
70
     * @param string $modelClassName
71
     * @param bool   $extSyntax      Get the EXT: Syntax instead of a rel Path
72
     *
73
     * @return string
74
     */
75
    public static function getByModelName($modelClassName, $extSyntax = false)
76
    {
77
        $modelInformation = ClassNamingUtility::explodeObjectModelName($modelClassName);
78
79
        $extensionKey = GeneralUtility::camelCaseToLowerCaseUnderscored($modelInformation['extensionName']);
80
        $modelName = str_replace('\\', '/', $modelInformation['modelName']);
81
82
        $tableIconPath = ExtensionManagementUtility::extPath($extensionKey) . 'Resources/Public/Icons/' . $modelName . '.';
83
        $fileExtension = self::getIconFileExtension($tableIconPath);
84
        if ($fileExtension) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fileExtension of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
85
            return self::returnRelativeIconPath(
86
                $extensionKey,
87
                'Resources/Public/Icons/' . $modelName . '.' . $fileExtension,
88
                $extSyntax
89
            );
90
        }
91
92
        return self::getByExtensionKey($extensionKey, $extSyntax);
93
    }
94
95
    /**
96
     * Get the file extension (svg,png,gif) of the absolute path. The path is
97
     * without file extension but incl. the dot. e.g.:
98
     * "/test/icon.".
99
     *
100
     * @param string $absolutePathWithoutExtension
101
     *
102
     * @return string
103
     */
104
    public static function getIconFileExtension($absolutePathWithoutExtension)
105
    {
106
        $fileExtensions = [
107
            'svg',
108
            'png',
109
            'gif',
110
            'jpg',
111
        ];
112
        foreach ($fileExtensions as $fileExtension) {
113
            if (is_file($absolutePathWithoutExtension . $fileExtension)) {
114
                return $fileExtension;
115
            }
116
        }
117
118
        return false;
119
    }
120
121
    /**
122
     * Return the right relative path.
123
     *
124
     * @param string $extensionKey
125
     * @param string $path
126
     * @param bool   $extSyntax
127
     *
128
     * @return string
129
     */
130
    protected static function returnRelativeIconPath($extensionKey, $path, $extSyntax = false)
131
    {
132
        $extSyntaxPath = 'EXT:' . $extensionKey . '/' . $path;
133
        if ($extSyntax) {
134
            return $extSyntaxPath;
135
        }
136
137
        return PathUtility::getAbsoluteWebPath(GeneralUtility::getFileAbsFileName($extSyntaxPath));
138
    }
139
}
140