BackendLayoutProvider::createBackendLayout()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 1
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * Add backend layouts.
5
 */
6
declare(strict_types = 1);
7
8
namespace HDNET\Autoloader\Hooks;
9
10
use HDNET\Autoloader\Annotation\Hook;
11
use TYPO3\CMS\Backend\View\BackendLayout\BackendLayout;
12
use TYPO3\CMS\Backend\View\BackendLayout\BackendLayoutCollection;
13
use TYPO3\CMS\Backend\View\BackendLayout\DataProviderContext;
14
use TYPO3\CMS\Backend\View\BackendLayout\DataProviderInterface;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
17
/**
18
 * Add backend layouts.
19
 *
20
 * @see  https://github.com/georgringer/TYPO3.base/blob/master/typo3conf/ext/theme/Classes/View/BackendLayout/FileProvider.php
21
 * @Hook("TYPO3_CONF_VARS|SC_OPTIONS|BackendLayoutDataProvider")
22
 */
23
class BackendLayoutProvider implements DataProviderInterface
24
{
25
    /**
26
     * Layout information.
27
     *
28
     * @var array
29
     */
30
    protected static $backendLayoutInformation = [];
31
32
    /**
33
     * Add one backend layout information.
34
     */
35
    public static function addBackendLayoutInformation(array $backendLayout): void
36
    {
37
        self::$backendLayoutInformation[] = $backendLayout;
38
    }
39
40
    /**
41
     * Adds backend layouts to the given backend layout collection.
42
     */
43
    public function addBackendLayouts(DataProviderContext $dataProviderContext, BackendLayoutCollection $backendLayoutCollection): void
44
    {
45
        foreach (self::$backendLayoutInformation as $info) {
46
            $backendLayoutCollection->add($this->createBackendLayout($info));
47
        }
48
    }
49
50
    /**
51
     * Gets a backend layout by (regular) identifier.
52
     *
53
     * @param string $identifier
54
     * @param int    $pageId
55
     *
56
     * @return BackendLayout|null
57
     */
58
    public function getBackendLayout($identifier, $pageId)
59
    {
60
        foreach (self::$backendLayoutInformation as $info) {
61
            if ($this->getIdentifier($info) === $identifier) {
62
                return $this->createBackendLayout($info);
63
            }
64
        }
65
    }
66
67
    /**
68
     * Create a backend layout with the given information.
69
     *
70
     * @param $info
71
     *
72
     * @return mixed
73
     */
74
    protected function createBackendLayout($info)
75
    {
76
        $fileName = GeneralUtility::getFileAbsFileName($info['path']);
77
        $backendLayout = BackendLayout::create($this->getIdentifier($info), $info['label'], GeneralUtility::getUrl($fileName));
78
        if ($info['icon']) {
79
            $backendLayout->setIconPath(str_replace(PATH_site, '', $info['icon']));
80
        }
81
82
        return $backendLayout;
83
    }
84
85
    /**
86
     * Get identifier.
87
     *
88
     * @param $info
89
     *
90
     * @return string
91
     */
92
    protected function getIdentifier($info)
93
    {
94
        return $info['extension'] . '/' . $info['filename'];
95
    }
96
}
97