This repository has been archived by the owner on May 3, 2022. It is now read-only.
/
ComboLoader.php
233 lines (199 loc) · 6.01 KB
/
ComboLoader.php
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* File containing the ComboLoader class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformUIBundle\Loader;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use Symfony\Component\Templating\EngineInterface;
/**
* ComboLoader combines JS or CSS files.
*/
class ComboLoader implements Loader
{
/**
* @var string Path to YUI build directory.
*/
private $yuiBuildDirectory;
/**
* @var string Path to web directory.
*/
private $webDirectory;
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;
/** @var \Symfony\Component\Templating\EngineInterface */
private $templating;
/**
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
* @param \Symfony\Component\Templating\EngineInterface $templating
* @param string $yuiBuildDirectory
* @param string $webDirectory
*/
public function __construct(ConfigResolverInterface $configResolver, EngineInterface $templating, $yuiBuildDirectory, $webDirectory)
{
$this->configResolver = $configResolver;
$this->templating = $templating;
$this->yuiBuildDirectory = $yuiBuildDirectory;
$this->webDirectory = $webDirectory;
}
public function combineFilesContent(array $files, $version = '')
{
$content = '';
$files = $this->sanitizeFilenames($files, $version);
foreach ($files as $file) {
$ext = $this->findFileExtension($file);
if ($ext === Loader::EXT_JS) {
if ($this->isTemplateFile($file)) {
$content .= $this->renderTemplate($file);
} else {
if ($this->isYuiFile($file)) {
$file = $this->fixUnresolvedPath($file);
}
$content .= $this->loadFile($file, $ext);
}
} elseif ($ext === Loader::EXT_CSS) {
$file = $this->fixUnresolvedPath($file);
$content .= $this->loadFile($file, $ext);
} else {
throw new NotFoundException('file type', $file);
}
}
return $content;
}
public function getCombinedFilesContentType(array $files, $version = '')
{
$files = $this->sanitizeFilenames($files, $version);
$this->assertAtLeastOneFile($files);
$file = $files[0];
$ext = $this->findFileExtension($file);
if ($ext === Loader::EXT_JS) {
$type = 'application/javascript';
} elseif ($ext === Loader::EXT_CSS) {
$type = 'text/css';
} else {
throw new NotFoundException('file type', $file);
}
return $type;
}
/**
* Asserts that files array is not empty.
*
* @param array $files
*
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
*/
private function assertAtLeastOneFile(array $files)
{
if (empty($files)) {
throw new InvalidArgumentException('files', 'can not be empty');
}
}
/**
* Returns true if JS file is YUI file.
*
* @param $filename
*
* @return bool
*/
private function isYuiFile($filename)
{
return $filename[0] !== '/';
}
/**
* Fixes unresolved path.
*
* @param $filename
*
* @return string
*/
private function fixUnresolvedPath($filename)
{
return $this->yuiBuildDirectory . $filename;
}
/**
* Fixes filename for JS and CSS files.
*
* Replace underscores by dots.
*
* @param $filename
* @param $ext
*
* @return string
*/
private function fixFilename($filename, $ext)
{
return $this->webDirectory . str_replace('_' . $ext, '.' . $ext, $filename);
}
/**
* Reads file from disk.
*
* @param $file
* @param $ext
*
* @return string
* @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
*/
private function loadFile($file, $ext)
{
$filename = $this->fixFilename($file, $ext);
if (!is_readable($filename)) {
throw new NotFoundException('file', $file);
}
return file_get_contents($filename);
}
/**
* Returns file extension.
*
* @param $file
*
* @return string
*/
private function findFileExtension($file)
{
return substr(strrchr($file, '_'), 1);
}
private function isTemplateFile($file)
{
return preg_match('#/tpl/handlebars/.*_js#', $file) === 1;
}
private function renderTemplate($file)
{
preg_match('#/tpl/handlebars/(.*)_js#', $file, $matches);
$module = $matches[1];
$path = $this->getModuleSetting($module, 'path');
return $this->templating->render(
'eZPlatformUIBundle:Template:wraptemplate.js.twig',
[
'path' => $path,
'module' => $module,
'templateCode' => file_get_contents($path),
]
);
}
private function getModuleSetting($module, $setting)
{
return $this->configResolver->getParameter("yui.modules.$module.$setting", 'ez_platformui');
}
/**
* Removes version string (if any) from file paths.
*
* @param array $files
* @param string $version
*
* @return array
*/
private function sanitizeFilenames(array $files, $version)
{
$filesWithoutVersion = array_map(function ($file) use ($version) {
if (preg_match('/\?' . preg_quote($version, '/') . '$/', $file)) {
return substr($file, 0, -(strlen($version) + 1));
}
return $file;
}, $files);
return $filesWithoutVersion;
}
}