Completed
Push — try/admin-color-schemes ( 81f984...4f8120 )
by
unknown
25:37 queued 13:42
created

Manifest_Reader::read_manifests()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/* HEADER */ // phpcs:ignore
3
4
/**
5
 * This class reads autoloader manifest files.
6
 */
7
class Manifest_Reader {
8
9
	/**
10
	 * The Version_Selector object.
11
	 *
12
	 * @var Version_Selector
13
	 */
14
	private $version_selector;
15
16
	/**
17
	 * The constructor.
18
	 *
19
	 * @param Version_Selector $version_selector The Version_Selector object.
20
	 */
21
	public function __construct( $version_selector ) {
22
		$this->version_selector = $version_selector;
23
	}
24
25
	/**
26
	 * Reads all of the manifests in the given plugin paths.
27
	 *
28
	 * @param array  $plugin_paths  The paths to the plugins we're loading the manifest in.
29
	 * @param string $manifest_path The path that we're loading the manifest from in each plugin.
30
	 * @param array  $path_map The path map to add the contents of the manifests to.
31
	 *
32
	 * @return array $path_map The path map we've built using the manifests in each plugin.
33
	 */
34
	public function read_manifests( $plugin_paths, $manifest_path, &$path_map ) {
35
		$file_paths = array_map(
36
			function ( $path ) use ( $manifest_path ) {
37
				return trailingslashit( $path ) . $manifest_path;
38
			},
39
			$plugin_paths
40
		);
41
42
		foreach ( $file_paths as $path ) {
43
			$this->register_manifest( $path, $path_map );
44
		}
45
46
		return $path_map;
47
	}
48
49
	/**
50
	 * Registers a plugin's manifest file with the path map.
51
	 *
52
	 * @param string $manifest_path The absolute path to the manifest that we're loading.
53
	 * @param array  $path_map The path map to add the contents of the manifest to.
54
	 */
55
	protected function register_manifest( $manifest_path, &$path_map ) {
56
		if ( ! is_readable( $manifest_path ) ) {
57
			return;
58
		}
59
60
		$manifest = require $manifest_path;
61
		if ( ! is_array( $manifest ) ) {
62
			return;
63
		}
64
65
		foreach ( $manifest as $key => $data ) {
66
			$this->register_record( $key, $data, $path_map );
67
		}
68
	}
69
70
	/**
71
	 * Registers an entry from the manifest in the path map.
72
	 *
73
	 * @param string $key The identifier for the entry we're registering.
74
	 * @param array  $data The data for the entry we're registering.
75
	 * @param array  $path_map The path map to add the contents of the manifest to.
76
	 */
77
	protected function register_record( $key, $data, &$path_map ) {
78
		if ( isset( $path_map[ $key ]['version'] ) ) {
79
			$selected_version = $path_map[ $key ]['version'];
80
		} else {
81
			$selected_version = null;
82
		}
83
84
		if ( $this->version_selector->is_version_update_required( $selected_version, $data['version'] ) ) {
85
			$path_map[ $key ] = array(
86
				'version' => $data['version'],
87
				'path'    => $data['path'],
88
			);
89
		}
90
	}
91
}
92