Completed
Pull Request — master (#19)
by James
08:05
created

Config::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 2
1
<?php
2
namespace Intraxia\Jaxion\Core;
3
4
class Config {
5
	/**
6
	 * Configuration type.
7
	 *
8
	 * @var ConfigType
9
	 */
10
	public $type;
11
12
	/**
13
	 * App entry file.
14
	 *
15
	 * @var string
16
	 */
17
	public $file;
18
19
	/**
20
	 * App url.
21
	 *
22
	 * @var string
23
	 */
24
	public $url;
25
26
	/**
27
	 * App path.
28
	 *
29
	 * @var string
30
	 */
31
	public $path;
32
33
	/**
34
	 * App slug.
35
	 *
36
	 * @var string
37
	 */
38
	public $slug;
39
40
	/**
41
	 * App basename.
42
	 *
43
	 * @var string
44
	 */
45
	public $basename;
46
47
	/**
48
	 * Loaded configuration files.
49
	 *
50
	 * @var array
51
	 */
52
	private $loaded = array();
53
54
	/**
55
	 * Config constructor.
56
	 *
57
	 * @param string $type
58
	 * @param string $file
59
	 */
60
	public function __construct( $type, $file ) {
61
		$this->type = new ConfigType( $type );
62
		$this->file = $file;
63
64
		switch ( $this->type->getValue() ) {
65
			case ConfigType::PLUGIN:
66
			case ConfigType::MU_PLUGIN:
67
				$this->url = plugin_dir_url( $file );
68
				$this->path = plugin_dir_path( $file );
69
				$this->slug = dirname( $this->basename = plugin_basename( $file ) );
70
				break;
71
			case ConfigType::THEME:
72
				$this->url = get_stylesheet_directory_uri() . '/';
73
				$this->path = get_stylesheet_directory() . '/';
74
				$this->slug = dirname( $this->basename = plugin_basename( $file ) );
75
				break;
76
		}
77
	}
78
79
	/**
80
	 * Load a configuration JSON file from the config folder.
81
	 *
82
	 * @param string $filename
83
	 *
84
	 * @return array|null
85
	 */
86
	public function get_config_json( $filename ) {
87
		if ( isset( $this->loaded[ $filename ] ) ) {
88
			return $this->loaded[ $filename ];
89
		}
90
91
		$contents = file_get_contents( $this->path . 'config/' . $filename . '.json' );
1 ignored issue
show
introduced by
file_get_contents is highly discouraged, please use wpcom_vip_file_get_contents() instead.
Loading history...
92
93
		if ( $contents === false ) {
0 ignored issues
show
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
94
			return null;
95
		}
96
97
		return $this->loaded[ $filename ] = json_decode( $contents, true );
98
	}
99
}
100