miniMVC/src/sys/miniMVC.php

406 lines
7.8 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-04-27 16:28:25 -04:00
2012-01-13 11:58:06 -05:00
// --------------------------------------------------------------------------
2012-04-27 16:28:25 -04:00
// ! Traits
// --------------------------------------------------------------------------
2012-01-27 12:20:47 -05:00
2012-04-27 16:28:25 -04: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-04-27 16:28:25 -04:00
public function __construct($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
2012-04-26 16:50:41 -04:00
* @param array $params
2012-01-13 11:58:06 -05:00
*/
2012-04-27 16:28:25 -04:00
public function __call($name, $params = array())
2012-01-13 11:58:06 -05:00
{
2012-01-27 12:20:47 -05:00
if(is_callable($this->$name))
{
//Call the dynamic function
2012-04-26 16:50:41 -04:00
return call_user_func_array($this->$name, $params);
}
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-04-27 16:28:25 -04:00
public static function __callStatic($name, $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
/**
2012-04-27 16:28:25 -04:00
* PHP magic method that is called when an object is treated as a function
2012-01-27 12:20:47 -05:00
*
2012-04-27 16:28:25 -04:00
* @param array $args
2012-01-27 12:20:47 -05:00
*/
2012-04-27 16:28:25 -04:00
public static function __invoke($args = array())
{
$class = __CLASS__;
return new $class($args);
}
}
/**
* Singleton pattern
*
* @package miniMVC
* @subpackage System
*/
trait Singleton {
/**
* Singleton object
*
* @var self
*/
private static $instance;
/**
* PHP magic method that is called when an object is treated as a function
*
* @param array $params
* @return self
*/
public static function __invoke($params = array())
{
return self::get_instance($params);
}
2012-01-27 12:20:47 -05:00
2012-04-27 16:28:25 -04:00
// --------------------------------------------------------------------------
2012-01-27 12:20:47 -05:00
2012-04-27 16:28:25 -04:00
/**
* Singleton getter function
*
* @return self
*/
public static function &get_instance()
{
if ( ! isset(self::$instance))
2012-01-27 12:20:47 -05:00
{
2012-04-27 16:28:25 -04:00
$class = __CLASS__;
self::$instance = new $class;
2012-01-27 12:20:47 -05:00
}
2012-04-27 16:28:25 -04:00
return self::$instance;
2012-01-27 12:20:47 -05:00
}
2012-01-13 11:58:06 -05:00
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
2012-04-27 16:28:25 -04:00
* Magic function called when cloning an object
2012-01-27 12:20:47 -05:00
*/
2012-04-27 16:28:25 -04:00
public function __clone()
2012-01-27 12:20:47 -05:00
{
2012-04-27 16:28:25 -04:00
trigger_error('Clone is not allowed.', E_USER_ERROR);
2012-01-27 12:20:47 -05:00
}
}
// --------------------------------------------------------------------------
2012-04-27 16:28:25 -04:00
// ! Base Classes
2012-01-27 12:20:47 -05:00
// --------------------------------------------------------------------------
/**
2012-04-27 16:28:25 -04:00
* Class for standalone JSObject objects
2012-01-27 12:20:47 -05:00
*
* @package miniMVC
* @subpackage System
2012-01-27 12:20:47 -05:00
*/
2012-04-27 16:28:25 -04:00
class MM extends ArrayObject {
2012-03-05 12:01:55 -05:00
use JSObject;
2012-04-27 16:28:25 -04:00
/**
* Create the ArrayObject/JSObject hybrid object
*
* @param array
*/
public function __construct($members = array())
{
parent::__construct($members);
// Add the passed parameters to the object
foreach($members as $name => $value)
{
$this->$name = $value;
}
}
// --------------------------------------------------------------------------
/**
* Allow calling of array methods on the object and
* dynamic methods
*
* @param string $name
* @param array $params
* @return mixed
*/
public function __call($name, $params = array())
{
// 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);
}
// Allow dynamic method calls
if(is_callable($this->$name))
{
//Call the dynamic function
return call_user_func_array($this->$name, $params);
}
}
}
2012-01-27 12:20:47 -05:00
2012-04-27 16:28:25 -04:00
// --------------------------------------------------------------------------
/**
* Base class for the framework
*
* @package miniMVC
* @subpackage System
*/
class miniMVC extends MM {
use Singleton;
/**
2012-04-27 16:28:25 -04:00
* Reference to output class
*
2012-04-27 16:28:25 -04:00
* @var MM_Output
*/
2012-04-27 16:28:25 -04:00
public $output;
2012-01-27 12:20:47 -05:00
/**
* Constructor - Any classes loaded here become subclasses of miniMVC
*
* @param array $members
2012-01-27 12:20:47 -05:00
*/
2012-04-27 16:28:25 -04:00
public function __construct($members = array())
2012-01-27 12:20:47 -05:00
{
// Allow the class to be used like an array
2012-04-26 16:50:41 -04:00
parent::__construct($members);
2012-04-27 16:28:25 -04:00
$this->output = new MM_Output();
2012-01-27 12:20:47 -05:00
}
// --------------------------------------------------------------------------
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-04-27 16:28:25 -04:00
public function __call($name, $args = array())
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-04-27 16:28:25 -04:00
// Indirectly call output methods
if (is_callable(self::$instance->output->$name))
2012-01-27 12:20:47 -05:00
{
2012-04-27 16:28:25 -04:00
return call_user_func_array(self::$instance->output->$name, $args);
2012-01-27 12:20:47 -05:00
}
}
// --------------------------------------------------------------------------
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
2012-01-27 12:20:47 -05:00
// End of miniMVC.php