IncludeFileViewHelper.php 2.67 KB
Newer Older
Dainis Abols's avatar
Dainis Abols committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php

namespace Lu\LuStudyCatalogue\ViewHelpers;

use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Resource\FilePathSanitizer;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
use TYPO3Fluid\Fluid\Core\ViewHelper\ViewHelperInterface;

/**
 * Class IncludeFileViewHelper
 *
 * @author  Dainis Abols <dainis.abols@lu.lv>
 * @owner   University of Latvia
 * @version 2.1.0
 * @since   14.08.2018
 *
 * @package Lu\LuStudyCatalogue\ViewHelpers
 */
class IncludeFileViewHelper extends AbstractViewHelper implements ViewHelperInterface
{
    use CompileWithRenderStatic;

    /**
     * Initialize arguments
     */
    public function initializeArguments()
    {
        $this->registerArgument('path', 'string', 'Path to the CSS/JS file which should be included', true);
        $this->registerArgument('compress', 'bool', 'Define if file should be compressed', false, false);
    }

    /**
     * @param array                                                     $arguments
     * @param \Closure                                                  $renderChildrenClosure
     * @param \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface $renderingContext
     *
     * @return mixed|string|void
     */
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
    {
        $path         = $arguments['path'];
        $fileExists   = file_exists(GeneralUtility::getFileAbsFileName($path));
        $compress     = (bool)$arguments['compress'];
        $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);

        if (!$fileExists) {
            return;
        }

        if (TYPO3_MODE === 'FE') {
            $sanitizer = GeneralUtility::makeInstance(FilePathSanitizer::class);
            $path = $sanitizer->sanitize($path);

            // JS
            if (strtolower(substr($path, -3)) === '.js') {
                $pageRenderer->addJsFooterFile($path, null, $compress);

                // CSS
            } else if (strtolower(substr($path, -4)) === '.css') {
                $pageRenderer->addCssFile($path, 'stylesheet', 'all', '', $compress);
            }
        } else {
            // JS
            if (strtolower(substr($path, -3)) === '.js') {
                $pageRenderer->addJsFooterFile($path, null, $compress);

                // CSS
            } else if (strtolower(substr($path, -4)) === '.css') {
                $pageRenderer->addCssFile($path, 'stylesheet', 'all', '', $compress);
            }
        }
    }
}