Completed
Branch 2.0.0 (814c19)
by Jimmy
03:05
created

Config_Util   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2
1
<?php
2
/**
3
 * Gestion de l'objet Config_Util::$init.
4
 *
5
 * @author Eoxia <[email protected]>
6
 * @since 0.1.0
7
 * @version 1.0.0
8
 * @copyright 2015-2018 Eoxia
9
 * @package EO_Framework\Core\Util
10
 */
11
12
namespace eoxia;
13
14
if ( ! defined( 'ABSPATH' ) ) {
15
	exit;
16
}
17
18
if ( ! class_exists( '\eoxia\Config_Util' ) ) {
19
20
	/**
21
	 * Gestion de l'objet Config_Util::$init.
22
	 */
23
	class Config_Util extends \eoxia\Singleton_Util {
24
25
		/**
26
		 * Un tableau contenant toutes les configurations des fichies config.json
27
		 *
28
		 * @var array
29
		 */
30
		public static $init = array();
31
32
		/**
33
		 * Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util
34
		 *
35
		 * @return void nothing
36
		 */
37
		protected function construct() {}
38
39
		/**
40
		 * Initialise les fichiers de configuration
41
		 *
42
		 * @since 0.1.0
43
		 * @version 1.0.1
44
		 *
45
		 * @param string $path_to_config_file Le chemin vers le fichier config.json.
46
		 * @param string $plugin_slug         Le SLUG du plugin définis dans le fichier principale de config.json.
47
		 *
48
		 * @return \WP_Error|boolean {
49
		 *																		WP_Error Si le fichier est inexistant ou si le plugin ne contient pas de slug.
50
		 *                                    boolean  True si aucune erreur s'est produite.
51
		 *}.
52
		 */
53
		public function init_config( $path_to_config_file, $plugin_slug = '' ) {
54
			if ( empty( $path_to_config_file ) ) {
55
				return new \WP_Error( 'broke', __( 'Unable to load file', 'eoxia' ) );
56
			}
57
58
			if ( ! file_exists( $path_to_config_file ) ) {
59
				return new \WP_Error( 'broke', __( sprintf( 'File %s is not found', $path_to_config_file ) ) );
60
			}
61
62
			$tmp_config = JSON_Util::g()->open_and_decode( $path_to_config_file );
63
			if ( empty( $tmp_config->slug ) ) {
64
				return new \WP_Error( 'broke', __( 'This plugin need to have a slug', 'eoxia' ) );
65
			}
66
67
			$type = isset( $tmp_config->modules ) ? 'main' : 'module';
68
69
			if ( 'main' === $type ) {
70
				if ( $tmp_config->slug !== $plugin_slug && '' !== $plugin_slug ) {
71
					return new \WP_Error( 'broke', __( sprintf( 'Slug of plugin is not equal main config json file name %s => %s. Set correct slug in the file: %s', $plugin_slug, $tmp_config->slug, $path_to_config_file ), 'eoxia' ) );
72
				}
73
			}
74
75
			if ( ! empty( $plugin_slug ) ) {
76
				if ( ! isset( self::$init[ $plugin_slug ] ) ) {
77
					self::$init[ $plugin_slug ] = $tmp_config;
78
				} else {
79
					$abspath = str_replace( '\\', '/', ABSPATH );
80
81
					$slug = $tmp_config->slug;
82
					$tmp_path = str_replace( '\\', '/', self::$init[ $plugin_slug ]->path );
83
					$tmp_config->module_path = $tmp_config->path;
84
85
					$tmp_config->url = str_replace( $abspath, site_url('/'), $tmp_path . $tmp_config->path );
86
					$tmp_config->url = str_replace( '\\', '/', $tmp_config->url );
87
					$tmp_config->path = $tmp_path . $tmp_config->path;
88
					if ( isset( $tmp_config->external ) && ! empty( $tmp_config->external ) ) {
89
						self::$init['external']->$slug = $tmp_config;
90
					} else {
91
						self::$init[ $plugin_slug ]->$slug = $tmp_config;
92
					}
93
				}
94
			} else {
95
				self::$init[ $tmp_config->slug ] = $tmp_config;
96
			}
97
		}
98
	}
99
} // End if().
100