miniMVC/sys/miniMVC.php

189 lines
3.0 KiB
PHP

<?php
//Include common function
require('common.php');
/**
* JSObject
*
* Class for creating object-literal-like constructs in PHP
*/
class JSObject {
/**
* Constructor for creating the objects
*/
function __construct()
{
$args = func_get_args();
$members = $args[0];
// Add the passed parameters to the object
foreach($members as $name => $value)
{
if(is_array($value))
{
$value = new JSObject($value);
}
$this->$name = $value;
}
}
/**
* PHP magic method to facilitate dynamic methods
*
* @param string $name
* @param array $args
*/
function __call($name, $args)
{
if(is_callable($this->$name))
{
//Call the dynamic function
return call_user_func_array($this->$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>';
}
/**
* Constructor without the "new"
*
* @param array $members
*/
function __invoke($members=array())
{
return new JSObject($members);
}
}
/**
* Base class for the framework
*/
class miniMVC extends JSObject {
function __construct()
{
$this->buffer = "";
$this->headers = array();
parent::__construct(array(
'output' => array(
'set_header' => function($key, $val)
{
$this->headers[$key] = $val;
},
'append_output' => function($string)
{
$this->buffer .= $string;
},
'set_output' => function($string)
{
$this->buffer = $string;
}
),
));
}
/**
* PHP magic method called when ending the script
* Used for outputing HTML
*/
function __destruct()
{
// Set headers
foreach($this->headers as $key => $val)
{
@header("$key: $val");
}
}
/**
* PHP magic method to facilitate dynamic class loading
*
* @param string $name
*/
function __get($name)
{
$path = SYS_PATH."{$name}.php";
$class = "MM_{$key}";
//if(is_file($path))
//{
include($path);
if(class_exists($class, FALSE))
{
$this->$name = new $class();
}
//}
}
}
// --------------------------------------------------------------------------
class MM_Model extends miniMVC {
function __construct()
{
parent::__construct();
}
function __destruct()
{
parent::__destruct();
}
}
// --------------------------------------------------------------------------
class MM_Controller extends miniMVC {
function __construct()
{
parent::__construct();
}
function __destruct()
{
parent::__destruct();
}
}