Passed
Push — master ( a42a93...58cc2a )
by Aimeos
05:58
created

Standard::getUrls()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2024
7
 * @package MW
8
 * @subpackage Jsb2
9
 */
10
11
12
namespace Aimeos\MW\Jsb2;
13
14
15
/**
16
 *
17
 * Generates compressed JS files read from a .jsb2 package.
18
 *
19
 * @package MW
20
 * @subpackage Jsb2
21
 */
22
class Standard
23
{
24
	private array $registeredPackages;
25
	private string $basePath;
26
	private string $baseURL;
27
28
29
	/**
30
	 * Initializes the Jsb2 object.
31
	 *
32
	 * @param string $filename Path to manifest file
33
	 * @param string $baseURL Base URL for HTML output
34
	 * @param string[] $filter Which packages should NOT be returned
35
	 */
36
	public function __construct( string $filename, string $baseURL = '', array $filter = [] )
37
	{
38
		$manifest = $this->getManifest( $filename );
39
40
		$this->baseURL = rtrim( $baseURL, '/' ) . '/';
41
		$this->basePath = dirname( $filename ) . '/';
42
43
		$this->registeredPackages = $this->getPackages( $manifest, $filter );
44
	}
45
46
47
	/**
48
	 * Returns the list of URLs for packages files with given filter.
49
	 *
50
	 * @param string $name File name, e.g. "index.js", "index.css", "ltr.index.css" or "rtl.index.css"
51
	 * @return string[] List of URLs for the package files
52
	 */
53
	public function getFiles( string $name ) : array
54
	{
55
		$files = [];
56
57
		foreach( $this->registeredPackages[$name] ?? [] as $package )
58
		{
59
			foreach( $package->fileIncludes as $singleFile ) {
60
				$files[] = $this->basePath . $singleFile->path . $singleFile->text;
61
			}
62
		}
63
64
		return $files;
65
	}
66
67
68
	/**
69
	 * Get the packages from a JSON decoded manifest and validates them.
70
	 *
71
	 * @param object JSON decoded manifest
0 ignored issues
show
Bug introduced by
The type Aimeos\MW\Jsb2\JSON was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
72
	 * @param string[] $filter What packages should NOT be returned
73
	 */
74
	protected function getPackages( $manifest, array $filter = [] ) : array
75
	{
76
		$packageContainer = [];
77
78
		if( !isset( $manifest->pkgs ) || !is_array( $manifest->pkgs ) ) {
79
			throw new \Aimeos\MW\Jsb2\Exception( 'No packages found' );
80
		}
81
82
		foreach( $manifest->pkgs as $package )
83
		{
84
			if( !isset( $package->name ) || !isset( $package->file ) || !is_object( $package ) ) {
85
				throw new \Aimeos\MW\Jsb2\Exception( 'Invalid package content' );
86
			}
87
88
			if( !isset( $package->fileIncludes ) || !is_array( $package->fileIncludes ) ) {
89
				throw new \Aimeos\MW\Jsb2\Exception( 'No files in package found' );
90
			}
91
92
			if( !in_array( $package->name, $filter ) ) {
93
				$packageContainer[$package->file][] = $package;
94
			}
95
		}
96
97
		return $packageContainer;
98
	}
99
100
101
	/**
102
	 * Returns the content of a manifest file.
103
	 *
104
	 * @param string $filepath Path to manifest
105
	 * @return object Manifest file content
106
	 * @throws \Aimeos\MW\Jsb2\Exception
107
	 */
108
	protected function getManifest( string $filepath )
109
	{
110
		if( !file_exists( $filepath ) ) {
111
			throw new \Aimeos\MW\Jsb2\Exception( sprintf( 'File does not exists: "%1$s"', $filepath ) );
112
		}
113
114
		if( ( $content = file_get_contents( $filepath ) ) === false ) {
115
			throw new \Aimeos\MW\Jsb2\Exception( sprintf( 'Unable to read content from "%1$s"', $filepath ) );
116
		}
117
118
		if( ( $content = json_decode( $content ) ) === null ) {
119
			throw new \Aimeos\MW\Jsb2\Exception( 'File content is not JSON encoded' );
120
		}
121
122
		return $content;
123
	}
124
}
125