diff --git a/app/config/db.php b/app/config/db.php index 9647d15..8de8d12 100644 --- a/app/config/db.php +++ b/app/config/db.php @@ -9,5 +9,5 @@ $db_conf = array( 'db' => '', 'prefix' => '', 'persist' => '', - ), + ) ); \ No newline at end of file diff --git a/app/errors/error_db.php b/app/errors/error_db.php new file mode 100644 index 0000000..85e159d --- /dev/null +++ b/app/errors/error_db.php @@ -0,0 +1,7 @@ +
+

A Database Error was encountered

+ +

Code:

+

Driver Code:

+

Message:

+
\ No newline at end of file diff --git a/app/errors/error_general.php b/app/errors/error_general.php index 43947d1..aaca8ab 100644 --- a/app/errors/error_general.php +++ b/app/errors/error_general.php @@ -3,16 +3,7 @@ Error diff --git a/app/errors/error_php.php b/app/errors/error_php.php index 9f31b3f..791f911 100644 --- a/app/errors/error_php.php +++ b/app/errors/error_php.php @@ -1,4 +1,4 @@ -
+

A PHP Error was encountered

Severity:

@@ -13,7 +13,7 @@

- File:
+ File:
Line:
Function:

diff --git a/app/errors/error_php_exception.php b/app/errors/error_php_exception.php new file mode 100755 index 0000000..0cc89a0 --- /dev/null +++ b/app/errors/error_php_exception.php @@ -0,0 +1,23 @@ +
+

An uncaught exception was thrown.

+ +

Message:

+ + + +

Backtrace:

+ getTrace() as $error): ?> + + +

+ File:
+ Line:
+ Function: + Args:

*/ ?> +

+ + +

+ + +
\ No newline at end of file diff --git a/index.php b/index.php index c79858e..fbd1ab4 100644 --- a/index.php +++ b/index.php @@ -1,7 +1,7 @@ Stack trace:
%s
thrown in %s on line %s"; + $message = $exception->getMessage(); - // alter your trace as you please, here - $trace = $exception->getTrace(); - - // build your tracelines - $result = array(); - foreach ($trace as $key => $stackPoint) { - $result[] = sprintf( - $traceline, - $key, - $stackPoint['file'], - $stackPoint['line'], - $stackPoint['function'], - implode(', ', $stackPoint['args']) - ); - } - // trace always ends with {main} - $result[] = '#' . ++$key . ' {main}'; - - // write tracelines into main template - $msg = sprintf( - $msg, - get_class($exception), - $exception->getMessage(), - $exception->getFile(), - $exception->getLine(), - implode("
", $result), - $exception->getFile(), - $exception->getLine() - ); - - echo $msg; + //$filepath = str_replace(BASE_PATH, "", $filepath); + + // Contain the content for buffering + ob_start(); + + include(APP_PATH.'/errors/error_php_exception.php'); + + $buffer = ob_get_contents(); + ob_end_clean(); + echo $buffer; } // -------------------------------------------------------------------------- @@ -338,7 +315,7 @@ function route() */ function site_url($segment) { - return $url = BASEURL . URL_INDEX_FILE . $segment; + return $url = BASE_URL . URL_INDEX_FILE . $segment; } // -------------------------------------------------------------------------- diff --git a/sys/db.php b/sys/db.php index 79c48f0..3e1fce8 100644 --- a/sys/db.php +++ b/sys/db.php @@ -4,24 +4,24 @@ */ class db extends PDO { - private $where; + private $statement; private static $instance; - public static function get_instance($dbname="default") + public static function get_instance($dbname="default", $options=array()) { - if ( ! isset(self::$instance)) + if ( ! isset(self::$instance[$dbname])) { - echo 'Creating new instance of db class.'; - self::$instance = new db($dbname); + //echo 'Creating new instance of db class.'; + self::$instance[$dbname] = new db($dbname); } - return self::$instance; + return self::$instance[$dbname]; } function __construct($dbname="default", $options=array()) { //Include the database config file - load_file('config/db','app'); + require(APP_PATH . 'config/db.php'); // Array manipulation is too verbose extract($db_conf[$dbname]); @@ -48,7 +48,8 @@ class db extends PDO { } $opts = array( - PDO::ATTR_PERSISTENT => (isset($persist)) ? $persist : FALSE, + //PDO::ATTR_PERSISTENT => (isset($persist)) ? $persist : FALSE, + //PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING, ); if( ! isset($persist)) @@ -59,6 +60,8 @@ class db extends PDO { $options = array_merge($opts, $options); parent::__construct($dsn, $user, $pass, $options); + + self::$instance[$dbname] =& $this; } // -------------------------------------------------------------------------- @@ -80,6 +83,22 @@ class db extends PDO { return call_user_func_array($this->$name, $args); } } + + // -------------------------------------------------------------------------- + + /** + * PHP magic methods to call non-static methods statically + * + * @param string $name + * @param array $args + */ + public static function __callStatic($name, $args) + { + if(is_callable(parent::$name)) + { + return call_user_func_array(parent::$name, $args); + } + } // -------------------------------------------------------------------------- @@ -124,18 +143,76 @@ class db extends PDO { * * @param array $members */ - function __invoke($db="default") + /*function __invoke($db="default") { return self::get_instance($db); } function start_transaction() { + if( ! $this->inTransaction()) + { + return $this->beginTransaction(); + } + } + + function commit() + { + if($this->inTransaction()) + { + return parent::commit(); + } } - function end_transaction() + function rollback() { - + if($this->inTransaction()) + { + return parent::rollBack(); + } + } + + function prepare($sql, $driver_options=array()) + { + $this->statement = parent::prepare($sql, $driver_options); + return $this->statement; + } + + function set() + { + + }*/ + + /** + * Returns the last error from the database + * + * @return string + */ + function get_last_error() + { + $error = array(); + + if(isset($this->statement)) + { + $error = $this->statement->errorInfo(); + } + else + { + $error = $this->errorInfo(); + } + + $code = $error[0]; + $driver_code = $error[1]; + $message = $error[2]; + + // Contain the content for buffering + ob_start(); + + include(APP_PATH.'/errors/error_db.php'); + + $buffer = ob_get_contents(); + ob_end_clean(); + echo $buffer; } } diff --git a/sys/miniMVC.php b/sys/miniMVC.php index 4cbc524..35a3d85 100644 --- a/sys/miniMVC.php +++ b/sys/miniMVC.php @@ -1,47 +1,24 @@ $value) + { + $this->$name = $value; + } + + } + + /** * PHP magic method to facilitate dynamic methods * * @param string $name @@ -49,16 +26,16 @@ class miniMVC{ */ function __call($name, $args) { - if(is_callable(self::$instance->$name)) + if(is_callable($this->$name)) { //Add $this object to args array_push($args, $this); //Call the dynamic function - return call_user_func_array(self::$instance->$name, $args); + return call_user_func_array($this->$name, $args); } } - + /** * PHP magic method to facilitate dynamically set static methods * @@ -73,7 +50,6 @@ class miniMVC{ } } - /** * Prints out the contents of the object when used as a string * @@ -81,31 +57,35 @@ class miniMVC{ */ function __toString() { - $args = func_get_args(); - $method = ( ! empty($args)) ? $args[0] : "print_r"; + // 32762 == (E_ALL|E_NOTICE|E_STRICT) & ~(E_ERROR | E_PARSE) + if(ini_get('error_reporting') == 32762) + { + $args = func_get_args(); + $method = ( ! empty($args)) ? $args[0] : "print_r"; + + $output = '
';
 		
