miniMVC/sys/core/traits.php

201 lines
3.8 KiB
PHP

<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
* @package miniMVC
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
* @link https://github.com/timw4mail/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
// ! Generic Trait
// --------------------------------------------------------------------------
/**
* Simple Trait to include the toString() magic method
*
* @package miniMVC
* @subpackage System
*/
trait Generic {
/**
* Prints out the contents of the object when used as a string
*
* @return string
*/
public function __toString()
{
if (ENVIRONMENT == 'DEVELOPMENT')
{
$args = func_get_args();
$method = ( ! empty($args)) ? $args[0] : "print_r";
$data = (isset($args[1])) ? $args[1] : [];
if (empty($data))
{
$data =& $this;
}
$output = '<pre>';
if ($method == "var_dump")
{
ob_start();
var_dump($data);
$output .= ob_get_contents();
ob_end_clean();
}
elseif ($method == "var_export")
{
ob_start();
var_export($data);
$output .= ob_get_contents();
ob_end_clean();
}
else
{
$output .= print_r($data, TRUE);
}
return $output . '</pre>';
}
else
{
return '';
}
}
}
// --------------------------------------------------------------------------
// ! JSObject Trait
// --------------------------------------------------------------------------
/**
* Parent trait of base class, contains much of the magic
*
* @package miniMVC
* @subpackage System
*/
trait JSObject {
use Generic;
/**
* Constructor for creating the objects
*
* @param array $members
* @return void
*/
public function __construct($members = [])
{
// Add the passed parameters to the object
foreach($members as $name => &$value)
{
$this->$name = $value;
}
}
// --------------------------------------------------------------------------
/**
* PHP magic method to facilitate dynamic methods
*
* @param string $name
* @param array $params
*/
public function __call($name, $params = [])
{
if (is_callable($this->$name))
{
//Call the dynamic function
return call_user_func_array($this->$name, $params);
}
}
// --------------------------------------------------------------------------
/**
* PHP magic method that is called when an object is treated as a function
*
* @param array $args
*/
public static function __invoke($args = [])
{
$class = __CLASS__;
return new $class($args);
}
}
// --------------------------------------------------------------------------
// ! Singleton Trait
// --------------------------------------------------------------------------
/**
* Singleton pattern
*
* @package miniMVC
* @subpackage System
*/
trait Singleton {
use Generic;
/**
* Singleton object
*
* @var self
*/
protected static $instance;
/**
* Protected constructor for creating the one instance
*/
abstract protected function __construct();
/**
* PHP magic method that is called when an object is treated as a function
*
* @param array $params
* @return self
*/
public static function __invoke($params = [])
{
return self::get_instance($params);
}
// --------------------------------------------------------------------------
/**
* Singleton getter function
*
* @return self
*/
public static function &get_instance()
{
if ( ! isset(self::$instance))
{
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
// --------------------------------------------------------------------------
/**
* Magic function called when cloning an object
*/
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}
// End of traits.php