Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.00% covered (success)
96.00%
24 / 25
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Manifest_Reader
96.00% covered (success)
96.00%
24 / 25
75.00% covered (warning)
75.00%
3 / 4
10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 read_manifests
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 register_manifest
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 register_record
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/* HEADER */ // phpcs:ignore
3
4/**
5 * This class reads autoloader manifest files.
6 */
7class 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}