2012-01-13 11:58:06 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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-27 12:20:47 -05:00
|
|
|
|
2012-01-13 11:58:06 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-01-27 12:20:47 -05:00
|
|
|
|
2012-01-13 11:58:06 -05:00
|
|
|
/**
|
2012-04-05 12:48:10 -04:00
|
|
|
* Parent trait of base class, contains much of the magic
|
2012-01-13 11:58:06 -05:00
|
|
|
*/
|
2012-03-05 12:01:55 -05:00
|
|
|
trait JSObject {
|
2012-01-13 11:58:06 -05:00
|
|
|
|
|
|
|
/**
|
2012-01-27 12:20:47 -05:00
|
|
|
* Constructor for creating the objects
|
|
|
|
*/
|
2012-03-05 14:34:51 -05:00
|
|
|
public function __construct(array $members = array())
|
2012-01-27 12:20:47 -05:00
|
|
|
{
|
|
|
|
// Add the passed parameters to the object
|
|
|
|
foreach($members as $name => $value)
|
|
|
|
{
|
|
|
|
$this->$name = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PHP magic method to facilitate dynamic methods
|
2012-01-13 11:58:06 -05:00
|
|
|
*
|
2012-01-27 12:20:47 -05:00
|
|
|
* @param string $name
|
|
|
|
* @param array $args
|
2012-01-13 11:58:06 -05:00
|
|
|
*/
|
2012-03-05 14:34:51 -05:00
|
|
|
public function __call($name, array $args)
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-01-27 12:20:47 -05:00
|
|
|
if(is_callable($this->$name))
|
|
|
|
{
|
|
|
|
//Add $this object to args
|
|
|
|
array_push($args, $this);
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
//Call the dynamic function
|
|
|
|
return call_user_func_array($this->$name, $args);
|
2012-01-27 12:12:22 -05:00
|
|
|
}
|
2012-01-13 11:58:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-27 12:20:47 -05:00
|
|
|
* PHP magic method to facilitate dynamically set static methods
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param array $args
|
2012-01-13 11:58:06 -05:00
|
|
|
*/
|
2012-03-05 14:34:51 -05:00
|
|
|
public static function __callStatic($name, array $args)
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-01-27 12:20:47 -05:00
|
|
|
if(is_callable(self::$name))
|
|
|
|
{
|
|
|
|
return call_user_func_array(self::$name, $args);
|
|
|
|
}
|
2012-01-13 11:58:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints out the contents of the object when used as a string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-03-05 12:01:55 -05:00
|
|
|
public function __toString()
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-01-17 11:55:42 -05:00
|
|
|
if(ENVIRONMENT == 'DEVELOPMENT')
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-01-17 11:55:42 -05:00
|
|
|
$args = func_get_args();
|
|
|
|
$method = ( ! empty($args)) ? $args[0] : "print_r";
|
|
|
|
$data = (isset($args[1])) ? $args[1] : array();
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-01-17 11:55:42 -05:00
|
|
|
if(empty($data))
|
|
|
|
{
|
|
|
|
$data =& $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = '<pre>';
|
|
|
|
|
|
|
|
if($method == "var_dump")
|
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
var_dump($data);
|
|
|
|
$output .= ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
}
|
|
|
|
else if($method == "var_export")
|
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
var_export($data);
|
|
|
|
$output .= ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$output .= print_r($data, TRUE);
|
|
|
|
}
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-01-17 11:55:42 -05:00
|
|
|
return $output . '</pre>';
|
2012-01-13 11:58:06 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-17 11:55:42 -05:00
|
|
|
return '';
|
2012-01-13 11:58:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
/**
|
|
|
|
* PHP magic method to facilitate dynamic class loading
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2012-03-05 12:01:55 -05:00
|
|
|
public function __get($name)
|
2012-01-27 12:20:47 -05:00
|
|
|
{
|
|
|
|
$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');
|
|
|
|
|
|
|
|
if(class_exists($class, FALSE))
|
|
|
|
{
|
|
|
|
$this->$name = new $class;
|
|
|
|
}
|
|
|
|
}
|
2012-01-13 11:58:06 -05:00
|
|
|
|
|
|
|
/**
|
2012-01-27 12:20:47 -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
|
|
|
|
*
|
|
|
|
* @extends JSObject
|
|
|
|
*/
|
2012-03-05 12:01:55 -05:00
|
|
|
class miniMVC {
|
|
|
|
|
|
|
|
use JSObject;
|
2012-01-27 12:20:47 -05:00
|
|
|
|
|
|
|
private static $instance;
|
|
|
|
private static $count;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor - Any classes loaded here become subclasses of miniMVC
|
|
|
|
*/
|
2012-03-05 12:01:55 -05:00
|
|
|
protected function __construct()
|
2012-01-27 12:20:47 -05:00
|
|
|
{
|
|
|
|
self::$instance =& $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PHP magic method to facilitate dynamic methods
|
2012-01-13 11:58:06 -05:00
|
|
|
*
|
2012-01-27 12:20:47 -05:00
|
|
|
* @param string $name
|
|
|
|
* @param array $args
|
|
|
|
*/
|
2012-03-05 12:01:55 -05:00
|
|
|
public function __call($name, $args)
|
2012-01-27 12:20:47 -05:00
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public static function __invoke()
|
|
|
|
{
|
|
|
|
return self::get_instance();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton getter function
|
|
|
|
*
|
|
|
|
* @return miniMVC object
|
2012-01-13 11:58:06 -05:00
|
|
|
*/
|
2012-01-27 12:20:47 -05:00
|
|
|
public static function &get_instance()
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-01-27 12:20:47 -05:00
|
|
|
if( ! isset(self::$count))
|
|
|
|
{
|
|
|
|
self::$count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! isset(self::$instance))
|
|
|
|
{
|
|
|
|
self::$count++;
|
|
|
|
self::$instance = new miniMVC;
|
|
|
|
}
|
|
|
|
|
|
|
|
$self =& self::$instance;
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to load classes into the singleton
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2012-03-05 12:01:55 -05:00
|
|
|
public function load_class($name, $type='class')
|
2012-01-27 12:20:47 -05:00
|
|
|
{
|
|
|
|
switch($type)
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-01-27 12:20:47 -05:00
|
|
|
default:
|
|
|
|
$path = APP_PATH . "classes/{$name}.php";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "sys":
|
|
|
|
$path = SYS_PATH . "{$name}.php";
|
|
|
|
break;
|
2012-01-13 11:58:06 -05:00
|
|
|
}
|
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
// In a subdirectory? No problem
|
|
|
|
if(strpos("/", $name) !== FALSE)
|
|
|
|
{
|
|
|
|
$n = explode("/", $name);
|
|
|
|
$name = $n[count($n) -1];
|
|
|
|
}
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
$class = "{$name}";
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
if(class_exists($class, FALSE))
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-01-27 12:20:47 -05:00
|
|
|
if ( ! isset($this->$name))
|
|
|
|
{
|
|
|
|
$this->$name = new $class;
|
|
|
|
return;
|
|
|
|
}
|
2012-01-13 11:58:06 -05:00
|
|
|
}
|
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
if(is_file($path))
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-01-27 12:20:47 -05:00
|
|
|
require_once($path);
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
if(class_exists($class, FALSE))
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-01-27 12:20:47 -05:00
|
|
|
if ( ! isset($this->$name))
|
|
|
|
{
|
|
|
|
$this->$name = new $class;
|
|
|
|
return;
|
|
|
|
}
|
2012-01-13 11:58:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-27 12:20:47 -05:00
|
|
|
* Convenience function to remove an object from the singleton
|
2012-01-13 11:58:06 -05:00
|
|
|
*
|
2012-01-27 12:20:47 -05:00
|
|
|
* @param string $name
|
2012-01-13 11:58:06 -05:00
|
|
|
*/
|
2012-03-05 12:01:55 -05:00
|
|
|
public function unload($name)
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-01-27 12:20:47 -05:00
|
|
|
if(isset($this->$name))
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-01-27 12:20:47 -05:00
|
|
|
unset($this->$name);
|
2012-01-13 11:58:06 -05:00
|
|
|
}
|
2012-01-27 12:20:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience function to load config files
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2012-03-05 12:01:55 -05:00
|
|
|
public function load_config($name)
|
2012-01-27 12:20:47 -05:00
|
|
|
{
|
|
|
|
$path = APP_PATH . "config/{$name}.php";
|
|
|
|
|
|
|
|
if(is_file($path))
|
2012-01-13 11:58:06 -05:00
|
|
|
{
|
2012-01-27 12:20:47 -05:00
|
|
|
require_once($path);
|
2012-01-13 11:58:06 -05:00
|
|
|
}
|
2012-01-27 12:20:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
/**
|
|
|
|
* Base Controller Class
|
|
|
|
*
|
|
|
|
* @extends miniMVC
|
|
|
|
*/
|
|
|
|
class MM_Controller extends miniMVC {
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
public $output, $page;
|
2012-01-13 11:58:06 -05:00
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->output = new Output();
|
|
|
|
$this->page = new Page();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function for loading a model into the current class
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
*/
|
|
|
|
function load_model($file, $args=array())
|
|
|
|
{
|
|
|
|
$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";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_file($path))
|
|
|
|
{
|
|
|
|
require_once($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ! empty($args))
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->$file = new $file($args);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->$file = new $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function for loading a view
|
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param array $data
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-03-05 14:34:51 -05:00
|
|
|
function load_view($file, array $data=array(), $return=FALSE)
|
2012-01-27 12:20:47 -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}/views/{$file}.php";
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's not a module, or doesn't exist in the module view folder
|
|
|
|
// look in the app view folder
|
|
|
|
if( ! is_file($path))
|
|
|
|
{
|
|
|
|
$path = APP_PATH . "views/{$file}.php";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contain the content for buffering
|
|
|
|
ob_start();
|
|
|
|
|
|
|
|
// Extract the data array
|
|
|
|
extract($data);
|
|
|
|
|
|
|
|
// Include the file
|
|
|
|
include($path);
|
|
|
|
|
2012-01-27 12:12:22 -05:00
|
|
|
$buffer = ob_get_contents();
|
|
|
|
ob_end_clean();
|
2012-01-27 12:20:47 -05:00
|
|
|
|
|
|
|
if($return == TRUE)
|
|
|
|
{
|
|
|
|
return $buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->output->append_output($buffer);
|
|
|
|
}
|
|
|
|
|
2012-01-13 11:58:06 -05:00
|
|
|
}
|
|
|
|
}
|
2012-01-27 12:12:22 -05:00
|
|
|
|
2012-01-27 12:20:47 -05:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base Model Class
|
|
|
|
*
|
|
|
|
* @extends miniMVC
|
|
|
|
*/
|
|
|
|
class MM_Model extends miniMVC {
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the database class to the current model class
|
|
|
|
*/
|
|
|
|
function load_db($name="default")
|
|
|
|
{
|
|
|
|
$this->db =& db::get_instance($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// End of miniMVC.php
|