1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2024 |
6
|
|
|
* @package Admin |
7
|
|
|
* @subpackage JQAdm |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Admin\JQAdm; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Generates concatenated CSS/JS files read from a JSB2 package files |
16
|
|
|
*/ |
17
|
|
|
class Bundle |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Returns the concatenated files for the requested resource |
21
|
|
|
* |
22
|
|
|
* @param string $resource Name of the resource, i.e. "index-js", "index-css", "index-ltr-css" or "index-rtl-css" |
23
|
|
|
* @return string Concatenated files for the requested resource |
24
|
|
|
*/ |
25
|
|
|
public static function get( array $filenames, string $resource ) : string |
26
|
|
|
{ |
27
|
|
|
$content = ''; |
28
|
|
|
$manifests = self::manifests( $filenames ); |
29
|
|
|
$packages = self::packages( $manifests, $resource ); |
30
|
|
|
|
31
|
|
|
foreach( $packages as $basepath => $package ) |
32
|
|
|
{ |
33
|
|
|
foreach( $package->fileIncludes as $singleFile ) |
34
|
|
|
{ |
35
|
|
|
$file = $basepath . '/' . $singleFile->path . $singleFile->text; |
36
|
|
|
|
37
|
|
|
if( $data = file_get_contents( $file ) ) { |
38
|
|
|
$content .= $data; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $content; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the packages from a JSON decoded manifests |
49
|
|
|
* |
50
|
|
|
* @param array $manifests List of JSON decoded manifests |
51
|
|
|
* @param string $resource Name of the resource, i.e. "index-js", "index-css", "index-ltr-css" or "index-rtl-css" |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
protected static function packages( array $manifests, string $resource ) : array |
55
|
|
|
{ |
56
|
|
|
$list = []; |
57
|
|
|
|
58
|
|
|
foreach( $manifests as $basepath => $manifest ) |
59
|
|
|
{ |
60
|
|
|
if( empty( $manifest->pkgs ?? [] ) ) { |
61
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( 'No packages found' ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach( $manifest->pkgs as $package ) |
65
|
|
|
{ |
66
|
|
|
if( !isset( $package->file ) || !is_object( $package ) ) { |
67
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( 'Invalid package content' ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if( $package->file !== $resource ) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if( $package->overwrite ?? false ) { |
75
|
|
|
$list = [$basepath => $package]; |
76
|
|
|
} else { |
77
|
|
|
$list[$basepath] = $package; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $list; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the manifest file contents |
88
|
|
|
* |
89
|
|
|
* @param array $filepaths List of paths to the manifest files |
90
|
|
|
* @return array Manifest file contents |
91
|
|
|
*/ |
92
|
|
|
protected static function manifests( array $filepaths ) |
93
|
|
|
{ |
94
|
|
|
$list = []; |
95
|
|
|
|
96
|
|
|
foreach( $filepaths as $filepath ) |
97
|
|
|
{ |
98
|
|
|
if( !file_exists( $filepath ) ) { |
99
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'File does not exists: "%1$s"', $filepath ) ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if( ( $content = file_get_contents( $filepath ) ) === false ) { |
103
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Unable to read content from "%1$s"', $filepath ) ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if( ( $content = json_decode( $content ) ) === null ) { |
107
|
|
|
throw new \Aimeos\Admin\JQAdm\Exception( 'File content is not JSON encoded' ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$list[dirname( $filepath )] = $content; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $list; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|