| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\Helpers; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Config |
| 7 |
* Read data config from file |
| 8 |
* |
| 9 |
* @package LP\Helpers |
| 10 |
* @since 4.1.6.4 |
| 11 |
* @version 1.0.1 |
| 12 |
*/ |
| 13 |
class Config { |
| 14 |
/* |
| 15 |
* All of configurations of items |
| 16 |
* |
| 17 |
* @var array |
| 18 |
*/ |
| 19 |
protected static $instance; |
| 20 |
/** |
| 21 |
* @var array Array name files config. |
| 22 |
*/ |
| 23 |
protected $config_files = array(); |
| 24 |
/** |
| 25 |
* @var string Folder store files config |
| 26 |
*/ |
| 27 |
protected $dir; |
| 28 |
|
| 29 |
/** |
| 30 |
* Config constructor. |
| 31 |
* |
| 32 |
* @param array $items |
| 33 |
*/ |
| 34 |
protected function __construct( array $items = array() ) { |
| 35 |
$this->dir = LP_PLUGIN_PATH . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get the specified configuration value |
| 40 |
* |
| 41 |
* @param string $key | Format key: file_name:key_name:key_item:... |
| 42 |
* @param string $path | from folder 'config' |
| 43 |
* |
| 44 |
* @return array|mixed |
| 45 |
* @version 1.0.1 |
| 46 |
* @since 4.1.6.4 |
| 47 |
*/ |
| 48 |
public function get( string $key = '', string $path = '', array $args = [] ) { |
| 49 |
// Extract args |
| 50 |
foreach ( $args as $arg_key => $arg_value ) { |
| 51 |
$$arg_key = $arg_value; |
| 52 |
} |
| 53 |
$data_config = array(); |
| 54 |
$data_config_by_key = array(); |
| 55 |
|
| 56 |
if ( empty( $key ) ) { |
| 57 |
return $data_config; |
| 58 |
} |
| 59 |
|
| 60 |
$keys = explode( ':', $key ); |
| 61 |
$file_name = $keys[0]; |
| 62 |
$file_path = $this->dir . $path . DIRECTORY_SEPARATOR . $file_name . '.php'; |
| 63 |
|
| 64 |
if ( ! file_exists( $file_path ) ) { |
| 65 |
return $data_config; |
| 66 |
} |
| 67 |
|
| 68 |
$store_file_config = $this->config_files[ $file_name ] ?? array(); |
| 69 |
if ( empty( $store_file_config ) ) { |
| 70 |
$this->config_files[ $file_name ] = include $file_path; |
| 71 |
} |
| 72 |
|
| 73 |
$data_config = $this->config_files[ $file_name ]; |
| 74 |
|
| 75 |
$number_keys = count( $keys ); |
| 76 |
|
| 77 |
if ( 1 === $number_keys ) { |
| 78 |
return $data_config; |
| 79 |
} else { |
| 80 |
$data_config_by_key = $data_config; |
| 81 |
for ( $i = 1; $i < $number_keys; $i ++ ) { |
| 82 |
$data_config_by_key = $data_config_by_key[ $keys[ $i ] ] ?? array(); |
| 83 |
} |
| 84 |
|
| 85 |
return $data_config_by_key; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
public static function instance() { |
| 90 |
if ( is_null( self::$instance ) ) { |
| 91 |
self::$instance = new self(); |
| 92 |
} |
| 93 |
|
| 94 |
return self::$instance; |
| 95 |
} |
| 96 |
} |
| 97 |
|