miniMVC/src/sys/miniMVC.php

532 lines
10 KiB
PHP
Raw Normal View History

2012-01-13 11:58:06 -05:00
<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
* @package miniMVC
2012-01-13 11:58:06 -05:00
* @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
// --------------------------------------------------------------------------
// ! JSObject
// --------------------------------------------------------------------------
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
*
* @package miniMVC
* @subpackage System
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
*
* @param array $members
* @return void
2012-01-27 12:20:47 -05:00
*/
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;
}
}
// --------------------------------------------------------------------------
2012-01-27 12:20:47 -05:00
/**
* 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-13 11:58:06 -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
}
// --------------------------------------------------------------------------
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
{
if(ENVIRONMENT == 'DEVELOPMENT')
2012-01-13 11:58:06 -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
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
return $output . '</pre>';
2012-01-13 11:58:06 -05:00
}
else
{
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 = MM_SYS_PATH."{$name}.php";
2012-01-27 12:20:47 -05:00
$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-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;
}
}
// --------------------------------------------------------------------------
// ! MiniMVC
2012-01-27 12:20:47 -05:00
// --------------------------------------------------------------------------
/**
* Base class for the framework
*
* @package miniMVC
* @subpackage System
2012-01-27 12:20:47 -05:00
*/
class miniMVC extends ArrayObject {
2012-03-05 12:01:55 -05:00
use JSObject;
2012-01-27 12:20:47 -05:00
/**
* Singleton object
*
* @var miniMVC
*/
2012-01-27 12:20:47 -05:00
private static $instance;
/**
* Constructor - Any classes loaded here become subclasses of miniMVC
*
* @param array $members
2012-01-27 12:20:47 -05:00
*/
public function __construct(array $members=array())
2012-01-27 12:20:47 -05:00
{
// Allow the class to be used like an array
parent::__construct($members, STD_PROP_LIST | ARRAY_AS_PROPS);
2012-01-27 12:20:47 -05:00
self::$instance =& $this;
}
// --------------------------------------------------------------------------
2012-01-27 12:20:47 -05:00
/**
* 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
{
// Allow array operations on the object
if (substr($name, 0, 6) === 'array_' && is_callable($name))
{
$args = array_merge($this->getArrayCopy(), $args);
return call_user_func_array($name, $args);
}
// Call dynamic methods
if (is_callable(self::$instance->$name))
2012-01-27 12:20:47 -05:00
{
// Add $this object to args
2012-01-27 12:20:47 -05:00
array_push($args, $this);
// Call the dynamic function
2012-01-27 12:20:47 -05:00
return call_user_func_array(self::$instance->$name, $args);
}
}
// --------------------------------------------------------------------------
2012-01-27 12:20:47 -05:00
/**
* Magic function called when cloning an object
*/
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
// --------------------------------------------------------------------------
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()
{
return self::get_instance();
}
// --------------------------------------------------------------------------
2012-01-27 12:20:47 -05:00
/**
* 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::$instance))
{
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;
}
// --------------------------------------------------------------------------
2012-01-27 12:20:47 -05:00
/**
* Method to load classes into the singleton
*
* @param string $name
* @param string $type
* @return void
2012-01-27 12:20:47 -05:00
*/
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 = MM_APP_PATH . "classes/{$name}.php";
2012-01-27 12:20:47 -05:00
break;
case "sys":
$path = MM_SYS_PATH . "{$name}.php";
2012-01-27 12:20:47 -05:00
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-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
}
// --------------------------------------------------------------------------
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 = MM_APP_PATH . "config/{$name}.php";
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
}
}
2012-01-13 11:58:06 -05:00
// --------------------------------------------------------------------------
// ! MM_Controller
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
*
* @package miniMVC
* @subpackage System
2012-01-27 12:20:47 -05:00
*/
class MM_Controller extends miniMVC {
2012-01-13 11:58:06 -05:00
/**
* Instance of Output class
*
* @var Output
*/
public $output;
/**
* Instance of Page class
*
* @var Page
*/
public $page;
2012-01-13 11:58:06 -05:00
/**
* Create the controller object
*
* @return void
*/
public function __construct()
2012-01-27 12:20:47 -05:00
{
parent::__construct();
$this->output = new MM_Output();
$this->page = new MM_Page();
2012-01-27 12:20:47 -05:00
}
// --------------------------------------------------------------------------
2012-01-27 12:20:47 -05:00
/**
* Function for loading a model into the current class
*
* @param string $file
* @param array $args
* @return void
2012-01-27 12:20:47 -05:00
*/
public function load_model($file, $args=array())
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 = MM_MOD_PATH . "{$module}/models/{$file}.php";
2012-01-27 12:20:47 -05:00
}
if(is_file($path))
{
require_once($path);
}
if( ! empty($args))
{
$this->$file = new $file($args);
}
else
{
$this->$file = new $file;
}
}
// --------------------------------------------------------------------------
2012-01-27 12:20:47 -05:00
/**
* Function for loading a view
*
* @param string $file
* @param array $data
* @param bool $return
2012-01-27 12:20:47 -05:00
* @return mixed
*/
public 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 = MM_MOD_PATH . "{$module}/views/{$file}.php";
2012-01-27 12:20:47 -05:00
}
// 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 = MM_APP_PATH . "views/{$file}.php";
2012-01-27 12:20:47 -05:00
}
// Contain the content for buffering
ob_start();
// Extract the data array
extract($data);
// Include the file
include($path);
$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
}
}
// --------------------------------------------------------------------------
// ! MM_Model
2012-01-27 12:20:47 -05:00
// --------------------------------------------------------------------------
/**
* Base Model Class
*
* @package miniMVC
* @subpackage System
2012-01-27 12:20:47 -05:00
*/
class MM_Model extends miniMVC {
/**
* Initialize the model class
*
* @return void
*/
public function __construct()
2012-01-27 12:20:47 -05:00
{
parent::__construct();
}
// --------------------------------------------------------------------------
2012-01-27 12:20:47 -05:00
/**
* Adds the database class to the current model class
*
* @param string $name
* @return void
2012-01-27 12:20:47 -05:00
*/
public function load_db($name="default")
2012-01-27 12:20:47 -05:00
{
$this->db =& db::get_instance($name);
}
}
// End of miniMVC.php