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