Completed
Push — cleanup-service ( 60a2d8...0f35f3 )
by Romain
02:18
created

StringService::getExtensionRelativePath()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 7
nc 4
nop 1
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Formz project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Service;
15
16
use Romm\Formz\Service\Traits\ExtendedFacadeInstanceTrait;
17
use TYPO3\CMS\Core\SingletonInterface;
18
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Core\Utility\PathUtility;
21
use TYPO3\CMS\Extbase\Service\EnvironmentService;
22
23
class StringService implements SingletonInterface
24
{
25
    use ExtendedFacadeInstanceTrait;
26
27
    /**
28
     * @var EnvironmentService
29
     */
30
    protected $environmentService;
31
32
    /**
33
     * @param string|null $path If a string is given, it will be precessed by the extension relative path and returned.
34
     * @return string
35
     */
36
    public function getExtensionRelativePath($path = null)
37
    {
38
        $relativePath = ExtensionManagementUtility::siteRelPath('formz');
39
40
        if ($this->environmentService->isEnvironmentInBackendMode()) {
41
            $relativePath = '../' . $relativePath;
42
        }
43
44
        return (null !== $path)
45
            ? $relativePath . $path
46
            : $relativePath;
47
    }
48
49
    /**
50
     * @param string $path
51
     * @return string
52
     */
53
    public function getResourceRelativePath($path)
54
    {
55
        $relativePath = rtrim(
56
            PathUtility::getRelativePath(
57
                GeneralUtility::getIndpEnv('TYPO3_DOCUMENT_ROOT'),
58
                GeneralUtility::getFileAbsFileName($path)
59
            ),
60
            '/'
61
        );
62
63
        if ($this->environmentService->isEnvironmentInBackendMode()) {
64
            $relativePath = '../' . $relativePath;
65
        }
66
67
        return $relativePath;
68
    }
69
70
    /**
71
     * Sanitizes a string: lower case with dash separation.
72
     *
73
     * @param string $string
74
     * @return string
75
     */
76
    public function sanitizeString($string)
77
    {
78
        $string = str_replace('_', '-', GeneralUtility::camelCaseToLowerCaseUnderscored($string));
79
80
        while (strpos($string, '--')) {
81
            $string = str_replace('--', '-', $string);
82
        }
83
84
        return $string;
85
    }
86
87
    /**
88
     * @param EnvironmentService $environmentService
89
     */
90
    public function injectEnvironmentService(EnvironmentService $environmentService)
91
    {
92
        $this->environmentService = $environmentService;
93
    }
94
}
95