getJavaScriptAssetHandlerConnector()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\AssetHandler\Connector;
15
16
use Romm\Formz\AssetHandler\AssetHandlerFactory;
17
use Romm\Formz\Core\Core;
18
use Romm\Formz\Exceptions\FileCreationFailedException;
19
use Romm\Formz\Service\CacheService;
20
use TYPO3\CMS\Core\Page\PageRenderer;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23
/**
24
 * This manager is used to create instances of connectors, which will be used to
25
 * gather every asset used for a form, mainly JavaScript and CSS code.
26
 */
27
class AssetHandlerConnectorManager
28
{
29
    /**
30
     * @var AssetHandlerConnectorManager[]
31
     */
32
    private static $instances = [];
33
34
    /**
35
     * @var PageRenderer
36
     */
37
    private $pageRenderer;
38
39
    /**
40
     * @var AssetHandlerFactory
41
     */
42
    private $assetHandlerFactory;
43
44
    /**
45
     * @var JavaScriptAssetHandlerConnector
46
     */
47
    private $javaScriptAssetHandlerConnector;
48
49
    /**
50
     * @var CssAssetHandlerConnector
51
     */
52
    private $cssAssetHandlerConnector;
53
54
    /**
55
     * @var AssetHandlerConnectorStates
56
     */
57
    private $assetHandlerConnectorStates;
58
59
    /**
60
     * @param PageRenderer        $pageRenderer
61
     * @param AssetHandlerFactory $assetHandlerFactory
62
     */
63
    public function __construct(PageRenderer $pageRenderer, AssetHandlerFactory $assetHandlerFactory)
64
    {
65
        $this->pageRenderer = $pageRenderer;
66
        $this->assetHandlerFactory = $assetHandlerFactory;
67
        $this->cssAssetHandlerConnector = Core::instantiate(CssAssetHandlerConnector::class, $this);
68
        $this->javaScriptAssetHandlerConnector = Core::instantiate(JavaScriptAssetHandlerConnector::class, $this);
69
    }
70
71
    /**
72
     * @param PageRenderer        $pageRenderer
73
     * @param AssetHandlerFactory $assetHandlerFactory
74
     * @return AssetHandlerConnectorManager
75
     */
76
    public static function get(PageRenderer $pageRenderer, AssetHandlerFactory $assetHandlerFactory)
77
    {
78
        $hash = spl_object_hash($pageRenderer) . spl_object_hash($assetHandlerFactory);
79
80
        if (false === isset(self::$instances[$hash])) {
81
            self::$instances[$hash] = Core::instantiate(self::class, $pageRenderer, $assetHandlerFactory);
82
        }
83
84
        return self::$instances[$hash];
85
    }
86
87
    /**
88
     * Will take care of including internal FormZ JavaScript and CSS files. They
89
     * will be included only once, even if the view helper is used several times
90
     * in the same page.
91
     *
92
     * @return $this
93
     */
94
    public function includeDefaultAssets()
95
    {
96
        if (false === $this->assetHandlerConnectorStates->defaultAssetsWereIncluded()) {
97
            $this->assetHandlerConnectorStates->markDefaultAssetsAsIncluded();
98
99
            $this->getJavaScriptAssetHandlerConnector()->includeDefaultJavaScriptFiles();
100
            $this->getCssAssetHandlerConnector()->includeDefaultCssFiles();
101
        }
102
103
        return $this;
104
    }
105
106
    /**
107
     * Returns a file name based on the form object class name.
108
     *
109
     * @param string $prefix
110
     * @return string
111
     */
112
    public function getFormzGeneratedFilePath($prefix = '')
113
    {
114
        $formObject = $this->assetHandlerFactory->getFormObject();
115
        $formIdentifier = CacheService::get()->getFormCacheIdentifier($formObject->getClassName(), $formObject->getName());
116
        $prefix = (false === empty($prefix))
117
            ? $prefix . '-'
118
            : '';
119
120
        $identifier = substr(
121
            'fz-' . $prefix . $formIdentifier,
122
            0,
123
            22
124
        );
125
        $identifier .= '-' . md5($formObject->getHash() . $formObject->getName());
126
127
        return CacheService::GENERATED_FILES_PATH . $identifier;
128
    }
129
130
    /**
131
     * This function will check if the file at the given path exists. If it does
132
     * not, the callback is called to get the content of the file, which is put
133
     * in the created file.
134
     *
135
     * A boolean is returned: if the file did not exist, and it was created
136
     * without error, `true` is returned. Otherwise, `false` is returned.
137
     *
138
     * @param string   $relativePath
139
     * @param callable $callback
140
     * @return bool
141
     * @throws FileCreationFailedException
142
     */
143
    public function createFileInTemporaryDirectory($relativePath, callable $callback)
144
    {
145
        $result = false;
146
        $absolutePath = GeneralUtility::getFileAbsFileName($relativePath);
147
148
        if (false === $this->fileExists($absolutePath)) {
149
            $content = call_user_func($callback);
150
151
            $result = $this->writeTemporaryFile($absolutePath, $content);
152
153
            if (null !== $result) {
154
                throw FileCreationFailedException::fileCreationFailed($absolutePath, $result);
155
            }
156
        }
157
158
        return $result;
159
    }
160
161
    /**
162
     * This function is mocked in unit tests.
163
     *
164
     * @param string $absolutePath
165
     * @return bool
166
     */
167
    protected function fileExists($absolutePath)
168
    {
169
        return file_exists($absolutePath)
170
            && 0 !== filemtime($absolutePath);
171
    }
172
173
    /**
174
     * This function is mocked in unit tests.
175
     *
176
     * @param string $absolutePath
177
     * @param string $content
178
     * @return bool
179
     */
180
    protected function writeTemporaryFile($absolutePath, $content)
181
    {
182
        return GeneralUtility::writeFileToTypo3tempDir($absolutePath, $content);
183
    }
184
185
    /**
186
     * @return PageRenderer
187
     */
188
    public function getPageRenderer()
189
    {
190
        return $this->pageRenderer;
191
    }
192
193
    /**
194
     * @return AssetHandlerFactory
195
     */
196
    public function getAssetHandlerFactory()
197
    {
198
        return $this->assetHandlerFactory;
199
    }
200
201
    /**
202
     * @return AssetHandlerConnectorStates
203
     */
204
    public function getAssetHandlerConnectorStates()
205
    {
206
        return $this->assetHandlerConnectorStates;
207
    }
208
209
    /**
210
     * @return JavaScriptAssetHandlerConnector
211
     */
212
    public function getJavaScriptAssetHandlerConnector()
213
    {
214
        return $this->javaScriptAssetHandlerConnector;
215
    }
216
217
    /**
218
     * @return CssAssetHandlerConnector
219
     */
220
    public function getCssAssetHandlerConnector()
221
    {
222
        return $this->cssAssetHandlerConnector;
223
    }
224
225
    /**
226
     * @param AssetHandlerConnectorStates $assetHandlerConnectorStates
227
     */
228
    public function injectAssetHandlerConnectorStates(AssetHandlerConnectorStates $assetHandlerConnectorStates)
229
    {
230
        $this->assetHandlerConnectorStates = $assetHandlerConnectorStates;
231
    }
232
}
233