XmlStreamReader
3 weeks ago
partner-discount-sdk
3 weeks ago
api.php
3 weeks ago
arraytoxml.php
3 weeks ago
chunk.php
3 weeks ago
config.php
2 years ago
download.php
3 weeks ago
error.php
3 weeks ago
handler.php
3 weeks ago
helper.php
3 weeks ago
input.php
3 weeks ago
nested.php
3 weeks ago
rapidaddon.php
3 weeks ago
render.php
3 weeks ago
session.php
9 months ago
upload.php
3 weeks ago
zip.php
10 years ago
config.php
90 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class to load config files |
| 4 | * |
| 5 | * @author Maksym Tsypliakov <maksym.tsypliakov@gmail.com> |
| 6 | */ |
| 7 | class PMXI_Config implements IteratorAggregate { |
| 8 | /** |
| 9 | * Config variables stored |
| 10 | * @var array |
| 11 | */ |
| 12 | protected $config = array(); |
| 13 | /** |
| 14 | * List of loaded files in order to avoid loading same file several times |
| 15 | * @var array |
| 16 | */ |
| 17 | protected $loaded = array(); |
| 18 | |
| 19 | /** |
| 20 | * Static method to create config instance from file on disc |
| 21 | * @param string $filePath |
| 22 | * @param string[optional] $section |
| 23 | * @return PMXI_Config |
| 24 | */ |
| 25 | public static function createFromFile($filePath, $section = NULL) { |
| 26 | $config = new self(); |
| 27 | return $config->loadFromFile($filePath, $section); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Load config file |
| 32 | * @param string $filePath |
| 33 | * @param string[optional] $section |
| 34 | * @return PMXI_Config |
| 35 | */ |
| 36 | public function loadFromFile($filePath, $section = NULL) { |
| 37 | if ( ! is_null($section)) { |
| 38 | $this->config[$section] = self::createFromFile($filePath); |
| 39 | } else { |
| 40 | $filePath = realpath($filePath); |
| 41 | if ($filePath and ! in_array($filePath, $this->loaded)) { |
| 42 | require $filePath; |
| 43 | $config = (!isset($config)) ? array() : $config; |
| 44 | $this->loaded[] = $filePath; |
| 45 | $this->config = array_merge($this->config, $config); |
| 46 | } |
| 47 | } |
| 48 | return $this; |
| 49 | } |
| 50 | /** |
| 51 | * Return value of setting with specified name |
| 52 | * @param string $field Setting name |
| 53 | * @param string[optional] $section Section name to look setting in |
| 54 | * @return mixed |
| 55 | */ |
| 56 | public function get($field, $section = NULL) { |
| 57 | return ! is_null($section) ? $this->config[$section]->get($field) : $this->config[$field]; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Magic method for checking whether some config option are set |
| 62 | * @param string $field |
| 63 | * @return bool |
| 64 | */ |
| 65 | public function __isset($field) { |
| 66 | return isset($this->config[$field]); |
| 67 | } |
| 68 | /** |
| 69 | * Magic method to implement object-like access to config parameters |
| 70 | * @param string $field |
| 71 | * @return mixed |
| 72 | */ |
| 73 | public function __get($field) { |
| 74 | return $this->config[$field]; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Return all config options as array |
| 79 | * @return array |
| 80 | */ |
| 81 | public function toArray($section = NULL) { |
| 82 | return ! is_null($section) ? $this->config[$section]->toArray() : $this->config; |
| 83 | } |
| 84 | |
| 85 | #[\ReturnTypeWillChange] |
| 86 | public function getIterator() { |
| 87 | return new ArrayIterator($this->config); |
| 88 | } |
| 89 | |
| 90 | } |