bug touch

don't
This commit is contained in:
Malloc of Kuzkycyziklistan 2017-02-24 16:01:32 -06:00
parent 8140d14f41
commit c2a7937329
11 changed files with 205 additions and 79 deletions

27
AroMVC/ConfigSection.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace AroMVC\Core;
class ConfigSection {
protected $name = null;
protected $section = null;
public function __construct(string $name, array $section) {
$this->section = $section;
$this->name = $name;
}
public function hasKey(string $key): bool {
return key_exists($key, $this->section);
}
public function value(string $key): string {
if($this->hasKey($key))
return $this->section[$key];
else
throw new \Exception("Section '$this->section' in configuration does not contain key '$key'");
}
public function getSectionName(): string {
return $this->name;
}
}

29
AroMVC/Configuration.php Normal file
View file

@ -0,0 +1,29 @@
<?php
namespace AroMVC\Core;
define("AMVC_CNF_DB", "Database");
define("AMVC_CNF_DB_DSN", "dsn");
define("AMVC_CNF_DB_USER", "username");
define("AMVC_CNF_DB_PASS", "password");
class Configuration {
protected static $data = null;
public static function initialize(string $data) {
if(self::$data != null)
return;
self::$data = parse_ini_string($data);
}
public static function section(string $name): ConfigSection {
if(self::hasSection($name))
return new ConfigSection($name, self::$data[$name]);
else
throw new \Exception("Could not locate section '$name' in configuration");
}
public static function hasSection(string $name): bool {
return key_exists($name, self::$data);
}
}

View file

@ -1,9 +1,38 @@
<?php <?php
namespace AroMVC\Core; namespace AroMVC\Core;
use AroMVC\Core\Configuration as conf;
define("AMVC_DB_FETCH_EMPTY", 0);
define("AMVC_DB_FETCH_SCALAR", 1);
define("AMVC_DB_FETCH_ROWS", 2);
class Database { class Database {
private static $preparedQueries = []; private static $dbConn = null;
private static $rawQueries = []; private static $queries = [];
public static function initialize() {
self::$dbConn = new \PDO(
conf::section(AMVC_CNF_DB)->value(AMVC_CNF_DB_DSN),
conf::section(AMVC_CNF_DB)->value(AMVC_CNF_DB_USER),
conf::section(AMVC_CNF_DB)->value(AMVC_CNF_DB_PASS)
);
}
public static function select($columns): Selectable {
return new Selectable($columns);
}
public static function rawQuery(string $query, $params = null, int $type = AMVC_DB_FETCH_ROWS) {
$hash = self::hashQuery($query);
if(key_exists($hash, self::$queries))
// TODO finish function
}
protected static function hashQuery(string $query): string {
while(($pos = strpos($query, ":")) !== false) {
// TODO finish hasher
}
return md5($query);
}
} }

View file

