-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.php
72 lines (59 loc) · 1.92 KB
/
App.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
use App\Core\Session;
use App\Core\Config;
use App\Controller\Controller;
use App\Core\Database\Connection;
class App {
private static $_self;
public $Config;
public $DB;
private $_params = [];
private function __construct(){
$this->parseParams();
}
public static function getInstance(){
if(!isset(self::$_self)){
self::$_self = new App();
}
return self::$_self;
}
public function init() {
$this->Config = Config::load(['config']);
$this->DB = Connection::get();
Session::init(Session::DATABASE);
$this->dispatch();
}
private function dispatch(){
$sPath = $this->getParam('path', 'index');
$sProcess = $this->getParam('process', 'process');
$sController = Controller::getControllerFromName(ucfirst($sPath));
if(!fileFromNamespaceExists($sController)){
$sController = Controller::getControllerFromName('Index');
}
$oController = new $sController();
if(!method_exists($oController, $sProcess)){
$sProcess = 'process';
}
$oController->$sProcess();
}
public function redirect($sLocation){
$sUrlPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
header('Location: ' . $sUrlPath . $sLocation);
die;
}
public function getParam($sParam, $xDefault = null){
$sParam = str_replace('.', '_', $sParam);
return isset($this->_params[$sParam]) ? trim($this->_params[$sParam]) : $xDefault;
}
private function parseParams(){
$aRaw = json_decode(file_get_contents('php://input'), true);
if(is_array($aRaw)){
foreach($aRaw as $sParam => $sValor){
$_REQUEST[$sParam] = $sValor;
}
}
foreach($_REQUEST as $sParam => $sValor){
$this->_params[$sParam] = $sValor;
}
}
}