Completed
Push — update/audio-player ( d9f9b9...6dc0dd )
by
unknown
173:42 queued 163:37
created

Classes_Handler::set_class_paths()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 0
dl 16
loc 16
rs 9.4222
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file was automatically generated by automattic/jetpack-autoloader.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
namespace Automattic\Jetpack\Autoloader\jp02f167abb8e9ed34472e5bd963af3d06;
9
10
 // phpcs:ignore
11
12
/**
13
 * This class selects the package versions for the package classes.
14
 */
15
class Classes_Handler {
16
17
	/**
18
	 * The Plugins_Handler object.
19
	 *
20
	 * @var Plugins_Handler
21
	 */
22
	private $plugins_handler = null;
23
24
	/**
25
	 * The Version_Selector object.
26
	 *
27
	 * @var Version_Selector
28
	 */
29
	private $version_selector = null;
30
31
	/**
32
	 * The constructor.
33
	 *
34
	 * @param Plugins_Handler  $plugins_handler The Plugins_Handler object.
35
	 * @param Version_Selector $version_selector The Version_Selector object.
36
	 */
37
	public function __construct( $plugins_handler, $version_selector ) {
38
		$this->plugins_handler  = $plugins_handler;
39
		$this->version_selector = $version_selector;
40
	}
41
42
	/**
43
	 * Adds the version of a package to the $jetpack_packages_classmap global
44
	 * array so that the autoloader is able to find it.
45
	 *
46
	 * @param string $class_name Name of the class that you want to autoload.
47
	 * @param string $version Version of the class.
48
	 * @param string $path Absolute path to the class so that we can load it.
49
	 */
50 View Code Duplication
	public function enqueue_package_class( $class_name, $version, $path ) {
51
		global $jetpack_packages_classmap;
52
53
		if ( isset( $jetpack_packages_classmap[ $class_name ]['version'] ) ) {
54
			$selected_version = $jetpack_packages_classmap[ $class_name ]['version'];
55
		} else {
56
			$selected_version = null;
57
		}
58
59
		if ( $this->version_selector->is_version_update_required( $selected_version, $version ) ) {
60
			$jetpack_packages_classmap[ $class_name ] = array(
61
				'version' => $version,
62
				'path'    => $path,
63
			);
64
		}
65
	}
66
67
	/**
68
	 * Creates the path to the plugin's classmap file. The classmap filename is the filename
69
	 * generated by Jetpack Autoloader version >= 2.0.
70
	 *
71
	 * @param String $plugin_path The plugin path.
72
	 *
73
	 * @return String the classmap path.
74
	 */
75
	public function create_classmap_path( $plugin_path ) {
76
		return trailingslashit( $plugin_path ) . 'vendor/composer/jetpack_autoload_classmap.php';
77
	}
78
79
	/**
80
	 *  Initializes the classmap.
81
	 */
82 View Code Duplication
	public function set_class_paths() {
83
		$active_plugins_paths = $this->plugins_handler->get_all_active_plugins_paths();
84
		$classmap_paths       = array_map( array( $this, 'create_classmap_path' ), $active_plugins_paths );
85
86
		foreach ( $classmap_paths as $path ) {
87
			if ( is_readable( $path ) ) {
88
				$class_map = require $path;
89
90
				if ( is_array( $class_map ) ) {
91
					foreach ( $class_map as $class_name => $class_info ) {
92
						$this->enqueue_package_class( $class_name, $class_info['version'], $class_info['path'] );
93
					}
94
				}
95
			}
96
		}
97
	}
98
}
99