Completed
Pull Request — master (#19)
by James
04:03
created

Config::get_config_json()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 12
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 configuration files.
58
	 *
59
	 * @var array
60
	 */
61
	private $loaded = array();
62
63
	/**
64
	 * Config constructor.
65
	 *
66
	 * @param string $type
67
	 * @param string $file
68
	 */
69 27
	public function __construct( $type, $file ) {
70 27
		$this->type = new ConfigType( $type );
71 27
		$this->file = $file;
72
73 27
		switch ( $this->type->getValue() ) {
74 27
			case ConfigType::PLUGIN:
75 27
			case ConfigType::MU_PLUGIN:
76 27
				$this->url = plugin_dir_url( $file );
77 27
				$this->path = plugin_dir_path( $file );
78 27
				$this->slug = dirname( $this->basename = plugin_basename( $file ) );
79 27
				break;
80
			case ConfigType::THEME:
81
				$this->url = get_stylesheet_directory_uri() . '/';
82
				$this->path = get_stylesheet_directory() . '/';
83
				$this->slug = dirname( $this->basename = plugin_basename( $file ) );
84
				break;
85 27
		}
86 27
	}
87
88
	/**
89
	 * Load a configuration JSON file from the config folder.
90
	 *
91
	 * @param string $filename
92
	 *
93
	 * @return array|null
94
	 */
95
	public function get_config_json( $filename ) {
96
		if ( isset( $this->loaded[ $filename ] ) ) {
97
			return $this->loaded[ $filename ];
98
		}
99
100
		$contents = file_get_contents( $this->path . 'config/' . $filename . '.json' );
101
102
		if ( false === $contents ) {
103
			return null;
104
		}
105
106
		return $this->loaded[ $filename ] = json_decode( $contents, true );
107
	}
108
}
109