Completed
Push — master ( 78616b...e2534e )
by Nazar
04:37
created

Core   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 2 Features 1
Metric Value
wmc 16
c 5
b 2
f 1
lcom 1
cbo 2
dl 0
loc 137
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
C construct() 0 58 8
A load_config() 0 15 2
A get() 0 3 2
A set() 0 5 2
A __get() 0 3 1
A __set() 0 3 1
1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
use
10
	h;
11
12
/**
13
 * Core class.
14
 * Provides loading of base system configuration
15
 */
16
class Core {
17
	use Singleton;
18
	/**
19
	 * Is object constructed
20
	 * @var bool
21
	 */
22
	protected $constructed = false;
23
	/**
24
	 * @var mixed[]
25
	 */
26
	protected $config = [];
27
	/**
28
	 * Loading of base system configuration, creating of missing directories
29
	 *
30
	 * @throws ExitException
31
	 */
32
	protected function construct () {
33
		$this->config = $this->load_config();
34
		_include_once(DIR.'/config/main.php', false);
35
		defined('DEBUG') || define('DEBUG', false);
36
		defined('DOMAIN') || define('DOMAIN', $this->config['domain']);
37
		date_default_timezone_set($this->config['timezone']);
38
		if (!is_dir(PUBLIC_STORAGE)) {
39
			/** @noinspection MkdirRaceConditionInspection */
40
			@mkdir(PUBLIC_STORAGE, 0775, true);
41
			file_put_contents(
42
				PUBLIC_STORAGE.'/.htaccess',
43
				'Allow From All
44
<ifModule mod_headers.c>
45
	Header always append X-Frame-Options DENY
46
	Header set Content-Type application/octet-stream
47
</ifModule>
48
'
49
			);
50
		}
51
		if (!is_dir(CACHE)) {
52
			/** @noinspection MkdirRaceConditionInspection */
53
			@mkdir(CACHE, 0770);
54
		}
55
		if (!is_dir(PUBLIC_CACHE)) {
56
			/** @noinspection MkdirRaceConditionInspection */
57
			@mkdir(PUBLIC_CACHE, 0770);
58
			file_put_contents(
59
				PUBLIC_CACHE.'/.htaccess',
60
				'<FilesMatch "\.(css|js|html)$">
61
	Allow From All
62
</FilesMatch>
63
<ifModule mod_expires.c>
64
	ExpiresActive On
65
	ExpiresDefault "access plus 1 month"
66
</ifModule>
67
<ifModule mod_headers.c>
68
	Header set Cache-Control "max-age=2592000, public"
69
</ifModule>
70
AddEncoding gzip .js
71
AddEncoding gzip .css
72
AddEncoding gzip .html
73
'
74
			);
75
		}
76
		if (!is_dir(LOGS)) {
77
			/** @noinspection MkdirRaceConditionInspection */
78
			@mkdir(LOGS, 0770);
79
		}
80
		if (!is_dir(TEMP)) {
81
			/** @noinspection MkdirRaceConditionInspection */
82
			@mkdir(TEMP, 0775);
83
			file_put_contents(
84
				TEMP.'/.htaccess',
85
				"Allow From All\n"
86
			);
87
		}
88
		$this->constructed = true;
89
	}
90
	/**
91
	 * Load main.json config file and return array of it contents
92
	 *
93
	 * @return array
94
	 *
95
	 * @throws ExitException
96
	 */
97
	protected function load_config () {
98
		if (!file_exists(DIR.'/config/main.json')) {
99
			throw new ExitException(
100
				h::p('Config file not found, is system installed properly?').
101
				h::a(
102
					'How to install CleverStyle CMS',
103
					[
104
						'href' => 'https://github.com/nazar-pc/CleverStyle-CMS/wiki/Installation'
105
					]
106
				),
107
				500
108
			);
109
		}
110
		return file_get_json_nocomments(DIR.'/config/main.json');
111
	}
112
	/**
113
	 * Getting of base configuration parameter
114
	 *
115
	 * @param string $item
116
	 *
117
	 * @return false|string
118
	 */
119
	function get ($item) {
120
		return isset($this->config[$item]) ? $this->config[$item] : false;
121
	}
122
	/**
123
	 * Setting of base configuration parameter (available only at object construction)
124
	 *
125
	 * @param string $item
126
	 * @param mixed  $value
127
	 */
128
	function set ($item, $value) {
129
		if (!$this->constructed) {
130
			$this->config[$item] = $value;
131
		}
132
	}
133
	/**
134
	 * Getting of base configuration parameter
135
	 *
136
	 * @param string $item
137
	 *
138
	 * @return false|string
139
	 */
140
	function __get ($item) {
141
		return $this->get($item);
142
	}
143
	/**
144
	 * Setting of base configuration parameter (available only at object construction)
145
	 *
146
	 * @param string $item
147
	 * @param mixed  $value
148
	 */
149
	function __set ($item, $value) {
150
		$this->set($item, $value);
151
	}
152
}
153