Passed
Push — master ( da5c3a...138f6a )
by Torben
132:09 queued 128:49
created

FluidStandaloneService::getTemplateFolders()   A

Complexity

Conditions 6
Paths 24

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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