this is the base engine of my framework called conventionally mashup it is currently in alpha versions but is coming along nicely. so guys dont be nice grill this code for any mistakes or things that could be better.
bootstrap.php
<?php
/* directory seperator constant */
define('DS' , '/');
/* php extension constant */
define('EXT' , '.php');
/* mashup version contant */
define('MASHUP_VERSION' , '0.1');
/* supported php version by Mashup */
define('SUPPORTED_PHP' , '5.3.0');
/* defines the paths constants and application and system directory names */
function define_paths($app_dir_name = 'application' , $sys_dir_name = 'system')
{
/* define __BASEPATH is the root path to the root directory*/
if(!defined('__BASEPATH__'))
{
/* replace all \ slashes with linux style slash / */
define('__BASEPATH__' , str_replace('\\' , '/' , getcwd()) . DS);
}
/* the __SYSTEM__ path constant to the system directory */
if(!defined('__SYSTEM__'))
{
define('__SYSTEM__' , __BASEPATH__ . $sys_dir_name . DS);
}
/* the __APP__ path constant to the application directory */
if(!defined('__APP__'))
{
define('__APP__' , __BASEPATH__ . $app_dir_name . DS);
}
/* define the __BASE__ path to the base directory loacted inside the system directory */
if(!defined('__BASE__'))
{
define('__BASE__' , __SYSTEM__ . 'base' . DS);
}
/* define the __CONFIG__ constant */
if(!defined('__CONFIG__'))
{
define('__CONFIG__' , __APP__ . 'settings' . DS);
}
}
/* check if the path constants have been defined */
if(!defined('__BASEPATH__'))
{
/* define the contants if not defined */;
define_paths();
}
/* get the global functions */
require_once(__BASE__ . 'globals' . EXT);
/* valid version of php */
compare_php(SUPPORTED_PHP);
// ------ test case on loading exception class and throwing a exception that is logged to the logs file.
try{
makeit();
}
catch(mashup_exception $e)
{
$e->log('exception log test');
}
function makeit()
{
if(!isset($thisstuff))
{
load('exception');
throw new mashup_exception('its all messed up');
}
}
globals.php
<?php if(!defined('__BASEPATH__')){exit('No Direct Access');}
/* compare PHP_VERSION against a version number */
function compare_php($supported_version)
{
/* PHP_VERSION number less than version number */
if(PHP_VERSION < $supported_version)
{
/* if comparison is less than exit and show message */
exit('Your version of php :"' . PHP_VERSION . '" is not supported by Gasoline Framework');
}
/* else return true */
return(TRUE);
}
/* ----------------------------------------------------------------------------------------------- */
/* a better version of is_writeable() */
/* if safe_mode or windows OS create a tmp file and attempt to write to it.*/
function is_file_writeable($file)
{
/* check for safe mode */
if (@ini_get("safe_mode") == FALSE AND is_writeable($file) === TRUE)
{
/* return true if writeable */
return(TRUE);
}
/* check if dir */
if (is_dir($file))
{
/* create a tmp file */
$file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
/* open file with read/write privales */
if (!$fp = @fopen($file, 'rw'))
{
return(FALSE);
}
/* close and change file permissions */
@fclose($fp);
@chmod($file, 0600);
@unlink($file);
return(TRUE);
}
/* re attempt to open file */
elseif (!$fp = @fopen($file, 'rw') === FALSE)
{
return(FALSE);
}
/* return true if all is ok */
fclose($fp);
return(TRUE);
}
/* ----------------------------------------------------------------------------------------------- */
function load($class , $dir = 'library' , $prefix = 'mash')
{
static $__classes = array();
if(isset($__classes[$class]))
{
return $__classes[$class];
}
else
{
foreach(array(__APP__ , __SYSTEM__) as $path)
{
$_prefix = $prefix;
if(file_exists($path . $dir . DS . $class . EXT))
{
require($path . $dir . DS . $class . EXT);
if(class_exists($class , FALSE))
{
is_loaded($class);
$__classes[$class] = new $class();
return $__classes[$class];
}
}
if(file_exists($path . $dir . DS . $_prefix . $class . EXT))
{
require($path . $dir . DS . $_prefix . $class . EXT);
if(class_exists($prefix.$class , FALSE))
{
is_loaded($_prefix.$class);
$__classes[$_prefix.$class] = new $class();
return $__classes[$_prefix.$class];
}
}
}
return(FALSE);
}
return(FALSE);
}
/* ----------------------------------------------------------------------------------------------- */
function is_loaded($class)
{
static $__loaded = array();
if(isset($__loaded[$class]))
{
return $__loaded;
}
else
{
$__loaded[$class] = $class;
}
return $__loaded;
}
function get_config(array $replace = array())
{
if(file_exists(__CONFIG__ . 'settings' . EXT))
{
require(__CONFIG__ . 'settings' . EXT);
if(!isset($settings) && !is_array($settings))
{
exit('your config file does not seem to be formatted correctly');
}
if(count($replace) > 0)
{
foreach($replace as $k => $v)
{
if(isset($settings[$k]))
{
$settings[$k] = $v;
}
}
}
}
else
{
return(FALSE);
}
return $settings;
}
/* ----------------------------------------------------------------------------------------------- */
function get_item($item)
{
$config = get_config();
if(isset($config[$item]))
{
return $config[$item];
}
return(FALSE);
}
function exception_handler()
{
}
?>
library class exception.php
class mashup_exception extends Exception
{
public function log($message , $level = 'ERROR' , $__php_error = 0)
{
$_LE = load('log');
$_LE->write_log($message , $level , $__php_errror);
}
}
library class logs.phjp
<?php if(!defined('__BASEPATH__')){exit('No Direct Access');}
define('__LOGS__' , __SYSTEM__ . 'logs' . DS);
class log
{
private $log_levels = array( 1 =>'ERROR' , 2 => 'DEBUG' ,3 => 'RESULT' , 4 => 'ALL');
private $log_enabled = FALSE;
private $log_date;
private $log_paths;
private $log_ready = FALSE;
public function __construct()
{
$this->log_enabled = get_item('log_enabled');
$this->log_date = get_item('date_format');
$this->log_path = get_item('log_path');
if($this->log_enabled === FALSE)
{
exit('Logging Is Not Currently Enabled');
}
if($this->log_ready === TRUE)
{
return;
}
elseif($this->log_path === null or empty($this->log_path))
{
$this->log_path = __LOGS__;
if(file_exists($this->log_path) && is_file_writeable($this->log_path) === TRUE)
{
$this->log_ready = TRUE;
}
}
elseif(!empty($this->log_path) && file_exists($this->log_path))
{
$this->log_ready = TRUE;
}
return(FALSE);
}
public function write_log($_message , $level = 0 , $_php_error = 0)
{
if($this->log_ready === FALSE)
{
exit('Logging Could Not Be Configured Correctly And Currently Is Not In Ready State');
}
if($this->log_ready === TRUE)
{
if(in_array($level , $this->log_levels))
{
$filepath = $this->log_path .'log - '.date('y-m-d') . EXT;
$mess = 'Logged : ['. date($this->log_date) .'] Level:['.$level.'] PHP Error:['. $_php_error .'] Message: '.$_message ."\n";
}
if(! $fp = @fopen($filepath , 'ab'))
{
return(FALSE);
}
@flock($fp , LOCK_EX);
@fwrite($fp , $mess);
@flock($fp , LOCK_UN);
@fclose($fp);
@chmod($this->log_paths , 0600);
return(TRUE);
}
return(FALSE);
}
}