-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBuild_Parser.php
More file actions
96 lines (76 loc) · 2.42 KB
/
Build_Parser.php
File metadata and controls
96 lines (76 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php namespace WP_CLI_Build;
use Symfony\Component\Yaml\Yaml;
use WP_CLI;
use WP_CLI_Build\Helper\Utils;
class Build_Parser {
private mixed $filename = 'build.json';
private string $format = 'json';
private array $build = [];
public function __construct( $filename, $assoc_args = null ) {
// Set Build file.
$this->filename = empty( $filename ) ? 'build.json' : $filename;
// Set format.
$this->format = (str_contains($this->filename, 'yml')) ? 'yml' : 'json';
// Parse the Build file and Build sure it's valid.
$this->parse();
}
private function parse(): void
{
// Full Build file path.
$file_path = ( Utils::is_absolute_path( $this->filename ) ) ? $this->filename : realpath( '.' ) . '/' . $this->filename;
// Set specified path with --path argument.
if ( ! empty( WP_CLI::get_runner()->config['path'] ) ) {
$file_path = WP_CLI::get_runner()->config['path'] . '/' . $this->filename;
}
// Check if the file exists.
if ( ! file_exists( $file_path ) ) {
return;
}
// Check if the Build file is a valid yaml file.
if ( $this->format == 'yml' ) {
try {
$this->build = Yaml::parse( file_get_contents( $file_path ) );
} catch ( \Exception $e ) {
WP_CLI::error( 'Error parsing YAML from Build file (' . $this->filename . ').' );
return;
}
return;
}
// Build.json
try {
$this->build = json_decode( file_get_contents( $file_path ), TRUE );
} catch ( \Exception $e ) {
WP_CLI::error( 'Error parsing JSON from Build file (' . $this->filename . ').' );
return;
}
}
public function get( $key = null, $sub_key = null ) {
// With subkey.
if ( ! empty( $this->build[ $key ][ $sub_key ] ) ) {
return $this->build[ $key ][ $sub_key ];
}
// With key.
if ( ( ! empty( $this->build[ $key ] ) ) && ( empty( $sub_key ) ) ) {
return $this->build[ $key ];
}
return [];
}
public function get_core_version() {
if ( ! empty( $this->build['core']['download']['version'] ) ) {
return $this->build['core']['download']['version'];
}
return null;
}
public function get_plugin_version( $slug ) {
if ( ! empty( $this->build['plugins'][ $slug ]['version'] ) ) {
return $this->build['plugins'][ $slug ]['version'];
}
return null;
}
public function get_theme_version( $slug ) {
if ( ! empty( $this->build['themes'][ $slug ]['version'] ) ) {
return $this->build['themes'][ $slug ]['version'];
}
return null;
}
}