|
<?php |
|
class Core { |
|
// (A) PROPERTIES |
|
public $error = ""; // last error message |
|
public $pdo = null; // database connection |
|
public $stmt = null; // sql statement |
|
public $loaded = []; // loaded modules |
|
|
|
// (B) CONNECT TO DATABASE |
|
function __construct () { |
|
$this->pdo = new PDO( |
|
"mysql:host=".DB_HOST.";charset=".DB_CHARSET.";dbname=".DB_NAME, |
|
DB_USER, DB_PASSWORD, [ |
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC |
|
]); |
|
} |
|
|
|
// (C) CLOSE CONNECTION WHEN DONE |
|
function __destruct () { |
|
if ($this->stmt !== null) { $this->stmt = null; } |
|
if ($this->pdo !== null) { $this->pdo = null; } |
|
} |
|
|
|
// (D) RUN SQL QUERY |
|
// $sql : sql query |
|
// $data : array of parameters |
|
function exec ($sql, $data=null) : void { |
|
$this->stmt = $this->pdo->prepare($sql); |
|
$this->stmt->execute($data); |
|
} |
|
|
|
// (E) FETCH SINGLE ROW |
|
// $sql : sql query |
|
// $data : array of parameters |
|
function fetch ($sql, $data=null) { |
|
$this->exec($sql, $data); |
|
return $this->stmt->fetch(); |
|
} |
|
|
|
// (F) FETCH MULTIPLE ROWS |
|
// $sql : sql query |
|
// $data : array of parameters |
|
// $arrange : (string) arrange by [$arrange] => results |
|
// (array) arrange by [$arrange[0] => $arrange[1]] |
|
// (none) default - whatever is set in pdo |
|
function fetchAll ($sql, $data=null, $arrange=null) { |
|
// (F1) RUN SQL QUERY |
|
$this->exec($sql, $data); |
|
|
|
// (F2) FETCH ALL AS-IT-IS |
|
if ($arrange===null) { return $this->stmt->fetchAll(); } |
|
|
|
// (F3) ARRANGE BY $DATA[$ARRANGE] => RESULTS |
|
else if (is_string($arrange)) { |
|
$data = []; |
|
while ($r = $this->stmt->fetch()) { $data[$r[$arrange]] = $row; } |
|
return $data; |
|
} |
|
|
|
// (F4) ARRANGE BY $DATA[$ARRANGE[0]] => $ARRANGE[1] |
|
else { |
|
$data = []; |
|
while ($r = $this->stmt->fetch()) { $data[$r[$arrange[0]]] = $r[$arrange[1]]; } |
|
return $data; |
|
} |
|
} |
|
|
|
// (G) LOAD SPECIFIED MODULE |
|
// $module : module to load |
|
function load ($module) : void { |
|
// (G1) CHECK IF MODULE IS ALREADY LOADED |
|
if (isset($this->loaded[$module])) { return; } |
|
|
|
// (G2) EXTEND MODULE |
|
$file = PATH_LIB . "LIB-$module.php"; |
|
if (file_exists($file)) { |
|
require $file; |
|
$this->loaded[$module] = new $module($this); |
|
} else { throw new Exception("$module module not found!"); } |
|
} |
|
|
|
// (H) "MAGIC LINK" TO MODULE |
|
function __get ($name) { |
|
if (isset($this->loaded[$name])) { return $this->loaded[$name]; } |
|
} |
|
} |
|
|
|
// (I) ALL MODULES SHOULD EXTEND THIS CORE CLASS |
|
class Ext { |
|
// (I1) LINK MODULE TO CORE |
|
public $Core; |
|
public $error; |
|
function __construct ($core) { |
|
$this->Core =& $core; |
|
$this->error =& $core->error; |
|
} |
|
|
|
// (I2) MAKE MODULES LINKING EASIER |
|
function __get ($name) { |
|
if (isset($this->Core->loaded[$name])) { return $this->Core->loaded[$name]; } |
|
} |
|
} |