Config.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * AAM Configurations utility |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @version 7.0.0 |
| 15 | */ |
| 16 | class AAM_Framework_Utility_Config implements AAM_Framework_Utility_Interface |
| 17 | { |
| 18 | |
| 19 | use AAM_Framework_Utility_BaseTrait; |
| 20 | |
| 21 | /** |
| 22 | * Core AAM config db option |
| 23 | * |
| 24 | * @version 7.0.0 |
| 25 | */ |
| 26 | const DB_OPTION = 'aam_config'; |
| 27 | |
| 28 | /** |
| 29 | * Collection of configurations |
| 30 | * |
| 31 | * @var array |
| 32 | * @access private |
| 33 | * |
| 34 | * @version 7.0.0 |
| 35 | */ |
| 36 | private $_configs = null; |
| 37 | |
| 38 | /** |
| 39 | * @inheritDoc |
| 40 | */ |
| 41 | protected function __construct() |
| 42 | { |
| 43 | $this->_configs = apply_filters( |
| 44 | 'aam_init_config_filter', $this->_read_config() |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get all configurations or a very specific one |
| 50 | * |
| 51 | * @param string $name [Optional] |
| 52 | * @param mixed $default [Optional] |
| 53 | * |
| 54 | * @return mixed |
| 55 | * @access public |
| 56 | * |
| 57 | * @version 7.0.0 |
| 58 | */ |
| 59 | public function get($name = null, $default = null) |
| 60 | { |
| 61 | if (!empty($name)) { |
| 62 | if (array_key_exists($name, $this->_configs)) { |
| 63 | $result = $this->_configs[$name]; |
| 64 | } else { |
| 65 | $result = apply_filters('aam_get_config_filter', $default, $name); |
| 66 | } |
| 67 | } else { |
| 68 | $result = $this->_configs; |
| 69 | } |
| 70 | |
| 71 | return $result; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Set either a very specific config or all of them at once |
| 76 | * |
| 77 | * @param string|array $config |
| 78 | * @param mixed $value [Optional] |
| 79 | * |
| 80 | * @return bool |
| 81 | * @access public |
| 82 | * |
| 83 | * @version 7.0.0 |
| 84 | */ |
| 85 | public function set($config, $value = null) |
| 86 | { |
| 87 | if (is_array($config)) { |
| 88 | $this->_configs = $config; |
| 89 | } elseif (is_string($config)) { |
| 90 | $this->_configs[$config] = $value; |
| 91 | } else { |
| 92 | throw new InvalidArgumentException('The config is invalid'); |
| 93 | } |
| 94 | |
| 95 | // Saving the configurations in DB |
| 96 | return $this->_save_config(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Reset/delete a single configuration or all at once |
| 101 | * |
| 102 | * @param string $name [Optional] |
| 103 | * |
| 104 | * @return bool |
| 105 | * @access public |
| 106 | * |
| 107 | * @version 7.0.0 |
| 108 | */ |
| 109 | public function reset($name = null) |
| 110 | { |
| 111 | if (is_string($name)) { |
| 112 | if (array_key_exists($name, $this->_configs)) { |
| 113 | unset($this->_configs[$name]); |
| 114 | } |
| 115 | } else { |
| 116 | $this->_configs = []; |
| 117 | } |
| 118 | |
| 119 | return $this->_save_config(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Read configurations from DB |
| 124 | * |
| 125 | * @param mixed $default [Optional] |
| 126 | * |
| 127 | * @return mixed |
| 128 | * @access private |
| 129 | * |
| 130 | * @version 7.0.0 |
| 131 | */ |
| 132 | private function _read_config($default = []) |
| 133 | { |
| 134 | return AAM_Framework_Manager::_()->db->read(self::DB_OPTION, $default); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Save configurations to DB |
| 139 | * |
| 140 | * @return bool |
| 141 | * @access private |
| 142 | * |
| 143 | * @version 7.0.0 |
| 144 | */ |
| 145 | private function _save_config() |
| 146 | { |
| 147 | return AAM_Framework_Manager::_()->db->write( |
| 148 | self::DB_OPTION, $this->_configs |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | } |