miniMVC/sys/miniMVC.php

196 lines
3.6 KiB
PHP

<?php
/**
* Base class for the framework
*/
class miniMVC{
private static $instance;
private $buffer, $headers;
public static function get_instance()
{
if ( ! isset(self::$instance)) {
echo 'Creating new instance.';
$className = __CLASS__;
self::$instance = new $className;
}
return self::$instance;
}
function __construct()
{
// Load the page class
/*if( ! isset(self::$instance->page))
{
self::$instance->page &= new Page;
}
if( ! isset($this->output))
{
self::$instance->output &= new Output;
}*/
}
/**
* Magic function called when cloning an object
*/
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
/**
* Function for loading a view
*
* @param string $file
* @param array $data
* @return mixed
*/
function load_view($file, $data, $return=FALSE)
{
$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');
// 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);
$buffer = ob_get_contents();
ob_end_clean();
if($return)
{
return $buffer;
}
$this->output->append_output($buffer);
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* Prints out the contents of the object when used as a string
*
* @return string
*/
function __toString()
{
$args = func_get_args();
$method = ( ! empty($args)) ? $args[0] : "print_r";
$output = '<pre>';
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 . '</pre>';
}
/**
* 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(self::$instance->$name))
{
self::$instance->$name &= new $class;
return;
}
}
/*load_file($name, 'sys');
if(class_exists($class, FALSE))
{
self::$instance->$name &= new $class;
}*/
}
}
// End of miniMVC.php