@@ -54,174 +54,174 @@ |
||
54 | 54 | '%node.word, %node.array, $basePath, $template->global->' . self::CONFIG_PROVIDER . '))'); |
55 | 55 | } |
56 | 56 | |
57 | - /** |
|
58 | - * @param string $asset Asset relative path |
|
59 | - * @param array $args Other macro arguments |
|
60 | - * @param string $basePath Base path |
|
61 | - * @param array $config Macro configuration |
|
62 | - * @return string |
|
63 | - */ |
|
57 | + /** |
|
58 | + * @param string $asset Asset relative path |
|
59 | + * @param array $args Other macro arguments |
|
60 | + * @param string $basePath Base path |
|
61 | + * @param array $config Macro configuration |
|
62 | + * @return string |
|
63 | + */ |
|
64 | 64 | public static function resolveAssetPath($asset, array $args, $basePath, array $config) { |
65 | - list($path, $format, $need) = self::processArguments($asset, $args); |
|
66 | - $wwwDir = Utils::normalizePath($config['wwwDir']); |
|
67 | - $manifest = self::resolveManifest($path, $need, $wwwDir, $config); |
|
68 | - $revision = $manifest === NULL ? NULL : self::resolveRevision($manifest, $path, $need, $config); |
|
69 | - |
|
70 | - // Is revision only version (query parameter) or full path to asset? |
|
71 | - $isVersion = $revision === NULL || strspn($revision,"./") === 0; |
|
72 | - |
|
73 | - // Check if asset exists |
|
74 | - if ($config['existCheck'] === TRUE) { |
|
75 | - $ds = DIRECTORY_SEPARATOR; |
|
76 | - $filePath = $isVersion ? ($wwwDir . $ds . $path) : ($wwwDir . $ds . Utils::normalizePath($revision)); |
|
77 | - if (! file_exists($filePath)) { |
|
78 | - $msg = sprintf("Asset '%s' not found.", $filePath); |
|
79 | - Utils::throwError(new AssetNotFoundException($msg), $config['missingAsset'], $need); |
|
80 | - return ''; |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - // Format output |
|
85 | - return self::formatOutput($format, $basePath, $path, $revision, $isVersion); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @param string $format Output format |
|
90 | - * @param string $basePath Base path |
|
91 | - * @param string $path Asset relative path |
|
92 | - * @param string|null $revision Asset revision (version or path to file) |
|
93 | - * @param bool $isVersion Is revision only version or full path? |
|
94 | - * @return string |
|
95 | - */ |
|
96 | - private static function formatOutput($format, $basePath, $path, $revision, $isVersion) |
|
97 | - { |
|
98 | - $revision = $revision ?: 'unknown'; |
|
99 | - $path = $isVersion ? |
|
100 | - sprintf("%s?v=%s", $path, $revision) : |
|
101 | - $revision; |
|
102 | - |
|
103 | - return Strings::replace($format, |
|
104 | - '/%([^%]+)%/', |
|
105 | - function ($matches) use ($basePath, $format, $path, $revision) { |
|
106 | - switch ($matches[1]) { |
|
107 | - case 'raw': |
|
108 | - return $revision; |
|
109 | - case 'basePath': |
|
110 | - return $basePath; |
|
111 | - case 'path': |
|
112 | - return $path; |
|
113 | - case 'url': |
|
114 | - return sprintf("%s/%s", $basePath, $path); |
|
115 | - default: |
|
116 | - $msg = sprintf( |
|
117 | - "Asset macro: Invalid variable '%s' in format '%s'. " . |
|
118 | - "Use one of allowed variables: %%raw%%, %%basePath%%, %%path%%, %%url%%.", |
|
119 | - $matches[1], |
|
120 | - $format |
|
121 | - ); |
|
122 | - throw new InvalidVariableException($msg); |
|
123 | - } |
|
124 | - }); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @param string $asset Asset path specified in macro |
|
129 | - * @param array $args Macro arguments |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - private static function processArguments($asset, array $args) |
|
133 | - { |
|
134 | - $format = isset($args['format']) ? $args['format'] : (isset($args[0]) ? $args[0] : '%url%'); |
|
135 | - $need = isset($args['need']) ? $args['need'] : (isset($args[1]) ? $args[1] : TRUE); |
|
136 | - |
|
137 | - Validators::assert($asset, 'string', 'path'); |
|
138 | - Validators::assert($format, 'string', 'format'); |
|
139 | - Validators::assert($need, 'bool', 'need'); |
|
140 | - |
|
141 | - $path = Utils::normalizePath($asset); |
|
142 | - |
|
143 | - return [$path, $format, $need]; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @param string $asset Asset path specified in macro |
|
148 | - * @param bool $need Fail if manifest doesn't exist? |
|
149 | - * @param string $wwwDir Public www dir |
|
150 | - * @param array $config Macro configuration |
|
151 | - * @return null|array |
|
152 | - */ |
|
153 | - private static function resolveManifest($asset, $need, $wwwDir, array $config) { |
|
154 | - $manifest = $config['manifest']; |
|
155 | - |
|
156 | - // Asset revisions specified directly in configuration |
|
157 | - if (is_array($manifest)) { |
|
158 | - return $manifest; |
|
159 | - } |
|
160 | - |
|
161 | - // Path to JSON manifest |
|
162 | - if (is_string($manifest)) { |
|
163 | - if ( ! file_exists($manifest)) { |
|
164 | - $msg = sprintf("Manifest file not found: '%s'.", $manifest); |
|
165 | - Utils::throwError(new ManifestNotFoundException($msg), $config['missingManifest'], $need); |
|
166 | - return NULL; |
|
167 | - } |
|
168 | - return Json::decode(file_get_contents($manifest), Json::FORCE_ARRAY); |
|
169 | - } |
|
170 | - |
|
171 | - // Autodetect manifest path |
|
172 | - return self::autodetectManifest($asset, $wwwDir, $need, $config); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @param string $asset Asset path specified in macro |
|
177 | - * @param string $wwwDir Public www dir |
|
178 | - * @param bool $need Fail if asset/manifest doesn't exist? |
|
179 | - * @param array $config Macro configuration |
|
180 | - * @return null|array |
|
181 | - */ |
|
182 | - private static function autodetectManifest($asset, $wwwDir, $need, array $config) { |
|
183 | - // Get asset dir, there begins search manifest |
|
184 | - $dir = realpath($wwwDir . DIRECTORY_SEPARATOR . dirname($asset)); |
|
185 | - if ($dir === FALSE) { |
|
186 | - $msg = sprintf("Parent dir of asset '%s' not found.", $asset); |
|
187 | - Utils::throwError(new DirNotFoundException($msg), $config['missingAsset'], $need); |
|
188 | - return NULL; |
|
189 | - } |
|
190 | - |
|
191 | - // Autodetect manifest |
|
192 | - $autodetectPaths = $config['autodetect']; |
|
193 | - while (Strings::startsWith($dir, $wwwDir)) { |
|
194 | - foreach ($autodetectPaths as $path) { |
|
195 | - $path = $dir . DIRECTORY_SEPARATOR . $path; |
|
196 | - if (file_exists($path)) { |
|
197 | - return Json::decode(file_get_contents($path), Json::FORCE_ARRAY); |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - $dir = dirname($dir); // go up |
|
202 | - } |
|
203 | - |
|
204 | - $msg = sprintf("Manifest not found in: %s.", implode(', ', $autodetectPaths)); |
|
205 | - Utils::throwError(new ManifestNotFoundException($msg), $config['missingManifest'], $need); |
|
206 | - return NULL; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * @param null|array $manifest Array of revisions |
|
211 | - * @param string $path Asset path |
|
212 | - * @param bool $need Fail if revision doesn't exist? |
|
213 | - * @param array $config Macro configuration |
|
214 | - * @return null|string |
|
215 | - */ |
|
216 | - private static function resolveRevision($manifest, $path, $need, array $config) { |
|
217 | - $revision = isset($manifest[$path]) ? $manifest[$path] : NULL; |
|
65 | + list($path, $format, $need) = self::processArguments($asset, $args); |
|
66 | + $wwwDir = Utils::normalizePath($config['wwwDir']); |
|
67 | + $manifest = self::resolveManifest($path, $need, $wwwDir, $config); |
|
68 | + $revision = $manifest === NULL ? NULL : self::resolveRevision($manifest, $path, $need, $config); |
|
69 | + |
|
70 | + // Is revision only version (query parameter) or full path to asset? |
|
71 | + $isVersion = $revision === NULL || strspn($revision,"./") === 0; |
|
72 | + |
|
73 | + // Check if asset exists |
|
74 | + if ($config['existCheck'] === TRUE) { |
|
75 | + $ds = DIRECTORY_SEPARATOR; |
|
76 | + $filePath = $isVersion ? ($wwwDir . $ds . $path) : ($wwwDir . $ds . Utils::normalizePath($revision)); |
|
77 | + if (! file_exists($filePath)) { |
|
78 | + $msg = sprintf("Asset '%s' not found.", $filePath); |
|
79 | + Utils::throwError(new AssetNotFoundException($msg), $config['missingAsset'], $need); |
|
80 | + return ''; |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + // Format output |
|
85 | + return self::formatOutput($format, $basePath, $path, $revision, $isVersion); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @param string $format Output format |
|
90 | + * @param string $basePath Base path |
|
91 | + * @param string $path Asset relative path |
|
92 | + * @param string|null $revision Asset revision (version or path to file) |
|
93 | + * @param bool $isVersion Is revision only version or full path? |
|
94 | + * @return string |
|
95 | + */ |
|
96 | + private static function formatOutput($format, $basePath, $path, $revision, $isVersion) |
|
97 | + { |
|
98 | + $revision = $revision ?: 'unknown'; |
|
99 | + $path = $isVersion ? |
|
100 | + sprintf("%s?v=%s", $path, $revision) : |
|
101 | + $revision; |
|
102 | + |
|
103 | + return Strings::replace($format, |
|
104 | + '/%([^%]+)%/', |
|
105 | + function ($matches) use ($basePath, $format, $path, $revision) { |
|
106 | + switch ($matches[1]) { |
|
107 | + case 'raw': |
|
108 | + return $revision; |
|
109 | + case 'basePath': |
|
110 | + return $basePath; |
|
111 | + case 'path': |
|
112 | + return $path; |
|
113 | + case 'url': |
|
114 | + return sprintf("%s/%s", $basePath, $path); |
|
115 | + default: |
|
116 | + $msg = sprintf( |
|
117 | + "Asset macro: Invalid variable '%s' in format '%s'. " . |
|
118 | + "Use one of allowed variables: %%raw%%, %%basePath%%, %%path%%, %%url%%.", |
|
119 | + $matches[1], |
|
120 | + $format |
|
121 | + ); |
|
122 | + throw new InvalidVariableException($msg); |
|
123 | + } |
|
124 | + }); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @param string $asset Asset path specified in macro |
|
129 | + * @param array $args Macro arguments |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + private static function processArguments($asset, array $args) |
|
133 | + { |
|
134 | + $format = isset($args['format']) ? $args['format'] : (isset($args[0]) ? $args[0] : '%url%'); |
|
135 | + $need = isset($args['need']) ? $args['need'] : (isset($args[1]) ? $args[1] : TRUE); |
|
136 | + |
|
137 | + Validators::assert($asset, 'string', 'path'); |
|
138 | + Validators::assert($format, 'string', 'format'); |
|
139 | + Validators::assert($need, 'bool', 'need'); |
|
140 | + |
|
141 | + $path = Utils::normalizePath($asset); |
|
142 | + |
|
143 | + return [$path, $format, $need]; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @param string $asset Asset path specified in macro |
|
148 | + * @param bool $need Fail if manifest doesn't exist? |
|
149 | + * @param string $wwwDir Public www dir |
|
150 | + * @param array $config Macro configuration |
|
151 | + * @return null|array |
|
152 | + */ |
|
153 | + private static function resolveManifest($asset, $need, $wwwDir, array $config) { |
|
154 | + $manifest = $config['manifest']; |
|
155 | + |
|
156 | + // Asset revisions specified directly in configuration |
|
157 | + if (is_array($manifest)) { |
|
158 | + return $manifest; |
|
159 | + } |
|
160 | + |
|
161 | + // Path to JSON manifest |
|
162 | + if (is_string($manifest)) { |
|
163 | + if ( ! file_exists($manifest)) { |
|
164 | + $msg = sprintf("Manifest file not found: '%s'.", $manifest); |
|
165 | + Utils::throwError(new ManifestNotFoundException($msg), $config['missingManifest'], $need); |
|
166 | + return NULL; |
|
167 | + } |
|
168 | + return Json::decode(file_get_contents($manifest), Json::FORCE_ARRAY); |
|
169 | + } |
|
170 | + |
|
171 | + // Autodetect manifest path |
|
172 | + return self::autodetectManifest($asset, $wwwDir, $need, $config); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @param string $asset Asset path specified in macro |
|
177 | + * @param string $wwwDir Public www dir |
|
178 | + * @param bool $need Fail if asset/manifest doesn't exist? |
|
179 | + * @param array $config Macro configuration |
|
180 | + * @return null|array |
|
181 | + */ |
|
182 | + private static function autodetectManifest($asset, $wwwDir, $need, array $config) { |
|
183 | + // Get asset dir, there begins search manifest |
|
184 | + $dir = realpath($wwwDir . DIRECTORY_SEPARATOR . dirname($asset)); |
|
185 | + if ($dir === FALSE) { |
|
186 | + $msg = sprintf("Parent dir of asset '%s' not found.", $asset); |
|
187 | + Utils::throwError(new DirNotFoundException($msg), $config['missingAsset'], $need); |
|
188 | + return NULL; |
|
189 | + } |
|
190 | + |
|
191 | + // Autodetect manifest |
|
192 | + $autodetectPaths = $config['autodetect']; |
|
193 | + while (Strings::startsWith($dir, $wwwDir)) { |
|
194 | + foreach ($autodetectPaths as $path) { |
|
195 | + $path = $dir . DIRECTORY_SEPARATOR . $path; |
|
196 | + if (file_exists($path)) { |
|
197 | + return Json::decode(file_get_contents($path), Json::FORCE_ARRAY); |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + $dir = dirname($dir); // go up |
|
202 | + } |
|
203 | + |
|
204 | + $msg = sprintf("Manifest not found in: %s.", implode(', ', $autodetectPaths)); |
|
205 | + Utils::throwError(new ManifestNotFoundException($msg), $config['missingManifest'], $need); |
|
206 | + return NULL; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * @param null|array $manifest Array of revisions |
|
211 | + * @param string $path Asset path |
|
212 | + * @param bool $need Fail if revision doesn't exist? |
|
213 | + * @param array $config Macro configuration |
|
214 | + * @return null|string |
|
215 | + */ |
|
216 | + private static function resolveRevision($manifest, $path, $need, array $config) { |
|
217 | + $revision = isset($manifest[$path]) ? $manifest[$path] : NULL; |
|
218 | 218 | |
219 | - if ($revision === NULL) { |
|
220 | - $msg = sprintf("Revision for asset '%s' not found in manifest.", $path); |
|
221 | - Utils::throwError(new AssetVersionNotFound($msg), $config['missingRevision'], $need); |
|
222 | - } |
|
219 | + if ($revision === NULL) { |
|
220 | + $msg = sprintf("Revision for asset '%s' not found in manifest.", $path); |
|
221 | + Utils::throwError(new AssetVersionNotFound($msg), $config['missingRevision'], $need); |
|
222 | + } |
|
223 | 223 | |
224 | - return $revision; |
|
225 | - } |
|
224 | + return $revision; |
|
225 | + } |
|
226 | 226 | |
227 | 227 | } |
@@ -7,45 +7,45 @@ |
||
7 | 7 | |
8 | 8 | class Utils |
9 | 9 | { |
10 | - public static function normalizePath($path, $separator = '\\/') |
|
11 | - { |
|
12 | - // Remove any kind of unicode whitespace |
|
13 | - $normalized = preg_replace('#\p{C}+|^\./#u', '', $path); |
|
14 | - |
|
15 | - // Path remove self referring paths ("/./"). |
|
16 | - $normalized = preg_replace('#/\.(?=/)|^\./|\./$#', '', $normalized); |
|
17 | - |
|
18 | - // Regex for resolving relative paths |
|
19 | - $regex = '#\/*[^/\.]+/\.\.#Uu'; |
|
20 | - |
|
21 | - while (preg_match($regex, $normalized)) { |
|
22 | - $normalized = preg_replace($regex, '', $normalized); |
|
23 | - } |
|
24 | - |
|
25 | - if (preg_match('#/\.{2}|\.{2}/#', $normalized)) { |
|
26 | - throw new InvalidPathException( |
|
27 | - sprintf("Path is outside of the defined root, path: '%s', resolved: '%s'.", $path, $normalized) |
|
28 | - ); |
|
29 | - } |
|
30 | - |
|
31 | - return rtrim($normalized, $separator); |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * @param \Exception $e |
|
36 | - * @param string $action |
|
37 | - * @param bool $need |
|
38 | - * @throws \Exception |
|
39 | - */ |
|
40 | - public static function throwError(\Exception $e, $action = 'exception', $need = TRUE) |
|
41 | - { |
|
42 | - if ($need) { |
|
43 | - if ($action === 'exception') { |
|
44 | - throw $e; |
|
45 | - |
|
46 | - } elseif ($action === 'notice') { |
|
47 | - trigger_error($e->getMessage(), E_USER_NOTICE); |
|
48 | - } |
|
49 | - } |
|
50 | - } |
|
10 | + public static function normalizePath($path, $separator = '\\/') |
|
11 | + { |
|
12 | + // Remove any kind of unicode whitespace |
|
13 | + $normalized = preg_replace('#\p{C}+|^\./#u', '', $path); |
|
14 | + |
|
15 | + // Path remove self referring paths ("/./"). |
|
16 | + $normalized = preg_replace('#/\.(?=/)|^\./|\./$#', '', $normalized); |
|
17 | + |
|
18 | + // Regex for resolving relative paths |
|
19 | + $regex = '#\/*[^/\.]+/\.\.#Uu'; |
|
20 | + |
|
21 | + while (preg_match($regex, $normalized)) { |
|
22 | + $normalized = preg_replace($regex, '', $normalized); |
|
23 | + } |
|
24 | + |
|
25 | + if (preg_match('#/\.{2}|\.{2}/#', $normalized)) { |
|
26 | + throw new InvalidPathException( |
|
27 | + sprintf("Path is outside of the defined root, path: '%s', resolved: '%s'.", $path, $normalized) |
|
28 | + ); |
|
29 | + } |
|
30 | + |
|
31 | + return rtrim($normalized, $separator); |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * @param \Exception $e |
|
36 | + * @param string $action |
|
37 | + * @param bool $need |
|
38 | + * @throws \Exception |
|
39 | + */ |
|
40 | + public static function throwError(\Exception $e, $action = 'exception', $need = TRUE) |
|
41 | + { |
|
42 | + if ($need) { |
|
43 | + if ($action === 'exception') { |
|
44 | + throw $e; |
|
45 | + |
|
46 | + } elseif ($action === 'notice') { |
|
47 | + trigger_error($e->getMessage(), E_USER_NOTICE); |
|
48 | + } |
|
49 | + } |
|
50 | + } |
|
51 | 51 | } |
52 | 52 | \ No newline at end of file |