Overview

Namespaces

  • GLFramework
    • Cache
    • Controller
    • DaMa
      • Manipulators
    • Database
    • HTTP
    • Mail
    • Middleware
    • Model
    • Module
    • Tests
    • Upload
    • Utils
  • None

Classes

  • GLFramework\Bootstrap
  • GLFramework\Cache\Cache
  • GLFramework\Cache\MemcachedCache
  • GLFramework\Cache\MemoryCache
  • GLFramework\Controller
  • GLFramework\Controller\AdminController
  • GLFramework\Controller\APIController
  • GLFramework\Controller\AuthController
  • GLFramework\Controller\ErrorController
  • GLFramework\Controller\ExceptionController
  • GLFramework\DaMa\Association
  • GLFramework\DaMa\DataManipulation
  • GLFramework\DaMa\Manipulator
  • GLFramework\DaMa\Manipulators\CSVManipulator
  • GLFramework\DaMa\Manipulators\ManipulatorCore
  • GLFramework\DaMa\Manipulators\XLSManipulator
  • GLFramework\DaMa\Manipulators\XLSXManipulator
  • GLFramework\Database\Connection
  • GLFramework\Database\MySQLConnection
  • GLFramework\DatabaseManager
  • GLFramework\DBStructure
  • GLFramework\Events
  • GLFramework\Filesystem
  • GLFramework\HTTP\API
  • GLFramework\Log
  • GLFramework\Mail
  • GLFramework\Mail\Mail
  • GLFramework\Mail\MailSystem
  • GLFramework\Mail\PHPMailer
  • GLFramework\MailView
  • GLFramework\Middleware\APIAuthorizationMiddleware
  • GLFramework\Middleware\ControllerMiddleware
  • GLFramework\Model
  • GLFramework\Model\Page
  • GLFramework\Model\User
  • GLFramework\Model\UserPage
  • GLFramework\ModelResult
  • GLFramework\Module\Module
  • GLFramework\Module\ModuleManager
  • GLFramework\Request
  • GLFramework\Response
  • GLFramework\Tests\DatabaseTestCase
  • GLFramework\Tests\TestCase
  • GLFramework\Upload\Upload
  • GLFramework\Upload\Uploads
  • GLFramework\Utils\DataTablesManager
  • GLFramework\Versions
  • GLFramework\View

Interfaces

  • GLFramework\Middleware

Functions

  • array_merge_recursive_ex
  • e
  • file_get_php_classes
  • fix_date
  • fix_date_format
  • fix_decimal
  • fix_url
  • get
  • get_php_classes
  • http_response_code
  • instance_method
  • is_module_enabled
  • now
  • post
  • print_array
  • print_brief_debug
  • print_debug
  • random_str
  • reArrayFiles
  • reArrayPost
  • today
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Created by PhpStorm.
  4:  * User: manus
  5:  * Date: 13/1/16
  6:  * Time: 19:43
  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:      * @var \mysqli
 23:      */
 24: //    private static $link;
 25:     private static $selected;
 26:     private static $checked = false;
 27:     /**
 28:      * @var Connection
 29:      */
 30:     private static $connection;
 31: 
 32:     /**
 33:      * @var Cache
 34:      */
 35:     private static $cache;
 36: 
 37:     /**
 38:      * DBConnection constructor.
 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:      * @return Connection
 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: //                    throw new \Exception("Please, update database structure executing /install.php");
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:      * @return mixed
219:      * @deprecated
220:      */
221:     public function getLink()
222:     {
223:         return self::$link;
224:     }
225:     public function getConnection()
226:     {
227:         return self::$connection;
228:     }
229: 
230:     /**
231:      * @return Cache
232:      */
233:     public function getCache()
234:     {
235:         return self::$cache;
236:     }
237: 
238: }
API documentation generated by ApiGen