1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace GLFramework;
10:
11:
12: use GLFramework\Cache\Cache;
13: use GLFramework\Cache\MemoryCache;
14: use GLFramework\Database\Connection;
15: use GLFramework\Database\MySQLConnection;
16:
17: class DatabaseManager
18: {
19:
20:
21: 22: 23:
24:
25: private static $selected;
26: private static $checked = false;
27: 28: 29:
30: private static $connection;
31:
32: 33: 34:
35: private static $cache;
36:
37: 38: 39:
40: public function __construct()
41: {
42:
43: $this->connect();
44: }
45:
46: public function getConfig()
47: {
48: return Bootstrap::getSingleton()->getConfig();
49: }
50:
51: 52: 53:
54: public function instanceConnector()
55: {
56: $config = $this->getConfig();
57: if(isset($config['database']['connector']))
58: {
59: $connector = $config['database']['connector'];
60: return new $connector();
61: }
62: return new MySQLConnection();
63: }
64:
65: private function createCache()
66: {
67: $config = $this->getConfig();
68: if(isset($config['database']['cache']))
69: {
70: $configCache = $config['database']['cache'];
71: if(isset($configCache['connector']))
72: {
73: $this->instanceCache($configCache['connector']);
74: }
75: }
76: }
77: private function instanceCache($name)
78: {
79: self::$cache = new $name();
80: self::$cache->connect($this->getConfig());
81: }
82:
83: public function connect()
84: {
85: if (!self::$connection) {
86: $config = $this->getConfig();
87: self::$connection = $this->instanceConnector();
88: if(self::$connection->connect($config['database']['hostname'], $config['database']['username'], $config['database']['password']))
89: {
90: if(self::$connection->select_database($config['database']['database']))
91: {
92: self::$selected = true;
93: $this->createCache();
94:
95: if(!GL_INSTALL)
96: {
97: $this->checkDatabaseStructure();
98: }
99: return true;
100: }
101: }
102: else
103: {
104: throw new \Exception("Can not establish connection to database!");
105: }
106: }
107: return self::$selected;
108: }
109:
110: public static function isSelected()
111: {
112: return self::$selected;
113: }
114:
115: public function escape_string($string)
116: {
117: if(!self::$connection) return $string;
118:
119: if ($string == null) return $string;
120: return self::$connection->escape_string($string);
121: }
122:
123: public function cache($result, $key, $duration = null)
124: {
125: if($key && $this->getCache())
126: {
127: $this->getCache()->set($key, $result, $duration);
128: }
129: return $result;
130: }
131: public function pre_cache(&$result, $key)
132: {
133: if($key != null && $this->getCache())
134: {
135: if($this->getCache()->hash($key))
136: {
137: $result = $this->getCache()->get($key);
138: return true;
139: }
140: }
141: return false;
142: }
143:
144: public function checkDatabaseStructure()
145: {
146:
147: if(!self::$checked)
148: {
149: self::$checked = true;
150: $config = $this->getConfig();
151: if(!isset($config['database']['ignoreStructure']))
152: {
153: $manager = new DBStructure();
154: if($manager->haveModelChanges())
155: {
156:
157: $manager->executeModelChanges($this);
158: }
159: }
160: }
161: }
162: public function select($query, $cache = null, $duration = null)
163: {
164: if (self::$connection) {
165: if($this->pre_cache($result, $cache)) return $result;
166: return $this->cache(self::$connection->select($query, true), $cache, $duration);
167: } else {
168: throw new \Exception("Database connection is not open!");
169: }
170: }
171:
172: public function select_first($query, $cache = null)
173: {
174: $result = $this->select($query, $cache);
175: if ($result && count($result) > 0) return $result[0];
176: return $result;
177: }
178:
179: public function select_count($query, $cache = null)
180: {
181: $result = $this->select_first($query, $cache);
182: if ($result) return current($result);
183: return 0;
184: }
185:
186: public function exec($query, $removeCache = null)
187: {
188: if (self::$connection) {
189: if($this->getCache() && $removeCache) $this->getCache()->remove($removeCache);
190: return self::$connection->select($query, false);
191: } else {
192: throw new \Exception("Database connection is not open!");
193: }
194: throw new \Exception("Database connection is not open!");
195:
196: }
197:
198:
199: public function insert($query)
200: {
201: if($this->exec($query))
202: return $this->getLastInsertId();
203: return false;
204:
205: }
206:
207: public function getLastInsertId()
208: {
209: return $this->getConnection()->getLastInsertId();
210: }
211:
212: public function error()
213: {
214: return $this->getConnection()->getLastError();
215: }
216:
217: 218: 219: 220:
221: public function getLink()
222: {
223: return self::$link;
224: }
225: public function getConnection()
226: {
227: return self::$connection;
228: }
229:
230: 231: 232:
233: public function getCache()
234: {
235: return self::$cache;
236: }
237:
238: }