Passed
Push — master ( af9933...6d2375 )
by Torben
04:41 queued 01:21
created

FluidStandaloneService::getTemplateFolders()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 24
nop 1
dl 0
loc 29
ccs 0
cts 21
cp 0
crap 56
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Service;
13
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
16
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
17
use TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException;
18
use TYPO3\CMS\Fluid\View\StandaloneView;
19
20
/**
21
 * FluidStandaloneService
22
 */
23
class FluidStandaloneService
24
{
25
    protected ConfigurationManager $configurationManager;
26
27
    public function injectConfigurationManager(ConfigurationManager $configurationManager)
28
    {
29
        $this->configurationManager = $configurationManager;
30
    }
31
32
    /**
33
     * Returns the template folders for the given part
34
     *
35
     * @param string $part
36
     * @throws InvalidConfigurationTypeException
37
     * @return array
38
     */
39
    public function getTemplateFolders(string $part = 'template'): array
40
    {
41
        $extbaseConfig = $this->configurationManager->getConfiguration(
42
            ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
43
            'SfEventMgt'
44
        );
45
46
        if (!empty($extbaseConfig['view'][$part . 'RootPaths'])) {
47
            $templatePaths = $extbaseConfig['view'][$part . 'RootPaths'];
48
            ksort($templatePaths);
49
        }
50
        if (empty($templatePaths) && isset($extbaseConfig['view'])) {
51
            $path = $extbaseConfig['view'][$part . 'RootPath'];
52
            if (!empty($path)) {
53
                $templatePaths = [];
54
                $templatePaths[] = $path;
55
            }
56
        }
57
        if (empty($templatePaths)) {
58
            $templatePaths = [];
59
            $templatePaths[] = 'EXT:sf_event_mgt/Resources/Private/' . ucfirst($part) . 's/';
60
        }
61
62
        $absolutePaths = [];
63
        foreach ($templatePaths as $templatePath) {
64
            $absolutePaths[] = GeneralUtility::getFileAbsFileName($this->ensureSuffixedPath($templatePath));
65
        }
66
67
        return $absolutePaths;
68
    }
69
70
    /**
71
     * Makes sure the path ends with a slash
72
     *
73
     * @param string $path
74
     * @return string
75
     */
76
    protected function ensureSuffixedPath(string $path): string
77
    {
78
        return rtrim($path, '/') . '/';
79
    }
80
81
    /**
82
     * Renders a fluid standlone view for the given template
83
     *
84
     * @param string $template
85
     * @param array $variables
86
     * @param string $extensionName
87
     * @param string $pluginName
88
     * @return string
89
     */
90
    public function renderTemplate(
91
        string $template,
92
        array $variables,
93
        string $extensionName = 'SfEventMgt',
94
        string $pluginName = 'Pieventregistration'
95
    ): string {
96
        $emailView = GeneralUtility::makeInstance(StandaloneView::class);
97
        $emailView->getRequest()->setControllerExtensionName($extensionName);
98
        $emailView->getRequest()->setPluginName($pluginName);
99
        $emailView->setFormat('html');
100
        $emailView->setTemplateRootPaths($this->getTemplateFolders('template'));
101
        $emailView->setLayoutRootPaths($this->getTemplateFolders('layout'));
102
        $emailView->setPartialRootPaths($this->getTemplateFolders('partial'));
103
        $emailView->setTemplate($template);
104
        $emailView->assignMultiple($variables);
105
        return $emailView->render();
106
    }
107
108
    /**
109
     * Parses the given string with Fluid View
110
     *
111
     * @param string $string Any string
112
     * @param array $variables Variables
113
     * @return string Parsed string
114
     */
115
    public function parseStringFluid(string $string, array $variables = []): string
116
    {
117
        if ($string === '') {
118
            return '';
119
        }
120
        $standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
121
        $standaloneView->setTemplateSource($string);
122
        $standaloneView->assignMultiple($variables);
123
124
        return $standaloneView->render() ?? '';
125
    }
126
}
127