Completed
Push — 1.0 ( 717237...5bd44b )
by joanhey
03:09 queued 01:29
created

Config::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://wiki.kumbiaphp.com/Licencia
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * @category   Kumbia
16
 * @package    Config
17
 * @copyright  Copyright (c) 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com)
18
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
19
 */
20
21
/**
22
 * Clase para la carga de Archivos .INI y de configuración
23
 *
24
 * Aplica el patrón Singleton que utiliza un array
25
 * indexado por el nombre del archivo para evitar que
26
 * un .ini de configuración sea leido mas de una
27
 * vez en runtime con lo que aumentamos la velocidad.
28
 *
29
 * @category   Kumbia
30
 * @package    Config
31
 */
32
class Config
33
{
34
35
    /**
36
     * Contain all the config
37
     * -
38
     * Contenido de variables de configuracion
39
     *
40
     * @var array
41
     */
42
    protected static $_vars = array();
43
44
    /**
45
     * Get config vars
46
     * -
47
     * Obtiene un atributo de configuracion
48
     *
49
     * @param string $var nombre de variable de configuracion
50
     * @return mixed
51
     */
52
    public static function get($var)
53
    {
54
        $namespaces = explode('.', $var);
55
        switch (count($namespaces)) {
56
            case 3:
57
                return isset(self::$_vars[$namespaces[0]][$namespaces[1]][$namespaces[2]]) ?
58
                             self::$_vars[$namespaces[0]][$namespaces[1]][$namespaces[2]] : NULL;
59
            case 2:
60
                return isset(self::$_vars[$namespaces[0]][$namespaces[1]]) ?
61
                             self::$_vars[$namespaces[0]][$namespaces[1]] : NULL;
62
            case 1:
63
                return isset(self::$_vars[$namespaces[0]]) ? self::$_vars[$namespaces[0]] : NULL;
64
        }
65
    }
66
    /**
67
     * Get all configs
68
     * -
69
     * Obtiene toda la configuración
70
     *
71
     * @return array
72
     */
73
    public static function getAll() {
74
        return self::$_vars;
75
    }
76
77
    /**
78
     * Set variable in config
79
     * -
80
     * Asigna un atributo de configuracion
81
     *
82
     * @param string $var   variable de configuracion
83
     * @param mixed  $value valor para atributo
84
     */
85
    public static function set($var, $value)
86
    {
87
        $namespaces = explode('.', $var);
88
        switch (count($namespaces)) {
89
            case 3:
90
                self::$_vars[$namespaces[0]][$namespaces[1]][$namespaces[2]] = $value;
91
                break;
92
            case 2:
93
                self::$_vars[$namespaces[0]][$namespaces[1]] = $value;
94
                break;
95
            case 1:
96
                self::$_vars[$namespaces[0]] = $value;
97
                break;
98
        }
99
    }
100
101
    /**
102
     * Read config file
103
     * -
104
     * Lee un archivo de configuracion
105
     *
106
     * @param string  $file  archivo .ini
107
     * @param boolean $force forzar lectura de .ini
108
     * @return array
109
     */
110
    public static function & read($file, $force = FALSE)
111
    {
112
        if (isset(self::$_vars[$file]) && !$force) {
113
            return self::$_vars[$file];
114
        }
115
        self::$_vars[$file] = parse_ini_file(APP_PATH . "config/$file.ini", TRUE);
116
        return self::$_vars[$file];
117
    }
118
119
}
120