@ -1,7 +1,7 @@
<?php <?php
namespace AroMVC\Core; namespace AroMVC\Core;
abstract class AroModel { abstract class Model {
protected $rawData = []; protected $rawData = [];
protected $associations = []; protected $associations = [];
protected $hooks = []; protected $hooks = [];
@ -26,6 +26,10 @@ abstract class AroModel {
protected abstract function initialize(); protected abstract function initialize();
protected function get(string $name) {
return $this->rawData[$name];
}
public function __get(string $name) { public function __get(string $name) {
$name = strtolower($name); $name = strtolower($name);

View file

@ -1,38 +1,27 @@
<?php <?php
namespace AroMVC\Core; namespace AroMVC\Core;
define("AMVC_QRY_SEL_FROM", 1);
define("AMVC_QRY_SEL_JOINS", 2);
define("AMVC_QRY_SEL_NEEDON", 4);
define("AMVC_QRY_SEL_WHERE", 8);
define("AMVC_QRY_SEL_GROUP", 16);
define("AMVC_QRY_SEL_HAVING", 32);
define("AMVC_QRY_SEL_ORDER", 64);
define("AMVC_QRY_SEL_LIMIT", 128);
abstract class Queryable { abstract class Queryable {
protected $query = []; protected $query = "";
protected $params = [];
protected $results = null; protected $results = null;
protected $flags = 0; protected $flags = 0;
protected $allowConditionals = true; protected $locks = 0;
public function and(string $condition) { public function params(array $params) {
if(!$this->allowConditionals) $this->params = array_merge($this->params, $params);
throw new \Exception("Query type does not allow AND/OR subclauses"); return $this;
array_push($this->query, "AND", $condition);
} }
public function or(string $condition) { public function execute() {
if(!$this->allowConditionals)
throw new \Exception("Query type does not allow AND/OR subclauses");
array_push($this->query, "OR", $condition);
} }
public function execute(array $params) { /*protected function allowConditionals(bool $allow) {
// TODO execution logic if($allow)
var_dump($this->query); $this->setFlag(AMVC_QRY_CNDLS);
else
$this->clearFlag(AMVC_QRY_CNDLS);
} }
protected function setFlag(int $flag) { protected function setFlag(int $flag) {
@ -53,5 +42,21 @@ abstract class Queryable {
protected function checkForOrPast(int $flag): bool { protected function checkForOrPast(int $flag): bool {
return $this->checkFlag($flag) || $this->checkPast($flag); return $this->checkFlag($flag) || $this->checkPast($flag);
}*/
protected function isLocked() {
return $this->locks != 0;
}
protected function checkLock(int $lock) {
return ($this->locks & $lock) != 0;
}
protected function lock(int $lock) {
$this->locks |= $lock;
}
protected function unlock(int $lock) {
$this->locks &= ~$lock;
} }
} }

View file

@ -1,17 +1,41 @@
<?php <?php
namespace AroMVC\Core; namespace AroMVC\Core;
define("AMVC_QRY_SEL_LCK_JOIN", 1);
define("AMVC_QRY_SEL_COLS", 0);
define("AMVC_QRY_SEL_FROM", 1);
define("AMVC_QRY_SEL_JOINS", 2);
define("AMVC_QRY_SEL_WHERE", 3);
define("AMVC_QRY_SEL_GROUP", 4);
define("AMVC_QRY_SEL_HAVING", 5);
define("AMVC_QRY_SEL_ORDER", 6);
define("AMVC_QRY_SEL_LIMIT", 7);
// TODO allow appending and rewriting of existing data in a query from any point
class Selectable extends Queryable { class Selectable extends Queryable {
protected $components = [
AMVC_QRY_SEL_COLS => [],
AMVC_QRY_SEL_FROM => null,
AMVC_QRY_SEL_JOINS => [],
AMVC_QRY_SEL_WHERE => [],
AMVC_QRY_SEL_GROUP => [],
AMVC_QRY_SEL_HAVING => [],
AMVC_QRY_SEL_ORDER => [],
AMVC_QRY_SEL_LIMIT => null
];
public function __construct($selection) { public function __construct($selection) {
if(is_array($selection)) if(is_array($selection))
$selection = implode(",", $selection); $selection = implode(",", $selection);
$this->query = ["SELECT", $selection]; $this->components[AMVC_QRY_SEL_COLS] = $selection;
} }
public function execute(array $params): Selectable { public function execute(): Selectable {
if($this->checkFlag(AMVC_QRY_SEL_NEEDON)) if($this->checkFlag(AMVC_QRY_SEL_NEEDON))
throw new \Exception("JOIN clause declared in query with no matching ON clause"); throw new \Exception("JOIN clause declared in query with no matching ON or USING clause");
if($this->checkFlag(AMVC_QRY_SEL_GROUP) && !$this->checkFlag(AMVC_QRY_SEL_ORDER)) { if($this->checkFlag(AMVC_QRY_SEL_GROUP) && !$this->checkFlag(AMVC_QRY_SEL_ORDER)) {
if($this->checkFlag(AMVC_QRY_SEL_HAVING)) { if($this->checkFlag(AMVC_QRY_SEL_HAVING)) {
@ -23,26 +47,19 @@ class Selectable extends Queryable {
} }
} }
parent::execute($params); parent::execute();
return $this; return $this;
} }
protected function checkFrom() {
if(!$this->checkFlag(AMVC_QRY_SEL_FROM))
throw new \Exception("FROM clause must come first in a SELECT query");
}
public function from(string $where): Selectable { public function from(string $where): Selectable {
if($this->checkFlag(AMVC_QRY_SEL_FROM)) if($this->components[AMVC_QRY_SEL_FROM] != null)
throw new \Exception("Cannot declare second FROM clause on a SELECT query"); throw new \Exception("Cannot redefine FROM clause after first definition");
$this->setFlag(AMVC_QRY_SEL_FROM);
array_push($this->query, "FROM", $where); array_push($this->query, "FROM", $where);
return $this; return $this;
} }
public function join(string $type, string $table): Selectable { public function join(string $type, string $table): Selectable {
$this->checkFrom();
if($this->checkPast(AMVC_QRY_SEL_JOINS)) if($this->checkPast(AMVC_QRY_SEL_JOINS))
throw new \Exception("Invalid JOIN clause, must proceed FROM clause"); throw new \Exception("Invalid JOIN clause, must proceed FROM clause");
@ -83,20 +100,6 @@ class Selectable extends Queryable {
return $this; return $this;
} }
public function and(string $condition): Selectable {
if(!$this->checkFlag(AMVC_QRY_SEL_FROM))
throw new \Exception("Cannot use AND subclause until FROM clause has been declared in SELECT query");
parent::and($condition);
return $this;
}
public function or(string $condition): Selectable {
$this->checkFrom();
parent::or($condition);
return $this;
}
public function groupBy($columns): Selectable { public function groupBy($columns): Selectable {
$this->checkFrom(); $this->checkFrom();
if($this->checkForOrPast(AMVC_QRY_SEL_GROUP)) if($this->checkForOrPast(AMVC_QRY_SEL_GROUP))

View file

@ -1,7 +1,19 @@
<?php <?php
namespace AroMVC\Models; namespace AroMVC\Models;
use \AroMVC\Core\AroModel; use \AroMVC\Core\Model;
use \AroMVC\Core\Database as db;
class Company extends AroModel { class Company extends Model {
protected $id;
protected $name;
protected $states;
protected function initialize() {
$this->addHook("states", function() {
return db::select("*")
->from("States")
->where("`cid` = ?")
->params([$this->get("id")]);
});
}
} }

View file

@ -1,7 +1,21 @@
<?php <?php
namespace AroMVC\Models; namespace AroMVC\Models;
use \AroMVC\Core; use \AroMVC\Core\Model;
class State extends AroModel { class State extends Model {
protected $id;
protected $name;
protected $companyId;
protected $company;
protected $municipalities;
protected function initialize() {
$this->addHook("company", function() {
});
$this->addAssociation("companyId", "cid");
}
} }

View file

@ -1,18 +0,0 @@
<?php
spl_autoload_register(function($class) {
$class = str_replace("_", "\\", $class);
$class = ltrim($class, '\\');
$parts = explode("\\", $class);
if($parts[0] == "AroMVC") {
if(count($parts) < 3)
die("Autoloader failed: malformed class name $class");
if($parts[1] == "Core")
require_once "AroMVC". DIRECTORY_SEPARATOR . $parts[2] .".php";
else if(in_array($parts[1], ["Controllers", "Models", "ViewModels"]))
require_once $parts[1]. DIRECTORY_SEPARATOR. $parts[2] .".php";
else
die("Autoloader failed: malformed class name $class");
}
});

4
conf.ini Normal file
View file

@ -0,0 +1,4 @@
[Database]
dsn = "mysql:host=localhost;dbname=fire"
username = "squidlord"
password = ""

View file

@ -1,13 +1,30 @@
<?php <?php
namespace AroMVC; namespace AroMVC;
include "auto.php";
use AroMVC\Core\Selectable; use AroMVC\Core\Selectable;
use AroMVC\Models\Company; use AroMVC\Models\Company;
spl_autoload_register(function($class) {
$class = str_replace("_", "\\", $class);
$class = ltrim($class, '\\');
$parts = explode("\\", $class);
if($parts[0] == "AroMVC") {
if(count($parts) < 3)
die("Autoloader failed: malformed class name $class");
if($parts[1] == "Core")
require_once "AroMVC". DIRECTORY_SEPARATOR . $parts[2] .".php";
else if(in_array($parts[1], ["Controllers", "Models", "ViewModels"]))
require_once $parts[1]. DIRECTORY_SEPARATOR. $parts[2] .".php";
else
die("Autoloader failed: malformed class name $class");
}
});
$tmp = new Selectable("*"); $tmp = new Selectable("*");
$tmp->from("Companies") $tmp->from("Companies")
->where("`name` = ?") ->where("`name` = ?")
->or("`id` = ?") ->or("`id` = ?")
->execute(["dekko", 12]) ->execute()
->asModels(new Company); ->asModels(new Company);