-		$output = '
';
-	
-		if($method == "var_dump")
-		{
-			ob_start();
-			var_dump($this);
-			$output .= ob_get_contents();
-			ob_end_clean();
+			if($method == "var_dump")
+			{
+				ob_start();
+				var_dump($this);
+				$output .= ob_get_contents();
+				ob_end_clean();
+			}
+			else if($method == "var_export")
+			{
+				ob_start();
+				var_export($this);
+				$output .= ob_get_contents();
+				ob_end_clean();
+			}	
+			else
+			{
+				$output .= print_r($this, TRUE);
+			}
+		
+			return $output . '
'; } - else if($method == "var_export") - { - ob_start(); - var_export($this); - $output .= ob_get_contents(); - ob_end_clean(); - } - else - { - $output .= print_r($this, TRUE); - } - - return $output . '
'; } /** @@ -135,6 +115,60 @@ class miniMVC{ } } + /** + * PHP magic method that is called when an object is treated as a function + */ + public static function __invoke() + { + $class = __CLASS__; + return new $class; + } +} + +// -------------------------------------------------------------------------- + +/** + * Base class for the framework + */ +class miniMVC extends JSObject{ + + private static $instance; + private static $count; + + /** + * Constructor - Any classes loaded here become subclasses of miniMVC + */ + public function __construct() + { + self::$instance =& $this; + } + + /** + * PHP magic method to facilitate dynamic methods + * + * @param string $name + * @param array $args + */ + function __call($name, $args) + { + if(is_callable(self::$instance->$name)) + { + //Add $this object to args + array_push($args, $this); + + //Call the dynamic function + return call_user_func_array(self::$instance->$name, $args); + } + } + + /** + * Magic function called when cloning an object + */ + public function __clone() + { + trigger_error('Clone is not allowed.', E_USER_ERROR); + } + /** * PHP magic method that is called when an object is treated as a function */ @@ -143,14 +177,54 @@ class miniMVC{ return self::get_instance(); } + /** + * Singleton getter function + * + * @return miniMVC object + */ + public static function get_instance() + { + if( ! isset(self::$count)) + { + self::$count = 0; + } + + if ( ! isset(self::$instance)) + { + self::$count++; + self::$instance = new miniMVC; + } + + $self =& self::$instance; + + return $self; + } + /** * Method to load classes into the singleton * * @param string $name */ - function load_class($name) + function load_class($name, $type='class') { - $path = APP_PATH . "classes/{$name}.php"; + switch($type) + { + default: + $path = APP_PATH . "classes/{$name}.php"; + break; + + case "sys": + $path = SYS_PATH . "{$name}.php"; + break; + } + + // In a subdirectory? No problem + if(strpos("/", $name) !== FALSE) + { + $n = explode("/", $name); + $name = $n[count($n) -1]; + } + $class = "{$name}"; if(class_exists($class, FALSE)) @@ -262,7 +336,7 @@ class MM_Controller extends miniMVC { * * @param string $file */ - function load_model($file) + function load_model($file, $args=array()) { $path = ""; @@ -281,7 +355,15 @@ class MM_Controller extends miniMVC { require_once($path); - $this->$file = new $file; + if( ! empty($args)) + { + + $this->$file = new $file(extract($args)); + } + else + { + $this->$file = new $file; + } } }