not much
wewmy
This commit is contained in:
parent
4fd5e5fbd9
commit
f642090d47
15 changed files with 64 additions and 28 deletions
6
AroMVC/Base/Controller.php
Normal file
6
AroMVC/Base/Controller.php
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?php
|
||||||
|
namespace AroMVC\Base;
|
||||||
|
|
||||||
|
class Controller {
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Core;
|
namespace AroMVC\Base;
|
||||||
use AroMVC\Core\Database as db;
|
use AroMVC\Database\Database as db;
|
||||||
|
|
||||||
abstract class Model {
|
abstract class Model {
|
||||||
protected $rawData = [];
|
protected $rawData = [];
|
||||||
|
@ -14,7 +14,7 @@ abstract class Model {
|
||||||
|
|
||||||
public function __construct() {}
|
public function __construct() {}
|
||||||
public static function withId(int $id) {
|
public static function withId(int $id) {
|
||||||
$new = new static();
|
$new = new static;
|
||||||
$new->initialize();
|
$new->initialize();
|
||||||
|
|
||||||
return $new;
|
return $new;
|
||||||
|
@ -29,8 +29,8 @@ abstract class Model {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract function initialize();
|
protected abstract function initialize();
|
||||||
protected function setTable(string $tableName) {
|
protected static function getTable(): string {
|
||||||
$this->table = $tableName;
|
return self::$table ?? get_class(new static);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function get(string $name) {
|
protected function get(string $name) {
|
||||||
|
@ -53,11 +53,15 @@ abstract class Model {
|
||||||
$name = strtolower($name);
|
$name = strtolower($name);
|
||||||
|
|
||||||
if(array_key_exists($name, $this->associations))
|
if(array_key_exists($name, $this->associations))
|
||||||
$rawData[$this->associations[$name]] = $value;
|
$modified = $this->associations[$name];
|
||||||
else if(array_key_exists($name, $this->rawData))
|
else if(array_key_exists($name, $this->rawData))
|
||||||
$rawData[$name] = $value;
|
$modified = $name;
|
||||||
else
|
else
|
||||||
throw new \Exception("Cannot set the value for property '$name'.");
|
throw new \Exception("Cannot set the value for property '$name'.");
|
||||||
|
|
||||||
|
$rawData[$modified] = $value;
|
||||||
|
if(!in_array($modified, $this->modified))
|
||||||
|
$this->modified[] = $modified;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function addHook(string $name, $func) {
|
protected function addHook(string $name, $func) {
|
||||||
|
@ -69,14 +73,22 @@ abstract class Model {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function select(): Selectable {
|
public static function select(): Selectable {
|
||||||
return db::select("*", new static)->from(self::$table);
|
return db::select("*", new static)->from(self::getTable());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update() {
|
public function update() {
|
||||||
|
if(count($this->modified) == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var $query = db::update(self::getTable());
|
||||||
|
foreach($this->modified as $field)
|
||||||
|
// TODO set ... value
|
||||||
|
|
||||||
|
db::batchQuery($query);
|
||||||
|
$this->modified = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete() {
|
public function delete(): void {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Core;
|
namespace AroMVC\Configuration;
|
||||||
|
|
||||||
class ConfigSection {
|
class ConfigSection {
|
||||||
protected $name = null;
|
protected $name = null;
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Core;
|
namespace AroMVC\Configuration;
|
||||||
|
|
||||||
define("AMVC_CNF_DB", "Database");
|
define("AMVC_CNF_DB", "Database");
|
||||||
define("AMVC_CNF_DB_DSN", "dsn");
|
define("AMVC_CNF_DB_DSN", "dsn");
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Core;
|
namespace AroMVC\Database;
|
||||||
use AroMVC\Core\Configuration as conf;
|
use AroMVC\Configuration\Configuration as conf;
|
||||||
|
|
||||||
define("AMVC_DB_FETCH_VOID", 0);
|
define("AMVC_DB_FETCH_VOID", 0);
|
||||||
define("AMVC_DB_FETCH_SCALAR", 1);
|
define("AMVC_DB_FETCH_SCALAR", 1);
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Core;
|
namespace AroMVC\Database;
|
||||||
|
|
||||||
class Deletable extends Queryable {
|
class Deletable extends Queryable {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Core;
|
namespace AroMVC\Database;
|
||||||
|
|
||||||
class Insertable extends Queryable {
|
class Insertable extends Queryable {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Core;
|
namespace AroMVC\Database;
|
||||||
|
|
||||||
class Modifiable extends Queryable {
|
class Modifiable extends Queryable {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Core;
|
namespace AroMVC\Database;
|
||||||
use AroMVC\Core\Database as db;
|
use AroMVC\Core\Database as db;
|
||||||
|
|
||||||
abstract class Queryable {
|
abstract class Queryable {
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Core;
|
namespace AroMVC\Database;
|
||||||
|
|
||||||
define("AMVC_QRY_SEL_LCK_JOIN", 1);
|
define("AMVC_QRY_SEL_LCK_JOIN", 1);
|
||||||
|
|
6
AroMVC/IO/Directory.php
Normal file
6
AroMVC/IO/Directory.php
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?php
|
||||||
|
namespace AroMVC\IO;
|
||||||
|
|
||||||
|
class Directory {
|
||||||
|
|
||||||
|
}
|
6
AroMVC/IO/File.php
Normal file
6
AroMVC/IO/File.php
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?php
|
||||||
|
namespace AroMVC\IO;
|
||||||
|
|
||||||
|
class File {
|
||||||
|
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC\Core;
|
namespace AroMVC\Routing;
|
||||||
|
|
||||||
class Router {
|
class Router {
|
||||||
|
|
3
conf.ini
3
conf.ini
|
@ -1,3 +1,6 @@
|
||||||
|
[Project]
|
||||||
|
namespace = "Fire"
|
||||||
|
|
||||||
[Database]
|
[Database]
|
||||||
dsn = "mysql:host=localhost;dbname=test"
|
dsn = "mysql:host=localhost;dbname=test"
|
||||||
username = "root"
|
username = "root"
|
||||||
|
|
21
index.php
21
index.php
|
@ -1,33 +1,36 @@
|
||||||
<?php
|
<?php
|
||||||
namespace AroMVC;
|
namespace AroMVC;
|
||||||
use AroMVC\Core\Configuration;
|
use AroMVC\Configuration\Configuration as conf;
|
||||||
use AroMVC\Core\Database;
|
use AroMVC\Database\Database as db;
|
||||||
use AroMVC\Core\Selectable;
|
use AroMVC\Database\Selectable;
|
||||||
use AroMVC\Models\Company;
|
use AroMVC\Models\Company;
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
$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) < 3)
|
if(count($parts) < 2)
|
||||||
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";
|
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
|
else
|
||||||
die("Autoloader failed: malformed class name $class");
|
die("Autoloader failed: malformed class name $class");
|
||||||
|
} else if($parts[0] == $userns) {
|
||||||
|
if(in_array($parts[1], ["Controllers", "Models", "ViewModels"]))
|
||||||
|
require_once $parts[1]. DIRECTORY_SEPARATOR. $parts[2] .".php";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO write error handler
|
// TODO write error handler
|
||||||
|
|
||||||
Configuration::initialize(file_get_contents("conf.ini"));
|
|
||||||
Database::initialize();
|
|
||||||
|
|
||||||
$tmp = new Selectable("*");
|
$tmp = new Selectable("*");
|
||||||
|
|
||||||
$tmp->from("Companies")
|
$tmp->from("Companies")
|
||||||
|
|
Reference in a new issue