technote-space / gutenberg-package-versions
1
<?php
2
/**
3
 * @author Technote
4
 * @copyright Technote All Rights Reserved
5
 * @license http://www.opensource.org/licenses/gpl-2.0.php GNU General Public License, version 2
6
 * @link https://technote.space
7
 */
8

9
namespace Technote;
10

11
use WP_Filesystem_Direct;
12

13
// @codeCoverageIgnoreStart
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
// @codeCoverageIgnoreEnd
18

19
/**
20
 * Class GutenbergPackageVersionProvider
21
 * @package Technote
22
 */
23
class GutenbergPackageVersionProvider {
24

25
	/** @var array $cache */
26
	protected static $cache = [];
27

28
	/** @var WP_Filesystem_Direct $fs_cache */
29
	protected static $fs_cache;
30

31
	/** @var string $path */
32
	protected static $path;
33

34
	/** @var string $target */
35
	protected $target;
36

37
	/**
38
	 * GutenbergPackageVersionProvider constructor.
39
	 *
40
	 * @param string $target
41
	 */
42 1
	public function __construct( $target = 'gutenberg' ) {
43 1
		$target = strtolower( $target );
44 1
		if ( 'gutenberg' !== $target ) {
45 1
			$target = 'wp-core';
46
		}
47 1
		$this->target = $target;
48
	}
49

50
	/**
51
	 * @param array $segments
52
	 *
53
	 * @return string
54
	 */
55 1
	public function get_data_path( ...$segments ) {
56 1
		if ( ! isset( static::$path ) ) {
57 1
			static::$path = dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR;
58
		}
59

60 1
		return static::$path . $this->target . DIRECTORY_SEPARATOR . implode( DIRECTORY_SEPARATOR, $segments );
61
	}
62

63
	/**
64
	 * @param string $key
65
	 *
66
	 * @return array
67
	 * @SuppressWarnings(PHPMD.UndefinedVariable)
68
	 */
69 1
	private function get_cache( $key ) {
70 1
		if ( isset( static::$cache[ $this->target ][ $key ] ) ) {
71 1
			return [ true, static::$cache[ $this->target ][ $key ] ];
72
		}
73

74 1
		return [ false, null ];
75
	}
76

77
	/**
78
	 * @param string $key
79
	 * @param mixed $value
80
	 * @SuppressWarnings(PHPMD.UndefinedVariable)
81
	 */
82 1
	private function set_cache( $key, $value ) {
83 1
		static::$cache[ $this->target ][ $key ] = $value;
84
	}
85

86
	/**
87
	 * @param string $tag
88
	 *
89
	 * @return string
90
	 */
91 1
	public function normalize_tag( $tag ) {
92 1
		$tag = preg_replace( '#\Av\.?#', '', $tag );
93 1
		while ( substr_count( $tag, '.' ) < 2 ) {
94 1
			$tag .= '.0';
95
		}
96

97 1
		return $tag;
98
	}
99

100
	/**
101
	 * @return array
102
	 */
103 1
	public function get_tags() {
104 1
		list( $result, $tags ) = $this->get_cache( 'tags' );
105 1
		if ( ! $result ) {
106 1
			$tags = json_decode( $this->get_fs()->get_contents( $this->get_data_path( 'tags.json' ) ), true );
107 1
			$this->set_cache( 'tags', $tags );
108
		}
109

110 1
		return $tags;
111
	}
112

113
	/**
114
	 * @param string|null $tag
115
	 *
116
	 * @return array|null
117
	 */
118 1
	public function get_versions( $tag = null ) {
119 1
		list( $result, $versions ) = $this->get_cache( 'versions' );
120 1
		if ( ! $result ) {
121 1
			$versions = json_decode( $this->get_fs()->get_contents( $this->get_data_path( 'versions.json' ) ), true );
122 1
			$this->set_cache( 'versions', $versions );
123
		}
124

125 1
		if ( ! isset( $tag ) ) {
126 1
			return $versions;
127
		}
128

129 1
		$tag = $this->normalize_tag( $tag );
130 1
		if ( isset( $versions[ $tag ] ) ) {
131 1
			return $versions[ $tag ];
132
		}
133

134 1
		return null;
135
	}
136

137
	/**
138
	 * @param string $tag
139
	 * @param string $package
140
	 *
141
	 * @return string|false
142
	 */
143 1
	public function get_package_version( $tag, $package ) {
144 1
		$versions = $this->get_versions( $tag );
145

146 1
		if ( isset( $versions[ $package ] ) ) {
147 1
			return $versions[ $package ];
148
		}
149

150 1
		return false;
151
	}
152

153
	/**
154
	 * @param string $tag
155
	 * @param string $package
156
	 *
157
	 * @return bool
158
	 */
159 1
	public function package_exists( $tag, $package ) {
160 1
		$versions = $this->get_versions( $tag );
161

162 1
		return isset( $versions[ $package ] );
163
	}
164

165
	/**
166
	 * @return WP_Filesystem_Direct
167
	 */
168 1
	protected function get_fs() {
169 1
		if ( ! static::$fs_cache ) {
170
			// @codeCoverageIgnoreStart
171
			if ( ! class_exists( '\WP_Filesystem_Base' ) ) {
172
				require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
173
			}
174
			if ( ! class_exists( '\WP_Filesystem_Direct' ) ) {
175
				require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
176
			}
177

178
			// @see ABSPATH . 'wp-admin/includes/file.php' WP_Filesystem
179
			if ( ! defined( 'FS_CHMOD_DIR' ) ) {
180
				define( 'FS_CHMOD_DIR', file_exists( ABSPATH ) ? ( fileperms( ABSPATH ) & 0777 | 0755 ) : 0755 );
181
			}
182
			if ( ! defined( 'FS_CHMOD_FILE' ) ) {
183
				define( 'FS_CHMOD_FILE', file_exists( ABSPATH . 'index.php' ) ? ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) : 0644 );
184
			}
185
			// @codeCoverageIgnoreEnd
186

187 1
			static::$fs_cache = new WP_Filesystem_Direct( false );
188
		}
189

190 1
		return static::$fs_cache;
191
	}
192
}

Read our documentation on viewing source code .

Loading