miniMVC/sys/core/traits.php

201 lines
3.8 KiB
PHP
Raw Normal View History

2012-05-16 16:47:36 -04:00
<?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
*/
2012-05-17 16:22:39 -04:00
// --------------------------------------------------------------------------
2012-05-18 13:52:12 -04:00
// ! Generic Trait
2012-05-16 16:47:36 -04:00
// --------------------------------------------------------------------------
2012-05-18 13:52:12 -04:00
/**
* Simple Trait to include the toString() magic method
2012-05-16 16:47:36 -04:00
*
* @package miniMVC
* @subpackage System
*/
2012-05-18 13:52:12 -04:00
trait Generic {
2012-05-16 16:47:36 -04:00
/**
* Prints out the contents of the object when used as a string
*
* @return string
*/
public function __toString()
{
2012-05-18 13:52:12 -04:00
if (ENVIRONMENT == 'DEVELOPMENT')
2012-05-16 16:47:36 -04:00
{
$args = func_get_args();
$method = ( ! empty($args)) ? $args[0] : "print_r";
$data = (isset($args[1])) ? $args[1] : [];
2012-05-18 13:52:12 -04:00
if (empty($data))
2012-05-16 16:47:36 -04:00
{
$data =& $this;
}
$output = '<pre>';
2012-05-18 13:52:12 -04:00
if ($method == "var_dump")
2012-05-16 16:47:36 -04:00
{
ob_start();
var_dump($data);
$output .= ob_get_contents();
ob_end_clean();
}
2012-05-18 13:52:12 -04:00
elseif ($method == "var_export")
2012-05-16 16:47:36 -04:00
{
ob_start();
var_export($data);
$output .= ob_get_contents();
ob_end_clean();
}
else
{
$output .= print_r($data, TRUE);
}
return $output . '</pre>';
}
else
{
return '';
}
}
2012-05-18 13:52:12 -04:00
}
// --------------------------------------------------------------------------
// ! 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);
}
}
2012-05-16 16:47:36 -04:00
// --------------------------------------------------------------------------
/**
* 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 {
2012-05-18 13:52:12 -04:00
use Generic;
2012-05-16 16:47:36 -04:00
/**
* 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