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):
  • MvcLite\ConfigTest::testInit with data set "simple config, no exception"
  • MvcLite\ConfigTest::testInit with data set "invalid file location, expect exception"
  • MvcLite\ConfigTest::testInit with data set "valid file location, no exception"
50
Covered by 2 test(s):
  • MvcLite\ConfigTest::testInit with data set "invalid file location, expect exception"
  • MvcLite\ConfigTest::testInit with data set "valid file location, no exception"
51
Covered by 1 test(s):
  • MvcLite\ConfigTest::testInit with data set "invalid file location, expect exception"
5253
Covered by 1 test(s):
  • MvcLite\ConfigTest::testInit with data set "valid file location, no exception"
54
Covered by 1 test(s):
  • MvcLite\ConfigTest::testInit with data set "valid file location, no exception"
5556
Covered by 2 test(s):
  • MvcLite\ConfigTest::testInit with data set "simple config, no exception"
  • MvcLite\ConfigTest::testInit with data set "valid file location, no exception"
57
Covered by 2 test(s):
  • MvcLite\ConfigTest::testInit with data set "simple config, no exception"
  • MvcLite\ConfigTest::testInit with data set "valid file location, no exception"
585960616263646566676869
Covered by 7 test(s):
  • MvcLite\ConfigTest::testSet with data set #0
  • MvcLite\ConfigTest::testSet with data set #1
  • MvcLite\ConfigTest::testSet with data set #2
  • MvcLite\ConfigTest::testSet with data set #3
  • MvcLite\ConfigTest::testSet with data set #4
  • MvcLite\ConfigTest::testGetSection with data set "empty expected"
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
7071
Covered by 7 test(s):
  • MvcLite\ConfigTest::testSet with data set #0
  • MvcLite\ConfigTest::testSet with data set #1
  • MvcLite\ConfigTest::testSet with data set #2
  • MvcLite\ConfigTest::testSet with data set #3
  • MvcLite\ConfigTest::testSet with data set #4
  • MvcLite\ConfigTest::testGetSection with data set "empty expected"
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
727374757677787980818283
Covered by 4 test(s):
  • MvcLite\ConfigTest::testGet with data set "key exists, expect value"
  • MvcLite\ConfigTest::testGet with data set "key does not exist, do notexpect value"
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
84
Covered by 1 test(s):
  • MvcLite\ConfigTest::testGet with data set "key exists, expect value"
8586
Covered by 3 test(s):
  • MvcLite\ConfigTest::testGet with data set "key does not exist, do notexpect value"
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
8788899091929394959697
Covered by 6 test(s):
  • MvcLite\ConfigTest::testGetSection with data set "empty everything"
  • MvcLite\ConfigTest::testGetSection with data set "empty data"
  • MvcLite\ConfigTest::testGetSection with data set "empty expected"
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
98
Covered by 6 test(s):
  • MvcLite\ConfigTest::testGetSection with data set "empty everything"
  • MvcLite\ConfigTest::testGetSection with data set "empty data"
  • MvcLite\ConfigTest::testGetSection with data set "empty expected"
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
99
Covered by 4 test(s):
  • MvcLite\ConfigTest::testGetSection with data set "empty everything"
  • MvcLite\ConfigTest::testGetSection with data set "empty data"
  • MvcLite\ConfigTest::testGetSection with data set "empty expected"
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
100
Covered by 4 test(s):
  • MvcLite\ConfigTest::testGetSection with data set "empty everything"
  • MvcLite\ConfigTest::testGetSection with data set "empty data"
  • MvcLite\ConfigTest::testGetSection with data set "empty expected"
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
101
Covered by 1 test(s):
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
102
Covered by 1 test(s):
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
103
Covered by 6 test(s):
  • MvcLite\ConfigTest::testGetSection with data set "empty everything"
  • MvcLite\ConfigTest::testGetSection with data set "empty data"
  • MvcLite\ConfigTest::testGetSection with data set "empty expected"
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
104
Covered by 6 test(s):
  • MvcLite\ConfigTest::testGetSection with data set "empty everything"
  • MvcLite\ConfigTest::testGetSection with data set "empty data"
  • MvcLite\ConfigTest::testGetSection with data set "empty expected"
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
  • MvcLite\DispatcherTest::testDispatch with data set "good controller request"
  • MvcLite\DispatcherTest::testDispatch with data set "bad controller request"
105106107108109110111112113114115116117
Covered by 6 test(s):
  • MvcLite\ConfigTest::testSetAll with data set "simple test"
  • MvcLite\ConfigTest::testSetAll with data set "nested test"
  • MvcLite\ConfigTest::testGetSection with data set "empty everything"
  • MvcLite\ConfigTest::testGetSection with data set "empty data"
  • MvcLite\ConfigTest::testGetSection with data set "empty expected"
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
118
Covered by 4 test(s):
  • MvcLite\ConfigTest::testSetAll with data set "simple test"
  • MvcLite\ConfigTest::testSetAll with data set "nested test"
  • MvcLite\ConfigTest::testGetSection with data set "empty expected"
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
119
Covered by 6 test(s):
  • MvcLite\ConfigTest::testSetAll with data set "simple test"
  • MvcLite\ConfigTest::testSetAll with data set "nested test"
  • MvcLite\ConfigTest::testGetSection with data set "empty everything"
  • MvcLite\ConfigTest::testGetSection with data set "empty data"
  • MvcLite\ConfigTest::testGetSection with data set "empty expected"
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
120121
Covered by 6 test(s):
  • MvcLite\ConfigTest::testSetAll with data set "simple test"
  • MvcLite\ConfigTest::testSetAll with data set "nested test"
  • MvcLite\ConfigTest::testGetSection with data set "empty everything"
  • MvcLite\ConfigTest::testGetSection with data set "empty data"
  • MvcLite\ConfigTest::testGetSection with data set "empty expected"
  • MvcLite\ConfigTest::testGetSection with data set "expectations"
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;
    }
}