1: <?php
2: 3: 4: 5: 6: 7:
8:
9: namespace GLFramework;
10:
11:
12: use GLFramework\Module\ModuleManager;
13: use Symfony\Component\Yaml\Yaml;
14:
15: class Bootstrap
16: {
17: public static $VERSION = "0.2.1";
18: private static $singelton;
19: 20: 21:
22: private $manager;
23: private $events;
24: private $config;
25: private $directory;
26: private $startTime;
27: private $init = false;
28:
29: 30: 31:
32: public function __construct($directory, $config = "config.yml")
33: {
34: error_reporting(E_ERROR);
35: $this->startTime = microtime(true);
36: $this->events = new Events();
37: $this->directory = $directory;
38: $this->config = $this->loadConfig($this->directory, $config);
39: self::$singelton = $this;
40: }
41:
42: 43: 44: 45: 46: 47: 48:
49: public function loadConfig($folder, $file)
50: {
51: $config = Yaml::parse(file_get_contents($folder . "/{$file}"));
52: if(isset($config['include']))
53: {
54: $value = $config['include'];
55: if(!is_array($value)) $value = array($value);
56: foreach($value as $subfile)
57: {
58: $arr = $this->loadConfig($folder, $subfile);
59: $config = array_merge_recursive_ex($config, $arr);
60: }
61: unset($config['include']);
62: }
63: return $config;
64: }
65:
66: 67: 68: 69: 70:
71: public static function dispatch($response)
72: {
73: $response->display();
74: }
75:
76:
77: public static function getSingleton()
78: {
79: return self::$singelton;
80: }
81:
82: public static function isDebug()
83: {
84: $config = self::getSingleton()->getConfig();
85: return isset($config['app']['debug'])?$config['app']['debug']:false;
86: }
87:
88: 89: 90: 91:
92: public static function start($directory)
93: {
94: define("GL_TESTING", false);
95: define("GL_INSTALL", false);
96: $bootstrap = new Bootstrap($directory);
97: $url = $_SERVER['REQUEST_URI'];
98: $method = $_SERVER['REQUEST_METHOD'];
99: $bootstrap->run($url, $method)->display();
100: }
101:
102: 103: 104: 105:
106: public function init()
107: {
108: $this->init = true;
109: Log::d("Initializing framework...");
110: $this->register_error_handler();
111: date_default_timezone_set('Europe/Madrid');
112:
113: $this->manager = new ModuleManager($this->config, $this->directory);
114: $this->manager->init();
115: Log::d("Modules initialized: " . count($this->manager->getModules()));
116: }
117:
118: 119: 120:
121: public function setupTest()
122: {
123: define("GL_TESTING", true);
124: define("GL_INSTALL", false);
125: if(file_exists($this->directory . "/config.dev.yml"))
126: {
127: $this->overrideConfig($this->directory . "/config.dev.yml");
128: }
129: $this->init();
130: }
131:
132: 133: 134: 135: 136:
137: public function overrideConfig($file)
138: {
139: if(!$this->init)
140: {
141: $config = Yaml::parse(file_get_contents($file));
142: $this->config = array_merge_recursive_ex($this->config, $config);
143: }
144: else
145: {
146: throw new \Exception("Trying to override configuration after init()");
147: }
148: }
149:
150: 151: 152: 153: 154: 155:
156: public function run($url = null, $method = null)
157: {
158: $this->init();
159: Log::i("Welcome to GLFramework v" . $this->getVersion() . "");
160: Log::i("· PHP Version: " . phpversion());
161: Log::i("· Server Type: " . $_SERVER['SERVER_SOFTWARE']);
162: Log::i("· Server IP: " . $_SERVER['SERVER_ADDR'] . ":" . $_SERVER['SERVER_PORT']);
163: Log::i("· Extensiones de PHP: ");
164: Log::i(get_loaded_extensions());
165: session_start();
166: Events::fire('onCoreStartUp', $this->startTime);
167: $response = $this->manager->run($url, $method);
168: if($response)
169: $response->setUri($url);
170: return $response;
171: }
172:
173: 174: 175: 176: 177:
178: public function install()
179: {
180: define("GL_INSTALL", true);
181: $this->init();
182: echo "<pre>";
183: $db = new DatabaseManager();
184: if ($db->connect()) {
185: $this->log("Connection to database ok");
186:
187: $this->log("Installing Database...");
188: $models = $this->getModels();
189: foreach ($models as $model) {
190: $instance = new $model(null);
191: if ($instance instanceof Model) {
192: $diff = $instance->getStructureDifferences();
193: $this->log("Installing table '" . $instance->getTableName() . "' generated by " . get_class($instance) . "...", 2);
194:
195: foreach ($diff as $action) {
196: $this->log("Action: " . $action['sql'] . "...", 3, false);
197:
198: if (isset($_GET['exec']))
199: {
200: try{
201:
202: $db->exec($action['sql']);
203: $this->log("[OK]", 0);
204: }
205: catch(\Exception $ex)
206: {
207:
208: $this->log("[FAIL]", 0);
209: }
210: }
211: echo "\n";
212: }
213: }
214: }
215: if (!isset($_GET['exec'])) {
216: $this->log("Please <a href='?exec'>click here</a> to make this changes in the database.");
217:
218: } else {
219: $db2 = new DBStructure();
220: $db2->setDatabaseUpdate();
221: $this->log("All done site ready for develop/production!");
222: }
223: } else {
224: if ($db->getConnection() != null) {
225: if (isset($_GET['create_database'])) {
226: if ($db->exec("CREATE DATABASE " . $this->config['database']['database'])) {
227: echo "Database created successful! Please reload the navigator";
228: } else {
229: echo "Can not create the database!";
230: }
231: } else {
232: echo "Can not select the database <a href='install.php?create_database'>Try to create database</a>";
233: }
234:
235: } else {
236:
237: echo "Cannot connect to database";
238: }
239: }
240: }
241:
242: 243: 244: 245:
246: public function getConfig()
247: {
248: return $this->config;
249: }
250:
251: 252: 253: 254:
255: public function getDirectory()
256: {
257: return $this->directory;
258: }
259:
260: 261: 262:
263: public function getManager()
264: {
265: return $this->manager;
266: }
267:
268: 269: 270: 271:
272: public function getModels()
273: {
274: $list = array();
275: foreach($this->getManager()->getModules() as $module)
276: {
277: foreach($module->getModels() as $model)
278: {
279: $list[] = $model;
280: }
281: }
282: $files = scandir(__DIR__ . "/Model");
283: foreach($files as $file)
284: {
285: if($file == "." || $file == "..") continue;
286: $list[] = "GLFramework\\Model\\" . substr($file, 0, -4);
287: }
288: return $list;
289: }
290:
291:
292: 293: 294: 295: 296: 297:
298: public function log($message, $level = 1, $nl = true)
299: {
300: echo str_repeat("-", $level) . "> " . $message . ($nl?"\n":"");
301: }
302:
303:
304: function fatal_handler()
305: {
306: $errfile = "unknown file";
307: $errstr = "shutdown";
308: $errno = E_CORE_ERROR;
309: $errline = 0;
310:
311: $error = \error_get_last();
312:
313: if ($error !== NULL) {
314: $errno = $error["type"];
315: $errfile = $error["file"];
316: $errline = $error["line"];
317: $errstr = $error["message"];
318: if($errno | E_ERROR)
319: {
320: Log::getInstance()->error($errstr . " " . $errfile . " " . $errline);
321: if(isset($this->config['app']['ignore_errors']))
322: {
323: if(in_array($errno, $this->config['app']['ignore_errors'])) return;
324: }
325:
326: if(isset($this->config['app']['debug']) && $this->config['app']['debug'])
327: ($this->format_error($errno, $errstr, $errfile, $errline));
328: }
329: }
330:
331: }
332:
333: private function register_error_handler()
334: {
335: set_error_handler(array($this, "fatal_handler"));
336: register_shutdown_function(array($this, "fatal_handler"));
337: }
338:
339: function format_error($errno, $errstr, $errfile, $errline)
340: {
341:
342: echo "
343: <table>
344: <thead><th>Item</th><th>Description</th></thead>
345: <tbody>
346: <tr>
347: <th>Error</th>
348: <td><pre>$errstr</pre></td>
349: </tr>
350: <tr>
351: <th>Errno</th>
352: <td><pre>$errno</pre></td>
353: </tr>
354: <tr>
355: <th>File</th>
356: <td>$errfile</td>
357: </tr>
358: <tr>
359: <th>Line</th>
360: <td>$errline</td>
361: </tr>
362: <tr>
363: <th>Trace</th>
364: <td><pre>";
365: debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
366: echo "</pre></td>
367: </tr>
368: </tbody>
369: </table>";
370:
371:
372: }
373:
374: public function getVersion()
375: {
376: return self::$VERSION;
377: }
378:
379: }