1
|
|
|
<?php |
2
|
|
|
namespace Intraxia\Jaxion\Core; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class Config |
6
|
|
|
* |
7
|
|
|
* Configuration service to manage the |
8
|
|
|
* configuration data of the plugin or theme. |
9
|
|
|
* |
10
|
|
|
* @package Intraxia\Jaxion |
11
|
|
|
* @subpackage Core |
12
|
|
|
*/ |
13
|
|
|
class Config { |
14
|
|
|
/** |
15
|
|
|
* Configuration type. |
16
|
|
|
* |
17
|
|
|
* @var ConfigType |
18
|
|
|
*/ |
19
|
|
|
public $type; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* App entry file. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public $file; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* App url. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $url; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* App path. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $path; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* App slug. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
public $slug; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* App basename. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
public $basename; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Loaded JSON configuration files. |
58
|
|
|
* |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
private $json = array(); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Loaded PHP configuration files. |
65
|
|
|
* |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
private $php = array(); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Config constructor. |
72
|
|
|
* |
73
|
|
|
* @param string $type |
74
|
|
|
* @param string $file |
75
|
|
|
*/ |
76
|
27 |
|
public function __construct( $type, $file ) { |
77
|
27 |
|
$this->type = new ConfigType( $type ); |
78
|
27 |
|
$this->file = $file; |
79
|
|
|
|
80
|
27 |
|
switch ( $this->type->getValue() ) { |
81
|
27 |
|
case ConfigType::PLUGIN: |
82
|
18 |
|
case ConfigType::MU_PLUGIN: |
83
|
27 |
|
$this->url = plugin_dir_url( $file ); |
84
|
27 |
|
$this->path = plugin_dir_path( $file ); |
85
|
27 |
|
$this->slug = dirname( $this->basename = plugin_basename( $file ) ); |
86
|
27 |
|
break; |
87
|
|
|
case ConfigType::THEME: |
88
|
|
|
$this->url = get_stylesheet_directory_uri() . '/'; |
89
|
|
|
$this->path = get_stylesheet_directory() . '/'; |
90
|
|
|
$this->slug = dirname( $this->basename = plugin_basename( $file ) ); |
91
|
|
|
break; |
92
|
18 |
|
} |
93
|
27 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Load a configuration JSON file from the config folder. |
97
|
|
|
* |
98
|
|
|
* @param string $filename |
99
|
|
|
* |
100
|
|
|
* @return array|null |
101
|
|
|
*/ |
102
|
|
|
public function get_config_json( $filename ) { |
103
|
|
|
if ( isset( $this->json[ $filename ] ) ) { |
104
|
|
|
return $this->json[ $filename ]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$path = $this->path . 'config/' . $filename . '.json'; |
108
|
|
|
|
109
|
|
|
if ( ! file_exists( $path ) ) { |
110
|
|
|
return null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$contents = file_get_contents( $path ); |
114
|
|
|
|
115
|
|
|
if ( false === $contents ) { |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->json[ $filename ] = json_decode( $contents, true ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Load a configuration PHP file from the config folder. |
124
|
|
|
* |
125
|
|
|
* @param string $filename |
126
|
|
|
* |
127
|
|
|
* @return array|null |
128
|
|
|
*/ |
129
|
|
|
public function get_config_php( $filename ) { |
130
|
|
|
if ( isset( $this->php[ $filename ] ) ) { |
131
|
|
|
return $this->php[ $filename ]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$path = $this->path . 'config/' . $filename . '.php'; |
135
|
|
|
|
136
|
|
|
if ( ! file_exists( $path ) ) { |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->php[ $filename ] = require $path; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|