Completed
Push — master ( 9bd8a0...c20901 )
by joanhey
01:43
created

Metadata::getMetadata()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 13
nc 3
nop 4
1
<?php
2
3
/**
4
 * KumbiaPHP web & app Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file LICENSE.txt.
10
 * It is also available through the world-wide-web at this URL:
11
 * http://wiki.kumbiaphp.com/Licencia
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @category   Kumbia
17
 * @package    ActiveRecord
18
 * @subpackage Metadata
19
 * @copyright  Copyright (c) 2005-2014  Kumbia Team (http://www.kumbiaphp.com)
20
 * @license    http://wiki.kumbiaphp.com/Licencia     New BSD License
21
 */
22
23
namespace Kumbia\ActiveRecord\Metadata;
24
25
/**
26
 * Metadata de tabla
27
 *
28
 */
29
abstract class Metadata
30
{
31
32
    /**
33
     * Singleton de metadata
34
     *
35
     * @var array
36
     */
37
    private static $instances = array();
38
39
    /**
40
     * Descripción de los campos
41
     *
42
     * @var array
43
     */
44
    protected $fields = array();
45
46
    /**
47
     * Lista de campos
48
     *
49
     * @var array
50
     */
51
    protected $fieldsList = array();
52
53
    /**
54
     * Clave primaria
55
     *
56
     * @var string
57
     */
58
    protected $pk;
59
60
    /**
61
     * Campos con valor predeterminado
62
     *
63
     * @var array
64
     */
65
    protected $withDefault = array();
66
67
    /**
68
     * Campos con valor autogenerado
69
     *
70
     * @var array
71
     */
72
    protected $autoFields = array();
73
74
    /**
75
     * Metadata de la tabla
76
     *
77
     * @param  string   $type tipo de controlador
78
     * @param  string   $database
79
     * @param  string   $table
80
     * @param  string   $schema
81
     * @return Metadata
82
     */
83
    public static function get($type, $database, $table, $schema = null)
84
    {
85
        if (isset(self::$instances["$database.$table.$schema"])) {
86
            return self::$instances["$database.$table.$schema"];
87
        }
88
89
        return self::getMetadata($type, $database, $table, $schema);
90
    }
91
    /**
92
     * Obtiene la metadata de la tabla
93
     * Y la cachea si esta en producción
94
     *
95
     * @param  string   $type tipo de controlador
96
     * @param  string   $database
97
     * @param  string   $table
98
     * @param  string   $schema
99
     * @return Metadata
100
     */
101
    private static function getMetadata($type, $database, $table, $schema)
102
    {
103
        if (PRODUCTION && !(self::$instances["$database.$table.$schema"] = \Cache::driver()->get("$database.$table.$schema", 'ActiveRecord.Metadata'))) {
104
            return self::$instances["$database.$table.$schema"];
105
        }
106
        $class = ucwords($type) . 'Metadata';
107
108
        $class = __NAMESPACE__ . "\\$class";
109
110
        self::$instances["$database.$table.$schema"] = new $class($database, $table, $schema);
111
112
         // Cachea los metadatos
113
        if (PRODUCTION) {
114
            \Cache::driver()->save(
115
                self::$instances["$database.$table.$schema"],
116
                \Config::get('config.application.metadata_lifetime'),
117
                "$database.$table.$schema",
118
                'ActiveRecord.Metadata'
119
            );
120
        }
121
122
        return self::$instances["$database.$table.$schema"];
123
    }
124
125
    /**
126
     * Constructor
127
     *
128
     * @param string $database base de datos
129
     * @param string $table    tabla
130
     * @param string $schema   squema
131
     */
132
    private function __construct($database, $table, $schema = null)
133
    {
134
        $this->fields = $this->queryFields($database, $table, $schema);
135
        $this->fieldsList = \array_keys($this->fields);
136
    }
137
138
    /**
139
     * Permite el filtrado de columna en PK, por Defecto y Autogenerado
140
     * @param $m información de la columna
141
     * @param $field nombre de la columna
142
     */
143
    protected function filterCol($m, $field)
144
    {
145
        if ($m['Key'] == 'PRI')
146
            $this->pk = $field;
147
        elseif ($m['Default'])
148
            $this->withDefault[] = $field;
149
        elseif ($m['Auto'])
150
            $this->autoFields[] = $field;
151
    }
152
153
    /**
154
     * Consultar los campos de la tabla en la base de datos
155
     *
156
     * @param  string $database base de datos
157
     * @param  string $table    tabla
158
     * @param  string $schema   squema
159
     * @return array
160
     */
161
    abstract protected function queryFields($database, $table, $schema = null);
162
163
    /**
164
     * Obtiene la descripción de los campos
165
     *
166
     * @return array
167
     */
168
    public function getFields()
169
    {
170
        return $this->fields;
171
    }
172
173
    /**
174
     * Obtiene la lista de campos
175
     *
176
     * @return array
177
     */
178
    public function getFieldsList()
179
    {
180
        return $this->fieldsList;
181
    }
182
183
    /**
184
     * Obtiene la clave primaria
185
     *
186
     * @return string
187
     */
188
    public function getPK()
189
    {
190
        return $this->pk;
191
    }
192
193
    /**
194
     * Obtiene los campos con valor predeterminado
195
     *
196
     * @return array
197
     */
198
    public function getWithDefault()
199
    {
200
        return $this->withDefault;
201
    }
202
203
    /**
204
     * Obtiene los campos con valor generado automatico
205
     *
206
     * @return array
207
     */
208
    public function getAutoFields()
209
    {
210
        return $this->autoFields;
211
    }
212
}
213