Source of file Config.php
Size: 2,927 Bytes - Last Modified: 2015-12-22T09:12:14-05:00
../src/Config.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
Covered by 3 test(s):
50
Covered by 2 test(s):
51
Covered by 1 test(s):
5253
Covered by 1 test(s):
54
Covered by 1 test(s):
5556
Covered by 2 test(s):
57
Covered by 2 test(s):
585960616263646566676869
Covered by 7 test(s):
7071
Covered by 7 test(s):
727374757677787980818283
Covered by 4 test(s):
84
Covered by 1 test(s):
8586
Covered by 3 test(s):
8788899091929394959697
Covered by 6 test(s):
98
Covered by 6 test(s):
99
Covered by 4 test(s):
100
Covered by 4 test(s):
101
Covered by 1 test(s):
102
Covered by 1 test(s):
103
Covered by 6 test(s):
104
Covered by 6 test(s):
105106107108109110111112113114115116117
Covered by 6 test(s):
118
Covered by 4 test(s):
119
Covered by 6 test(s):
120121
Covered by 6 test(s):
122123124
| <?php /** * Registry * * @category PHP * @package MvcLite * @subpackage Config * @since File available since release 3.0.x * @author Cory Collier <corycollier@corycollier.com> */ namespace MvcLite; use MvcLite\Traits\Singleton as SingletonTrait; /** * Registry * * Data store for application level storage * * @category PHP * @package MvcLite * @subpackage Config * @since Class available since release 3.0.x * @author Cory Collier <corycollier@corycollier.com> */ class Config extends ObjectAbstract { use SingletonTrait; const MSG_ERR_BAD_CONFIG = 'Configuration not loaded. Bad file provided'; /** * Where all the config stuff goes * * @var array $data */ protected $data = []; /** * Init hook. * * @param string|array $config A filepath to an ini file, or an array of configuration options * * @return MvcLite\Config Returns $this, for object-chaining. */ public function init($config) { if (!is_array($config)) { if (!file_exists($config)) { throw new Exception(self::MSG_ERR_BAD_CONFIG); } $config = parse_ini_file($config); } $this->setAll($config); } /** * Setter for data. * * @param string $name The name to store the value as. * @param mixed $value The value to store. * * @return MvcLite\Config Returns $this, for object-chaining. */ public function set($name, $value) { $this->data[$name] = $value; return $this; } /** * Getter for data. * * @param string $name The name of what to get. * * @return MvcLite\Config Returns $this, for object-chaining. */ public function get($name) { if (array_key_exists($name, $this->data)) { return $this->data[$name]; } } /** * Gets an entire section of the configuration, by name. * * @param string $name The section name to retreive. * * @return array The results of the request. */ public function getSection($name) { $results = []; foreach ($this->data as $key => $value) { $parts = explode('.', $key); if ($name === $parts[0]) { $results[$key] = $value; } } return $results; } /** * Assigns multiple values to the config in a single method call. * * @param array $values A key/value array of things to store. * * @return MvcLite\Config Returns $this, for object-chaining. */ public function setAll($params = []) { // itereate through the built results, setting their values to registry foreach ($params as $setting => $values) { $this->set($setting, $values); } return $this; } } |