Skip to content
Browse files

Updates for Database, ViewRoute, and syntax on ClassLoader.

  • Loading branch information...
1 parent 79e5813 commit 2dec80f48c7de512d3bfb284d789d3d804f23d96 @ringmaster committed Dec 6, 2011
Showing with 285 additions and 5 deletions.
  1. +2 −2 Application.php
  2. +1 −1 ClassLoader.php
  3. +154 −0 DB.php
  4. +91 −0 Model.php
  5. +2 −2 View.php
  6. +35 −0 ViewRoute.php
View
4 Application.php
@@ -15,7 +15,7 @@ public static function start() {
$controller_class = $route->controller;
$action = $route->action;
- $controller = new $route->controller();
+ $controller = new $controller_class();
$request_method = strtolower( $_SERVER['REQUEST_METHOD'] );
if ( method_exists( $controller, $action . '_' . $request_method ) ) {
$action .= '_' . $request_method;
@@ -67,7 +67,7 @@ public static function script_name() {
$scriptname = $_SERVER['PHP_SELF'];
break;
default:
- throw new exception('Could not determine script name.');
+ throw new \Exception('Could not determine script name.');
die();
}
return $scriptname;
View
2 ClassLoader.php
@@ -53,7 +53,7 @@ public function setNamespaceSeparator( $sep ) {
/**
* Gets the namespace seperator used by classes in the namespace of this class loader.
*
- * @return void
+ * @return string
*/
public function getNamespaceSeparator() {
return $this->_namespaceSeparator;
View
154 DB.php
@@ -0,0 +1,154 @@
+<?php
+
+namespace microsite\core;
+
+use \PDO;
+
+class DB extends PDO
+{
+ static $db = array();
+ protected $pdo_statement;
+ protected $fetch_class;
+
+ /**
+ * Get a database reference.
+ * @static
+ * @param string $index (optional) The database name
+ * @return DB The Referenced database object
+ */
+ public static function get($index = 'root')
+ {
+ if(empty(self::$db[$index])) {
+ self::$db[$index] = new DB(Config::get('connect_string'));
+ }
+ return self::$db[$index];
+ }
+
+ public static function disconnect($index = 'root')
+ {
+ self::$db[$index] = null;
+ }
+
+ public static function set($instance, $index = 0)
+ {
+ if($index === 0) {
+ $index = count(self::$db);
+ }
+ self::$db[$index] = $instance;
+ }
+
+ public function __construct($connect_string, $username = '', $password = '', $index = 'root')
+ {
+ try {
+ parent::__construct($connect_string, $username, $password);
+ $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
+ }
+ catch (\PDOException $e) {
+ throw new \Exception('Connection to "' . $connect_string . '" failed: ' . $e->getMessage() );
+ }
+
+ DB::set($this, $index);
+ }
+
+ public function query( $query, $args = array() )
+ {
+ if(!empty($this->pdo_statement)) {
+ $this->pdo_statement->closeCursor();
+ }
+
+ if($this->pdo_statement = $this->prepare($query, array(PDO::ATTR_EMULATE_PREPARES => true))) {
+ $this->pdo_statement->setFetchMode( PDO::FETCH_CLASS, $this->fetch_class );
+
+ if(!$this->pdo_statement->execute($args)) {
+ throw new \Exception($this->pdo_statement->errorInfo());
+ }
+ return true;
+ }
+ else {
+ throw new \Exception($this->errorInfo());
+ }
+ }
+
+ public function results($query, $args = array(), $class_name = 'Model')
+ {
+ $this->fetch_class = $class_name;
+ if ( $this->query( $query, $args ) ) {
+ return $this->pdo_statement->fetchAll();
+ }
+ else {
+ return false;
+ }
+ }
+
+ public function row($query, $args = array(), $class_name = 'Model')
+ {
+ $this->fetch_class = $class_name;
+
+ if ( $this->query( $query, $args ) ) {
+ return $this->pdo_statement->fetch();
+ }
+ else {
+ return false;
+ }
+ }
+
+ public function col($query, $args = array())
+ {
+ if ( $this->query( $query, $args ) ) {
+ return $this->pdo_statement->fetchAll(PDO::FETCH_COLUMN);
+ }
+ else {
+ return false;
+ }
+ }
+
+ public function val($query, $args = array())
+ {
+ if ( $this->query( $query, $args ) ) {
+ $result = $this->pdo_statement->fetch(PDO::FETCH_NUM);
+ return $result[0];
+ }
+ else {
+ return false;
+ }
+ }
+
+ public function assoc($query, $args = array(), $keyfield = 0, $valuefield = 1)
+ {
+ if ( $this->query( $query, $args ) ) {
+ if(is_string($valuefield)) {
+ $this->fetch_class = $valuefield;
+ $result = $this->pdo_statement->fetchAll();
+ }
+ else {
+ $result = $this->pdo_statement->fetchAll(PDO::FETCH_NUM);
+ }
+ $output = array();
+ foreach($result as $item) {
+ if(is_object($item)) {
+ $output[$item->$keyfield] = $item;
+ }
+ elseif(is_array($item)) {
+ $output[$item[$keyfield]] = $item[$valuefield];
+ }
+ }
+ return $output;
+ }
+ else {
+ return false;
+ }
+ }
+
+ public static function inclause($values, $prefix)
+ {
+ $out = array();
+ $index = 0;
+ foreach($values as $value) {
+ $index++;
+ $out[':' . $prefix . $index] = $value;
+ }
+ return $out;
+ }
+}
+
+?>
View
91 Model.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace microsite\core;
+
+class Model extends \microsite\core\BaseObject
+{
+ protected $fields = array();
+
+ function __construct()
+ {
+
+ }
+
+ public static function get($table, $crit, $class = 'Model') {
+ $sql = "SELECT * FROM {$table} WHERE ";
+ $sql .= implode(' AND ', array_map(array($this, '_pdo_insert_prefix'), array_keys($crit)));
+
+ DB::get()->row($sql, $crit, $class);
+ }
+
+ public function insert($table){
+ $sql = "INSERT INTO {$table} (";
+ $sql .= implode(', ', array_keys($this->fields));
+ $sql .= ') VALUES (';
+ $sql .= implode(', ', array_map(array($this, '_pdo_field_prefix'), array_keys($this->fields)));
+ $sql .= ');';
+ DB::get()->query($sql, $this->fields);
+ }
+
+ public function update($table, $index){
+ if(is_string($index)) {
+ $index = array($index => $this->fields[$index]);
+ }
+ $update_fields = array_diff_key($this->fields, $index);
+
+ $sql = "UPDATE {$table} SET ";
+ $sql .= implode(', ', array_map(array($this, '_pdo_insert_prefix'), array_keys($update_fields)));
+ $sql .= ' WHERE ';
+ $sql .= implode(' AND ', array_map(array($this, '_pdo_insert_prefix'), array_keys($index)));
+
+ DB::get()->query($sql, array_merge($update_fields, $index));
+ }
+
+ public function update_insert($table, $index) {
+ if(is_string($index)) {
+ $index = array($index => $this->fields[$index]);
+ }
+ $sql = "SELECT count(*) FROM {$table} WHERE ";
+ $sql .= implode(' AND ',array_map(array($this, '_pdo_insert_prefix'), array_keys($index)));
+
+ if(DB::get()->val($sql, $index) > 0) {
+ $this->update($table, $index);
+ }
+ else {
+ $this->insert($table);
+ }
+ }
+
+ public function __set($fieldname, $value) {
+ $this->fields[$fieldname] = $value;
+ }
+
+ public function __get($fieldname) {
+ return $this->fields[$fieldname];
+ }
+
+ public function __call($name, $value) {
+ if(array_key_exists($name, $this->fields)) {
+ $this->fields[$name] = $value[0];
+ return $this;
+ }
+ }
+
+ private function _pdo_field_prefix($field){
+ return ':' . $field;
+ }
+
+ private function _pdo_insert_prefix($field) {
+ return "{$field} = :{$field}";
+ }
+
+ public function std() {
+ $obj = new \StdClass();
+ foreach($this->fields as $key => $value) {
+ $obj->$key = $value;
+ }
+ return $obj;
+ }
+}
+
+?>
View
4 View.php
@@ -8,7 +8,7 @@ class View extends BaseObject {
public function __construct( $vars = array(), $template = null ) {
$this->vars = $vars;
if ( isset($template) ) {
- $this->render( $template );
+ echo $this->render( $template );
}
}
@@ -42,7 +42,7 @@ public function render( $viewname ) {
$views = $this->get_views();
if ( $viewname == '' ) {
- if ( $this->vars['_view'] != '' ) {
+ if ( !empty($this->vars['_view']) ) {
$viewname = $this->vars['_view'];
} else {
$viewname = '404';
View
35 ViewRoute.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace microsite\core;
+
+class ViewRoute extends BaseObject implements Route {
+ public $controller;
+ public $action;
+ public $view;
+ public $regex;
+ public $params;
+
+ public function __construct($view = '', $regex = '', $params = array()) {
+ $this->controller = $this;
+ $this->view = $view;
+ $this->action = 'show_view';
+ $this->regex = $regex;
+ $this->params = $params;
+ }
+
+ public function match($path) {
+ if(empty($this->regex)) return false;
+ if(preg_match($this->regex, $path, $matches)) {
+ $this->params = array_merge($matches, $this->params);
+ return true;
+ }
+ return false;
+ }
+
+ public function show_view($route) {
+ View::create($route->params, $route->view);
+ }
+
+
+}
+

0 comments on commit 2dec80f

Please sign in to comment.
Something went wrong with that request. Please try again.