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\jpf11009ded9fc4592b6a05b61ce272b3c_jetpackⓥ11_7_a_3; |
---|
9 | |
---|
10 | // phpcs:ignore |
---|
11 | |
---|
12 | /** |
---|
13 | * This class loads other classes based on given parameters. |
---|
14 | */ |
---|
15 | class Version_Loader { |
---|
16 | |
---|
17 | /** |
---|
18 | * The Version_Selector object. |
---|
19 | * |
---|
20 | * @var Version_Selector |
---|
21 | */ |
---|
22 | private $version_selector; |
---|
23 | |
---|
24 | /** |
---|
25 | * A map of available classes and their version and file path. |
---|
26 | * |
---|
27 | * @var array |
---|
28 | */ |
---|
29 | private $classmap; |
---|
30 | |
---|
31 | /** |
---|
32 | * A map of PSR-4 namespaces and their version and directory path. |
---|
33 | * |
---|
34 | * @var array |
---|
35 | */ |
---|
36 | private $psr4_map; |
---|
37 | |
---|
38 | /** |
---|
39 | * A map of all the files that we should load. |
---|
40 | * |
---|
41 | * @var array |
---|
42 | */ |
---|
43 | private $filemap; |
---|
44 | |
---|
45 | /** |
---|
46 | * The constructor. |
---|
47 | * |
---|
48 | * @param Version_Selector $version_selector The Version_Selector object. |
---|
49 | * @param array $classmap The verioned classmap to load using. |
---|
50 | * @param array $psr4_map The versioned PSR-4 map to load using. |
---|
51 | * @param array $filemap The versioned filemap to load. |
---|
52 | */ |
---|
53 | public function __construct( $version_selector, $classmap, $psr4_map, $filemap ) { |
---|
54 | $this->version_selector = $version_selector; |
---|
55 | $this->classmap = $classmap; |
---|
56 | $this->psr4_map = $psr4_map; |
---|
57 | $this->filemap = $filemap; |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Finds the file path for the given class. |
---|
62 | * |
---|
63 | * @param string $class_name The class to find. |
---|
64 | * |
---|
65 | * @return string|null $file_path The path to the file if found, null if no class was found. |
---|
66 | */ |
---|
67 | public function find_class_file( $class_name ) { |
---|
68 | $data = $this->select_newest_file( |
---|
69 | isset( $this->classmap[ $class_name ] ) ? $this->classmap[ $class_name ] : null, |
---|
70 | $this->find_psr4_file( $class_name ) |
---|
71 | ); |
---|
72 | if ( ! isset( $data ) ) { |
---|
73 | return null; |
---|
74 | } |
---|
75 | |
---|
76 | return $data['path']; |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Load all of the files in the filemap. |
---|
81 | */ |
---|
82 | public function load_filemap() { |
---|
83 | if ( empty( $this->filemap ) ) { |
---|
84 | return; |
---|
85 | } |
---|
86 | |
---|
87 | foreach ( $this->filemap as $file_identifier => $file_data ) { |
---|
88 | if ( empty( $GLOBALS['__composer_autoload_files'][ $file_identifier ] ) ) { |
---|
89 | require_once $file_data['path']; |
---|
90 | |
---|
91 | $GLOBALS['__composer_autoload_files'][ $file_identifier ] = true; |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Compares different class sources and returns the newest. |
---|
98 | * |
---|
99 | * @param array|null $classmap_data The classmap class data. |
---|
100 | * @param array|null $psr4_data The PSR-4 class data. |
---|
101 | * |
---|
102 | * @return array|null $data |
---|
103 | */ |
---|
104 | private function select_newest_file( $classmap_data, $psr4_data ) { |
---|
105 | if ( ! isset( $classmap_data ) ) { |
---|
106 | return $psr4_data; |
---|
107 | } elseif ( ! isset( $psr4_data ) ) { |
---|
108 | return $classmap_data; |
---|
109 | } |
---|
110 | |
---|
111 | if ( $this->version_selector->is_version_update_required( $classmap_data['version'], $psr4_data['version'] ) ) { |
---|
112 | return $psr4_data; |
---|
113 | } |
---|
114 | |
---|
115 | return $classmap_data; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Finds the file for a given class in a PSR-4 namespace. |
---|
120 | * |
---|
121 | * @param string $class_name The class to find. |
---|
122 | * |
---|
123 | * @return array|null $data The version and path path to the file if found, null otherwise. |
---|
124 | */ |
---|
125 | private function find_psr4_file( $class_name ) { |
---|
126 | if ( ! isset( $this->psr4_map ) ) { |
---|
127 | return null; |
---|
128 | } |
---|
129 | |
---|
130 | // Don't bother with classes that have no namespace. |
---|
131 | $class_index = strrpos( $class_name, '\\' ); |
---|
132 | if ( ! $class_index ) { |
---|
133 | return null; |
---|
134 | } |
---|
135 | $class_for_path = str_replace( '\\', '/', $class_name ); |
---|
136 | |
---|
137 | // Search for the namespace by iteratively cutting off the last segment until |
---|
138 | // we find a match. This allows us to check the most-specific namespaces |
---|
139 | // first as well as minimize the amount of time spent looking. |
---|
140 | for ( |
---|
141 | $class_namespace = substr( $class_name, 0, $class_index ); |
---|
142 | ! empty( $class_namespace ); |
---|
143 | $class_namespace = substr( $class_namespace, 0, strrpos( $class_namespace, '\\' ) ) |
---|
144 | ) { |
---|
145 | $namespace = $class_namespace . '\\'; |
---|
146 | if ( ! isset( $this->psr4_map[ $namespace ] ) ) { |
---|
147 | continue; |
---|
148 | } |
---|
149 | $data = $this->psr4_map[ $namespace ]; |
---|
150 | |
---|
151 | foreach ( $data['path'] as $path ) { |
---|
152 | $path .= '/' . substr( $class_for_path, strlen( $namespace ) ) . '.php'; |
---|
153 | if ( file_exists( $path ) ) { |
---|
154 | return array( |
---|
155 | 'version' => $data['version'], |
---|
156 | 'path' => $path, |
---|
157 | ); |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | return null; |
---|
163 | } |
---|
164 | } |
---|