miniMVC/sys/core/miniMVC.php

127 lines
2.5 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-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-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-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