another wow
mewoio
This commit is contained in:
parent
f642090d47
commit
d536a8f4c4
10 changed files with 69 additions and 45 deletions
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Base;
|
namespace AroMVC\Base;
|
||||||
|
|
||||||
class Controller {
|
abstract class Controller {
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Base;
|
namespace AroMVC\Base;
|
||||||
use AroMVC\Database\Database as db;
|
use AroMVC\Database\Database as db;
|
||||||
|
use AroMVC\Database\Selectable;
|
||||||
|
|
||||||
abstract class Model {
|
abstract class Model {
|
||||||
protected $rawData = [];
|
protected $rawData = [];
|
||||||
|
@ -8,16 +9,21 @@ abstract class Model {
|
||||||
protected $hooks = [];
|
protected $hooks = [];
|
||||||
|
|
||||||
protected static $table = null;
|
protected static $table = null;
|
||||||
protected $index = "id";
|
protected static $index = "id";
|
||||||
protected $deleted = false;
|
protected $deleted = false;
|
||||||
protected $modified = [];
|
protected $modified = [];
|
||||||
|
|
||||||
public function __construct() {}
|
public function __construct() {}
|
||||||
public static function withId(int $id) {
|
public static function withId(int $id) {
|
||||||
$new = new static;
|
$row = db::select("*") ->from(self::getTable())
|
||||||
$new->initialize();
|
->where("`". self::$index ."` = :id")
|
||||||
|
->params(["id" => $id])
|
||||||
|
->asArray();
|
||||||
|
|
||||||
return $new;
|
if(count($row) == 0)
|
||||||
|
throw new \Exception("Cannot instantiate new ". get_class(new static) ." with id '$id' because it does not exist");
|
||||||
|
|
||||||
|
return self::withRow($row[0]);
|
||||||
}
|
}
|
||||||
public static function withRow(array $row) {
|
public static function withRow(array $row) {
|
||||||
$new = new static();
|
$new = new static();
|
||||||
|
@ -30,7 +36,8 @@ abstract class Model {
|
||||||
|
|
||||||
protected abstract function initialize();
|
protected abstract function initialize();
|
||||||
protected static function getTable(): string {
|
protected static function getTable(): string {
|
||||||
return self::$table ?? get_class(new static);
|
$fqcn = get_class(new static);
|
||||||
|
return self::$table ?? substr($fqcn, strrpos($fqcn, "\\")+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function get(string $name) {
|
protected function get(string $name) {
|
||||||
|
@ -72,15 +79,15 @@ abstract class Model {
|
||||||
$this->associations[strtolower($associationName)] = strtolower($rawName);
|
$this->associations[strtolower($associationName)] = strtolower($rawName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function select(): Selectable {
|
public static function select($columns = "*"): Selectable {
|
||||||
return db::select("*", new static)->from(self::getTable());
|
return db::select($columns, new static)->from(self::getTable());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update() {
|
public function update(): void {
|
||||||
if(count($this->modified) == 0)
|
if(count($this->modified) == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var $query = db::update(self::getTable());
|
$query = db::update(self::getTable());
|
||||||
foreach($this->modified as $field)
|
foreach($this->modified as $field)
|
||||||
// TODO set ... value
|
// TODO set ... value
|
||||||
|
|
||||||
|
|
6
AroMVC/Base/ViewModel.php
Normal file
6
AroMVC/Base/ViewModel.php
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?php
|
||||||
|
namespace AroMVC\Base;
|
||||||
|
|
||||||
|
abstract class ViewModel {
|
||||||
|
|
||||||
|
}
|
|
@ -88,7 +88,7 @@ class Database {
|
||||||
$query = trim($query);
|
$query = trim($query);
|
||||||
while(($pos = strpos($query, ":")) !== false) {
|
while(($pos = strpos($query, ":")) !== false) {
|
||||||
for($end = $pos+1;;$end++) {
|
for($end = $pos+1;;$end++) {
|
||||||
if(!ctype_alnum($query[$end]))
|
if($end == strlen($query) || !ctype_alnum($query[$end]))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Database;
|
namespace AroMVC\Database;
|
||||||
use AroMVC\Core\Database as db;
|
use AroMVC\Database\Database as db;
|
||||||
|
|
||||||
abstract class Queryable {
|
abstract class Queryable {
|
||||||
protected $query = "";
|
protected $query = "";
|
||||||
|
@ -16,6 +16,7 @@ abstract class Queryable {
|
||||||
|
|
||||||
public function execute() {
|
public function execute() {
|
||||||
$this->results = db::rawRowQuery($this->query, $this->params);
|
$this->results = db::rawRowQuery($this->query, $this->params);
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRawQuery() {
|
public function getRawQuery() {
|
||||||
|
|
|
@ -28,7 +28,7 @@ class Selectable extends Queryable {
|
||||||
|
|
||||||
protected function getModelReflection($model): \ReflectionClass {
|
protected function getModelReflection($model): \ReflectionClass {
|
||||||
$type = new \ReflectionClass($model);
|
$type = new \ReflectionClass($model);
|
||||||
if(!$type->isSubclassOf("\\AroMVC\\Core\\AroModel"))
|
if(!$type->isSubclassOf("\\AroMVC\\Base\\Model"))
|
||||||
throw new \Exception("Cannot instantiate non-model object.");
|
throw new \Exception("Cannot instantiate non-model object.");
|
||||||
|
|
||||||
return $type;
|
return $type;
|
||||||
|
@ -73,7 +73,7 @@ class Selectable extends Queryable {
|
||||||
|
|
||||||
if(!$this->isEmpty(AMVC_QRY_SEL_WHERE)) {
|
if(!$this->isEmpty(AMVC_QRY_SEL_WHERE)) {
|
||||||
$wheres = array_map(function($x) {
|
$wheres = array_map(function($x) {
|
||||||
return "($x)";
|
return "$x";
|
||||||
}, $this->components[AMVC_QRY_SEL_WHERE]);
|
}, $this->components[AMVC_QRY_SEL_WHERE]);
|
||||||
array_push($query, "WHERE", implode(" AND ", $wheres));
|
array_push($query, "WHERE", implode(" AND ", $wheres));
|
||||||
}
|
}
|
||||||
|
@ -191,19 +191,23 @@ class Selectable extends Queryable {
|
||||||
|
|
||||||
public function asArray() {
|
public function asArray() {
|
||||||
if($this->results == null)
|
if($this->results == null)
|
||||||
throw new \Exception("Cannot return results from a query that has not executed.");
|
$this->execute();
|
||||||
|
|
||||||
return $this->results;
|
return $this->results;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function asModels($model = null) {
|
public function asModels($model = null) {
|
||||||
|
echo "hi";
|
||||||
|
|
||||||
if($this->results == null)
|
if($this->results == null)
|
||||||
throw new \Exception("Cannot return results from a query that has not executed.");
|
$this->execute();
|
||||||
|
|
||||||
if($this->model == null || $model != null)
|
if($this->model == null || $model != null)
|
||||||
$this->model = $this->getModelReflection($model);
|
$this->model = $this->getModelReflection($model);
|
||||||
|
|
||||||
foreach($this->results as $result)
|
var_dump($this->results);
|
||||||
|
foreach($this->results as $result) {
|
||||||
yield $this->model->getMethod("withRow")->invoke(null, $result);
|
yield $this->model->getMethod("withRow")->invoke(null, $result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
namespace Fire\Controllers;
|
||||||
|
|
||||||
class Home {
|
class Home {
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Models;
|
namespace Fire\Models;
|
||||||
use \AroMVC\Core\Model;
|
use \AroMVC\Base\Model;
|
||||||
use \AroMVC\Core\Database as db;
|
use \AroMVC\Database\Database as db;
|
||||||
|
|
||||||
class Company extends Model {
|
class Company extends Model {
|
||||||
protected $id;
|
protected $id;
|
||||||
|
@ -10,10 +10,8 @@ class Company extends Model {
|
||||||
|
|
||||||
protected function initialize() {
|
protected function initialize() {
|
||||||
$this->addHook("states", function() {
|
$this->addHook("states", function() {
|
||||||
return db::select("*")
|
return State::select() ->where("`cid` = :cid")
|
||||||
->from("States")
|
->params(["cid" => $this->get("id")]);
|
||||||
->where("`cid` = ?")
|
|
||||||
->params([$this->get("id")]);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,10 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Models;
|
namespace Fire\Models;
|
||||||
use \AroMVC\Core\Model;
|
use \AroMVC\Base\Model;
|
||||||
|
use \AroMVC\Database\Database as db;
|
||||||
|
|
||||||
class State extends Model {
|
class State extends Model {
|
||||||
protected static $table = "State";
|
|
||||||
|
|
||||||
protected $id;
|
protected $id;
|
||||||
protected $name;
|
protected $name;
|
||||||
|
|
||||||
|
|
42
index.php
42
index.php
|
@ -1,42 +1,50 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC;
|
namespace AroMVC;
|
||||||
|
include "AroMVC/Configuration/Configuration.php";
|
||||||
|
include "AroMVC/Configuration/ConfigSection.php";
|
||||||
use AroMVC\Configuration\Configuration as conf;
|
use AroMVC\Configuration\Configuration as conf;
|
||||||
use AroMVC\Database\Database as db;
|
use AroMVC\Database\Database as db;
|
||||||
use AroMVC\Database\Selectable;
|
use AroMVC\Database\Selectable;
|
||||||
use AroMVC\Models\Company;
|
use Fire\Models\Company;
|
||||||
|
|
||||||
conf::initialize(file_get_contents("conf.ini"));
|
conf::initialize(file_get_contents("conf.ini"));
|
||||||
db::initialize();
|
|
||||||
|
|
||||||
spl_autoload_register(function($class) {
|
spl_autoload_register(function($class) {
|
||||||
$userns = Configuration::section("Project")->value("namespace");
|
$userns = conf::section("Project")->value("namespace");
|
||||||
|
|
||||||
$class = str_replace("_", "\\", $class);
|
$class = str_replace("_", "\\", $class);
|
||||||
$class = ltrim($class, '\\');
|
$class = ltrim($class, '\\');
|
||||||
|
|
||||||
$parts = explode("\\", $class);
|
$parts = explode("\\", $class);
|
||||||
if($parts[0] == "AroMVC") {
|
if($parts[0] == "AroMVC") {
|
||||||
if(count($parts) < 2)
|
if(count($parts) < 3)
|
||||||
die("Autoloader failed: malformed class name $class");
|
die("Autoloader failed: malformed class name $class");
|
||||||
|
|
||||||
if($parts[1] == "Core")
|
if($parts[1] == "Core")
|
||||||
require_once "AroMVC". DIRECTORY_SEPARATOR . $parts[2] .".php";
|
return;
|
||||||
else
|
|
||||||
die("Autoloader failed: malformed class name $class");
|
require_once "AroMVC" . DIRECTORY_SEPARATOR . $parts[1] . DIRECTORY_SEPARATOR . $parts[2] . ".php";
|
||||||
} else if($parts[0] == $userns) {
|
} else if($parts[0] == $userns) {
|
||||||
if(in_array($parts[1], ["Controllers", "Models", "ViewModels"]))
|
if(in_array($parts[1], ["Controllers", "Models", "ViewModels"]))
|
||||||
require_once $parts[1]. DIRECTORY_SEPARATOR. $parts[2] .".php";
|
require_once $parts[1] . DIRECTORY_SEPARATOR . $parts[2] .".php";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
db::initialize();
|
||||||
|
|
||||||
// TODO write error handler
|
// TODO write error handler
|
||||||
|
|
||||||
$tmp = new Selectable("*");
|
$company = Company::withId(1);
|
||||||
|
|
||||||
$tmp->from("Companies")
|
var_dump(
|
||||||
->where("`name` = :name OR `id` = :cid")
|
/*$tmp->from("Companies")
|
||||||
->params(["name" => "winco", "cid" => 20])
|
->where("`name` = :name OR `id` = :cid")
|
||||||
->join("LEFT JOIN", "Invoices")
|
->params(["name" => "winco", "cid" => 20])
|
||||||
->using("id")
|
->join("LEFT JOIN", "Invoices")
|
||||||
->execute()
|
->using("id")
|
||||||
->asModels(new Company);
|
->execute()
|
||||||
|
->asModels(new Company)*/
|
||||||
|
$company
|
||||||
|
);
|
||||||
|
|
||||||
|
echo $company->name;
|
||||||
|
|
||||||
|
var_dump($company->states->asModels());
|
Reference in a new issue