miniMVC/sys/core/miniMVC.php

400 lines
8.0 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-05-03 07:56:14 -04:00
// ! JSObject Trait
// --------------------------------------------------------------------------
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-05-14 14:35:57 -04:00
public function __construct($members = [])
2012-01-27 12:20:47 -05:00
{
// Add the passed parameters to the object
2012-05-14 14:35:57 -04:00
foreach($members as $name => &$value)
2012-01-27 12:20:47 -05:00
{
$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-05-14 14:35:57 -04:00
public function __call($name, $params = [])
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
/**
* 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";
2012-05-14 14:35:57 -04:00
$data = (isset($args[1])) ? $args[1] : [];
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-05-14 14:35:57 -04:00
public static function __invoke($args = [])
2012-04-27 16:28:25 -04:00
{
$class = __CLASS__;
return new $class($args);
}
}
2012-05-03 07:56:14 -04:00
// --------------------------------------------------------------------------
// ! Singleton Trait
2012-04-30 16:24:05 -04:00
// --------------------------------------------------------------------------
2012-04-27 16:28:25 -04:00
/**
* Singleton pattern
*
* @package miniMVC
* @subpackage System
*/
trait Singleton {
/**
* Singleton object
*
* @var self
*/
2012-05-01 16:39:14 -04:00
protected static $instance;
/**
* Protected constructor for creating the one instance
*/
abstract protected function __construct();
2012-04-27 16:28:25 -04:00
/**
* PHP magic method that is called when an object is treated as a function
*
* @param array $params
* @return self
*/
2012-05-14 14:35:57 -04:00
public static function __invoke($params = [])
2012-04-27 16:28:25 -04:00
{
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
*/
2012-05-14 14:35:57 -04:00
public function __construct($members = [])
2012-04-27 16:28:25 -04:00
{
parent::__construct($members);
// Add the passed parameters to the object
2012-05-14 14:35:57 -04:00
foreach($members as $name => &$value)
2012-04-27 16:28:25 -04:00
{
$this->$name = $value;
}
}
// --------------------------------------------------------------------------
/**
* Allow calling of array methods on the object and
* dynamic methods
*
* @param string $name
* @param array $params
* @return mixed
*/
2012-05-14 14:35:57 -04:00
public function __call($name, $params = [])
2012-04-27 16:28:25 -04: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);
}
// 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-05-14 14:35:57 -04:00
public function __construct($members = [])
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-05-14 14:35:57 -04: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-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
2012-05-15 16:53:10 -04:00
* @return object
2012-01-27 12:20:47 -05:00
*/
2012-05-03 13:26:09 -04:00
public function load_class($name)
2012-01-27 12:20:47 -05:00
{
// In a subdirectory? No problem
2012-05-03 13:26:09 -04:00
if (strpos("/", $name) !== FALSE)
2012-01-27 12:20:47 -05:00
{
$n = explode("/", $name);
$name = $n[count($n) -1];
}
2012-01-13 11:58:06 -05:00
2012-05-03 13:26:09 -04:00
// Try system library first, then app library
$path = MM_SYS_PATH . "libraries/{$name}.php";
2012-05-15 16:53:10 -04:00
$name = "MM_{$name}";
2012-05-03 13:26:09 -04:00
if ( ! is_file($path))
{
$path = MM_APP_PATH . "classes/{$name}.php";
$name = str_replace('MM_', '', $name);
}
2012-01-27 12:20:47 -05:00
$class = "{$name}";
2012-01-13 11:58:06 -05:00
// Load the class file if need be
2012-05-03 13:26:09 -04:00
if ( ! class_exists($class, FALSE))
2012-01-13 11:58:06 -05:00
{
2012-05-03 13:26:09 -04:00
if (is_file($path))
2012-01-27 12:20:47 -05:00
{
require_once($path);
2012-01-27 12:20:47 -05:00
}
2012-01-13 11:58:06 -05:00
}
// Create the object, and add it to the current miniMVC object
if (class_exists($class, FALSE))
2012-01-13 11:58:06 -05:00
{
2012-05-03 13:26:09 -04:00
// Remove MM_ prefix from name
$name = str_replace('MM_', '', $name);
if ( ! isset($this->$name))
2012-01-13 11:58:06 -05:00
{
// Call a singleton, if the get_instance method exists
2012-05-03 13:26:09 -04:00
if (method_exists($class, 'get_instance'))
2012-01-27 12:20:47 -05:00
{
2012-05-15 10:23:31 -04:00
return $this->$name =& $class::get_instance();
2012-01-27 12:20:47 -05:00
}
2012-05-15 10:23:31 -04:00
return $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
* 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-05-03 13:26:09 -04: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
2012-05-03 13:26:09 -04: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