2011-12-27 13:24:28 -05:00
|
|
|
<?php
|
2012-01-09 10:32:52 -05:00
|
|
|
/**
|
|
|
|
* MiniMVC
|
|
|
|
*
|
|
|
|
* Convention-based micro-framework for PHP
|
|
|
|
*
|
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2011 - 2012
|
|
|
|
* @link https://github.com/timw4mail/miniMVC
|
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-01-04 19:47:32 -05:00
|
|
|
|
2011-12-27 13:24:28 -05:00
|
|
|
/**
|
2012-01-04 19:47:32 -05:00
|
|
|
* Parent class of base class, contains much of the magic
|
2011-12-27 13:24:28 -05:00
|
|
|
*/
|
2012-01-04 19:47:32 -05:00
|
|
|
class JSObject {
|
2011-12-29 11:24:10 -05:00
|
|
|
|
|
|
|
/**
|
2012-01-04 19:47:32 -05:00
|
|
|
* Constructor for creating the objects
|
|
|
|
*/
|
|
|
|
function __construct($members = array())
|
|
|
|
{
|
|
|
|
// Add the passed parameters to the object
|
|
|
|
foreach($members as $name => $value)
|
|
|
|
{
|
|
|
|
$this->$name = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-30 17:09:29 -05:00
|
|
|
* PHP magic method to facilitate dynamic methods
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param array $args
|
|
|
|
*/
|
|
|
|
function __call($name, $args)
|
|
|
|
{
|
2012-01-04 19:47:32 -05:00
|
|
|
if(is_callable($this->$name))
|
2011-12-30 17:09:29 -05:00
|
|
|
{
|
|
|
|
//Add $this object to args
|
|
|
|
array_push($args, $this);
|
|
|
|
|
|
|
|
//Call the dynamic function
|
2012-01-04 19:47:32 -05:00
|
|
|
return call_user_func_array($this->$name, $args);
|
2011-12-30 17:09:29 -05:00
|
|
|
}
|
|
|
|
}
|
2012-01-04 19:47:32 -05:00
|
|
|
|
2011-12-30 17:09:29 -05:00
|
|
|
/**
|
|
|
|
* PHP magic method to facilitate dynamically set static methods
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param array $args
|
|
|
|
*/
|
|
|
|
public static function __callStatic($name, $args)
|
|
|
|
{
|
|
|
|
if(is_callable(self::$name))
|
|
|
|
{
|
|
|
|
return call_user_func_array(self::$name, $args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-28 19:33:41 -05:00
|
|
|
/**
|
|
|
|
* Prints out the contents of the object when used as a string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function __toString()
|
2011-12-27 13:24:28 -05:00
|
|
|
{
|
2012-01-05 16:31:50 -05:00
|
|
|
$args = func_get_args();
|
|
|
|
$method = ( ! empty($args)) ? $args[0] : "print_r";
|
|
|
|
$data = (isset($args[1])) ? $args[1] : array();
|
|
|
|
|
2012-01-04 19:47:32 -05:00
|
|
|
// 32762 == (E_ALL|E_NOTICE|E_STRICT) & ~(E_ERROR | E_PARSE)
|
2012-01-05 16:31:50 -05:00
|
|
|
if(ini_get('error_reporting') == 32762 && empty($data))
|
2011-12-27 13:24:28 -05:00
|
|
|
{
|
2012-01-05 16:31:50 -05:00
|
|
|
$data =& $this;
|
|
|
|
}
|
2012-01-04 19:47:32 -05:00
|
|
|
|
2012-01-05 16:31:50 -05:00
|
|
|
$output = '<pre>';
|
2012-01-04 19:47:32 -05:00
|
|
|
|
2012-01-05 16:31:50 -05:00
|
|
|
if($method == "var_dump")
|
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
var_dump($data);
|
|
|
|
$output .= ob_get_contents();
|
|
|
|
ob_end_clean();
|
2011-12-28 19:33:41 -05:00
|
|
|
}
|
2012-01-05 16:31:50 -05:00
|
|
|
else if($method == "var_export")
|
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
var_export($data);
|
|
|
|
$output .= ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$output .= print_r($data, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output . '</pre>';
|
2011-12-27 13:24:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-30 17:09:29 -05:00
|
|
|
* PHP magic method to facilitate dynamic class loading
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
function __get($name)
|
|
|
|
{
|
|
|
|
$path = SYS_PATH."{$name}.php";
|
|
|
|
$class = "{$name}";
|
|
|
|
|
|
|
|
if(class_exists($class, FALSE))
|
|
|
|
{
|
|
|
|
if( ! isset($this->$name))
|
|
|
|
{
|
|
|
|
$this->$name = new $class;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
load_file($name, 'sys');
|
|
|
|
|
2011-12-28 19:33:41 -05:00
|
|
|
if(class_exists($class, FALSE))
|
|
|
|
{
|
2011-12-29 10:31:04 -05:00
|
|
|
$this->$name = new $class;
|
|
|
|
}
|
2011-12-30 17:09:29 -05:00
|
|
|
}
|
|
|
|
|
2012-01-04 19:47:32 -05:00
|
|
|
/**
|
|
|
|
* 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
|
2012-01-09 10:32:52 -05:00
|
|
|
*
|
|
|
|
* @extends JSObject
|
2012-01-04 19:47:32 -05:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-12-30 17:09:29 -05:00
|
|
|
/**
|
|
|
|
* PHP magic method that is called when an object is treated as a function
|
|
|
|
*/
|
|
|
|
public static function __invoke()
|
|
|
|
{
|
|
|
|
return self::get_instance();
|
|
|
|
}
|
2012-01-03 16:36:46 -05:00
|
|
|
|
2012-01-04 19:47:32 -05:00
|
|
|
/**
|
|
|
|
* Singleton getter function
|
|
|
|
*
|
|
|
|
* @return miniMVC object
|
|
|
|
*/
|
2012-01-11 10:26:56 -05:00
|
|
|
public static function &get_instance()
|
2012-01-04 19:47:32 -05:00
|
|
|
{
|
|
|
|
if( ! isset(self::$count))
|
|
|
|
{
|
|
|
|
self::$count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset(self::$instance))
|
|
|
|
{
|
|
|
|
self::$count++;
|
|
|
|
self::$instance = new miniMVC;
|
|
|
|
}
|
|
|
|
|
|
|
|
$self =& self::$instance;
|
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2012-01-03 16:36:46 -05:00
|
|
|
/**
|
|
|
|
* Method to load classes into the singleton
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2012-01-04 19:47:32 -05:00
|
|
|
function load_class($name, $type='class')
|
2012-01-03 16:36:46 -05:00
|
|
|
{
|
2012-01-04 19:47:32 -05:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2012-01-03 16:36:46 -05:00
|
|
|
$class = "{$name}";
|
|
|
|
|
|
|
|
if(class_exists($class, FALSE))
|
|
|
|
{
|
|
|
|
if ( ! isset($this->$name))
|
|
|
|
{
|
|
|
|
$this->$name = new $class;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_file($path))
|
|
|
|
{
|
|
|
|
require_once($path);
|
|
|
|
|
|
|
|
if(class_exists($class, FALSE))
|
|
|
|
{
|
|
|
|
if ( ! isset($this->$name))
|
|
|
|
{
|
|
|
|
$this->$name = new $class;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to remove an object from the singleton
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
function unload($name)
|
|
|
|
{
|
|
|
|
if(isset($this->$name))
|
|
|
|
{
|
|
|
|
unset($this->$name);
|
|
|
|
}
|
|
|
|
}
|
2012-01-08 16:07:54 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to load config files
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
function load_config($name)
|
|
|
|
{
|
|
|
|
$path = APP_PATH . "config/{$name}.php";
|
|
|
|
|
|
|
|
if(is_file($path))
|
|
|
|
{
|
|
|
|
require_once($path);
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 13:24:28 -05:00
|
|
|
}
|
|
|
|
|
2011-12-30 17:09:29 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-01-09 10:32:52 -05:00
|
|
|
/**
|
|
|
|
* Base Controller Class
|
|
|
|
*
|
|
|
|
* @extends miniMVC
|
|
|
|
*/
|
2011-12-29 10:31:04 -05:00
|
|
|
class MM_Controller extends miniMVC {
|
2011-12-27 13:24:28 -05:00
|
|
|
|
2012-01-11 10:26:56 -05:00
|
|
|
public $output, $page;
|
|
|
|
|
2011-12-29 10:31:04 -05:00
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
2012-01-11 10:26:56 -05:00
|
|
|
$this->output = new Output();
|
|
|
|
$this->page = new Page();
|
2011-12-30 17:09:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function for loading a model into the current controller
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
*/
|
2012-01-04 19:47:32 -05:00
|
|
|
function load_model($file, $args=array())
|
2011-12-30 17:09:29 -05:00
|
|
|
{
|
|
|
|
$path = "";
|
|
|
|
|
|
|
|
// The module is the lower of the class name
|
|
|
|
// need to figure out a way to allow multiple controllers
|
|
|
|
// in one module
|
|
|
|
$module = strtolower(get_class($this));
|
|
|
|
|
|
|
|
$not_modules = array('miniMVC', 'page', 'db', 'output');
|
|
|
|
|
|
|
|
// If it's a module, look in the module view folder
|
|
|
|
if( ! in_array($module, $not_modules))
|
|
|
|
{
|
|
|
|
$path = MOD_PATH . "{$module}/models/{$file}.php";
|
|
|
|
}
|
|
|
|
|
2012-01-05 16:31:50 -05:00
|
|
|
if(is_file($path))
|
|
|
|
{
|
|
|
|
require_once($path);
|
|
|
|
}
|
2011-12-30 17:09:29 -05:00
|
|
|
|
2012-01-04 19:47:32 -05:00
|
|
|
if( ! empty($args))
|
|
|
|
{
|
|
|
|
|
2012-01-05 16:31:50 -05:00
|
|
|
$this->$file = new $file($args);
|
2012-01-04 19:47:32 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->$file = new $file;
|
|
|
|
}
|
2011-12-30 17:09:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-01-09 10:32:52 -05:00
|
|
|
/**
|
|
|
|
* Base Model Class
|
|
|
|
*
|
|
|
|
* @extends miniMVC
|
|
|
|
*/
|
2011-12-30 17:09:29 -05:00
|
|
|
class MM_Model extends miniMVC {
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the database class to the current model class
|
|
|
|
*/
|
2012-01-05 16:31:50 -05:00
|
|
|
function load_db($name="default")
|
2011-12-30 17:09:29 -05:00
|
|
|
{
|
2012-01-05 16:31:50 -05:00
|
|
|
$this->db = db::get_instance($name);
|
2011-12-30 17:09:29 -05:00
|
|
|
}
|
|
|
|
|
2011-12-29 10:31:04 -05:00
|
|
|
}
|
|
|
|
// End of miniMVC.php
|