Created basic loader, added minifier, misc improvements
This commit is contained in:
parent
3354364d5e
commit
e03cad02be
@ -1,5 +1,92 @@
|
||||
<?php
|
||||
|
||||
$config = array(
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Base Url
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This is the url path where the framework is located. Requires trailing
|
||||
| slash.
|
||||
|
|
||||
*/
|
||||
'base_url' => '',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Content Domain
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This is the domain used for serving content, such as css, javascript.
|
||||
|
|
||||
*/
|
||||
'content_domain' => '',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Static Lib Path
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This is the path where the 'assets' directory is on the static domain.
|
||||
|
|
||||
*/
|
||||
'static_lib_path' => $config['content_domain'].'assets/',
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Group Style Path
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This is the path that is used to determine the relative path to the
|
||||
| stylesheet minifier. This should not need to be changed.
|
||||
|
|
||||
*/
|
||||
'style_path' => $config['static_lib_path'] . '/css.php/g/';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Group Javascript Path
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| This is the path that is used to determine the relative path to the
|
||||
| javascript minifier. This should not need to be changed.
|
||||
|
|
||||
*/
|
||||
'script_path' => $config['static_lib_path'] . '/js.php/g/';
|
||||
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Default title
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Default title for webpages
|
||||
|
|
||||
*/
|
||||
|
||||
'default_title' => "Tim's Home Page",
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Default css group
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Default css group
|
||||
|
|
||||
*/
|
||||
|
||||
'default_css_group' => "css",
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Default js group
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Default js group
|
||||
|
|
||||
*/
|
||||
|
||||
'default_js_group' => "js",
|
||||
|
||||
);
|
||||
|
7
app/config/routes.php
Normal file
7
app/config/routes.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
$routes = array(
|
||||
// Default Paths
|
||||
'default_controller' => 'test',
|
||||
'default_module' => 'default',
|
||||
);
|
@ -11,7 +11,7 @@
|
||||
<p>Backtrace: </p>
|
||||
<?php foreach(debug_backtrace() as $error): ?>
|
||||
|
||||
<?php if(isset($error['file']) && ! stristr($error['file'], SYS_DIR)): ?>
|
||||
<?php if(isset($error['file']) && ! stristr($error['file'], SYS_PATH)): ?>
|
||||
<p style="margin-left:10px">
|
||||
File: <?php echo $error['file'] ?><br />
|
||||
Line: <?php echo $error['line'] ?><br />
|
||||
|
6
app/views/footer.php
Normal file
6
app/views/footer.php
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
<? if($foot_js != ""): ?>
|
||||
<?= $foot_js ?>
|
||||
<? endif ?>
|
||||
</body>
|
||||
</html>
|
11
app/views/header.php
Normal file
11
app/views/header.php
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
<head>
|
||||
<?= $meta ?>
|
||||
<?= $css ?>
|
||||
<?= $head_tags ?>
|
||||
<title><?= $title ?></title>
|
||||
<? if(!empty($base)) { ?><base href="<?=$base ?>" /><? } ?>
|
||||
<?= $head_tags ?>
|
||||
<?= $head_js ?>
|
||||
</head>
|
||||
<body<?= (!empty($body_class)) ? "class=\"" . $body_class . "\"" : ""; ?><?= (!empty($body_id)) ? " id=\"" . $body_id . "\"" : ""; ?>>
|
5
app/views/message.php
Normal file
5
app/views/message.php
Normal file
@ -0,0 +1,5 @@
|
||||
<section class="message <?= $stat_class ?>">
|
||||
<span class="icon"></span>
|
||||
<?= $message ?>
|
||||
<span class="close"></span>
|
||||
</section>
|
2086
assets/config/JSMinPlus.php
Executable file
2086
assets/config/JSMinPlus.php
Executable file
File diff suppressed because it is too large
Load Diff
56
assets/config/config.php
Executable file
56
assets/config/config.php
Executable file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Document Root
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The folder where the index of the website exists. In most situations,
|
||||
| this will not need to be changed.
|
||||
|
|
||||
| If the website is in a folder off of the domain name, like:
|
||||
| http://example.com/website/
|
||||
| you will need to add that folder to the document root.
|
||||
|
|
||||
*/
|
||||
$document_root = $_SERVER['DOCUMENT_ROOT'];
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| CSS Folder
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The folder where css files exist, in relation to the document root
|
||||
|
|
||||
*/
|
||||
$css_root = $document_root. '/css/';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Path from
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Path fragment to rewrite in css files
|
||||
|
|
||||
*/
|
||||
$path_from = '';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Path to
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The path fragment replacement for the css files
|
||||
|
|
||||
*/
|
||||
$path_to = '';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| JS Folder
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The folder where javascript files exist, in relation to the document root
|
||||
|
|
||||
*/
|
||||
$js_root = $document_root. '/js/';
|
15
assets/config/css_groups.php
Executable file
15
assets/config/css_groups.php
Executable file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
return array(
|
||||
/*-----
|
||||
Css
|
||||
-----*/
|
||||
|
||||
/*
|
||||
For each group create an array like so
|
||||
|
||||
'my_group' => array(
|
||||
'path/to/css/file1.css',
|
||||
'path/to/css/file2.css'
|
||||
),
|
||||
*/
|
||||
);
|
11
assets/config/js_groups.php
Executable file
11
assets/config/js_groups.php
Executable file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
return array(
|
||||
/*
|
||||
For each group create an array like so
|
||||
|
||||
'my_group' => array(
|
||||
'path/to/css/file1.css',
|
||||
'path/to/css/file2.css'
|
||||
),
|
||||
*/
|
||||
);
|
106
assets/css.php
Executable file
106
assets/css.php
Executable file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
//Get config files
|
||||
require('./config/config.php');
|
||||
|
||||
//Include the css groups
|
||||
$groups = require("./config/css_groups.php");
|
||||
|
||||
//The name of this file
|
||||
$this_file = 'css.php';
|
||||
|
||||
|
||||
//Function for compressing the CSS as tightly as possible
|
||||
function compress($buffer) {
|
||||
|
||||
//Remove CSS comments
|
||||
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
|
||||
|
||||
//Remove tabs, spaces, newlines, etc.
|
||||
$buffer = preg_replace('`\s+`', ' ', $buffer);
|
||||
$replace = array(
|
||||
' )' => ')',
|
||||
') ' => ')',
|
||||
' }' => '}',
|
||||
'} ' => '}',
|
||||
' {' => '{',
|
||||
'{ ' => '{',
|
||||
', ' => ',',
|
||||
': ' => ':',
|
||||
'; ' => ';',
|
||||
);
|
||||
|
||||
//Eradicate every last space!
|
||||
$buffer = trim(strtr($buffer, $replace));
|
||||
$buffer = str_replace('{ ', '{', $buffer);
|
||||
$buffer = str_replace('} ', '}', $buffer);
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
//Creative rewriting
|
||||
$pi = $_SERVER['PATH_INFO'];
|
||||
$pia = explode('/', $pi);
|
||||
|
||||
$pia_len = count($pia);
|
||||
$i = 1;
|
||||
|
||||
while($i < $pia_len)
|
||||
{
|
||||
$j = $i+1;
|
||||
$j = (isset($pia[$j])) ? $j : $i;
|
||||
|
||||
$_GET[$pia[$i]] = $pia[$j];
|
||||
|
||||
$i = $j + 1;
|
||||
};
|
||||
|
||||
$css = '';
|
||||
$modified = array();
|
||||
|
||||
if(isset($groups[$_GET['g']]))
|
||||
{
|
||||
foreach($groups[$_GET['g']] as $file)
|
||||
{
|
||||
$new_file = realpath($css_root.$file);
|
||||
$css .= file_get_contents($new_file);
|
||||
$modified[] = filemtime($new_file);
|
||||
}
|
||||
}
|
||||
|
||||
//Add this page too
|
||||
$modified[] = filemtime($this_file);
|
||||
|
||||
//Get the latest modified date
|
||||
rsort($modified);
|
||||
$last_modified = $modified[0];
|
||||
|
||||
if(!isset($_GET['debug']))
|
||||
{
|
||||
$css = compress($css);
|
||||
}
|
||||
|
||||
$css = strtr($css, $path_from, $path_to);
|
||||
|
||||
$requested_time=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
|
||||
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
||||
: time();
|
||||
|
||||
if($last_modified === $requested_time)
|
||||
{
|
||||
header("HTTP/1.1 304 Not Modified");
|
||||
exit();
|
||||
}
|
||||
|
||||
//This GZIPs the CSS for transmission to the user
|
||||
//making file size smaller and transfer rate quicker
|
||||
ob_start("ob_gzhandler");
|
||||
|
||||
header("Content-Type: text/css; charset=utf8");
|
||||
header("Cache-control: public, max-age=691200, must-revalidate");
|
||||
header("Last-Modified: ".gmdate('D, d M Y H:i:s', $last_modified)." GMT");
|
||||
header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($this_file) + 691200))." GMT");
|
||||
|
||||
echo $css;
|
||||
|
||||
ob_end_flush();
|
||||
//End of css.php
|
144
assets/js.php
Executable file
144
assets/js.php
Executable file
@ -0,0 +1,144 @@
|
||||
<?php
|
||||
//Get config files
|
||||
require('./config/config.php');
|
||||
require('./config/JSMinPlus.php');
|
||||
|
||||
//Include the js groups
|
||||
$groups_file = "./config/js_groups.php";
|
||||
$groups = require($groups_file);
|
||||
|
||||
//The name of this file
|
||||
$this_file = 'js.php';
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get Files
|
||||
*
|
||||
* Concatenates the javascript files for the current
|
||||
* group as a string
|
||||
* @return string
|
||||
*/
|
||||
function get_files()
|
||||
{
|
||||
global $groups, $js_root;
|
||||
|
||||
$js = '';
|
||||
|
||||
foreach($groups[$_GET['g']] as $file)
|
||||
{
|
||||
$new_file = realpath($js_root.$file);
|
||||
$js .= file_get_contents($new_file);
|
||||
}
|
||||
|
||||
return $js;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
//Creative rewriting of /g/groupname to ?g=groupname
|
||||
$pi = $_SERVER['PATH_INFO'];
|
||||
$pia = explode('/', $pi);
|
||||
|
||||
$pia_len = count($pia);
|
||||
$i = 1;
|
||||
|
||||
while($i < $pia_len)
|
||||
{
|
||||
$j = $i+1;
|
||||
$j = (isset($pia[$j])) ? $j : $i;
|
||||
|
||||
$_GET[$pia[$i]] = $pia[$j];
|
||||
|
||||
$i = $j + 1;
|
||||
};
|
||||
|
||||
$js = '';
|
||||
$modified = array();
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
//Aggregate the last modified times of the files
|
||||
if(isset($groups[$_GET['g']]))
|
||||
{
|
||||
$cache_file = $js_root.'/cache/'.$_GET['g'];
|
||||
|
||||
foreach($groups[$_GET['g']] as $file)
|
||||
{
|
||||
$new_file = realpath($js_root.$file);
|
||||
$modified[] = filemtime($new_file);
|
||||
}
|
||||
|
||||
//Add this page too, as well as the groups file
|
||||
$modified[] = filemtime($this_file);
|
||||
$modified[] = filemtime($groups_file);
|
||||
|
||||
$cache_modified = 0;
|
||||
|
||||
//Add the cache file
|
||||
if(is_file($cache_file))
|
||||
{
|
||||
$cache_modified = filemtime($cache_file);
|
||||
}
|
||||
}
|
||||
else //Nothing to display? Just exit
|
||||
{
|
||||
die("You must specify a group that exists");
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
//Get the latest modified date
|
||||
rsort($modified);
|
||||
$last_modified = $modified[0];
|
||||
|
||||
$requested_time=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
|
||||
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
||||
: time();
|
||||
|
||||
// If the browser's cached version is up to date,
|
||||
// don't resend the file
|
||||
if($last_modified === $requested_time)
|
||||
{
|
||||
header("HTTP/1.1 304 Not Modified");
|
||||
exit();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
//Determine what to do: rebuild cache, send files as is, or send cache.
|
||||
if($cache_modified < $last_modified)
|
||||
{
|
||||
$js = JSMinPlus::minify(get_files());
|
||||
$cs = file_put_contents($cache_file, $js);
|
||||
|
||||
//Make sure cache file gets created/updated
|
||||
if($cs === FALSE)
|
||||
{
|
||||
die("Cache file was not created. Make sure you have the correct folder permissions.");
|
||||
}
|
||||
}
|
||||
else if(isset($_GET['debug']))
|
||||
{
|
||||
$js = get_files();
|
||||
}
|
||||
else
|
||||
{
|
||||
$js = file_get_contents($cache_file);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
//This GZIPs the js for transmission to the user
|
||||
//making file size smaller and transfer rate quicker
|
||||
ob_start("ob_gzhandler");
|
||||
|
||||
header("Content-Type: application/javascript; charset=utf8");
|
||||
header("Cache-control: public, max-age=691200, must-revalidate");
|
||||
header("Last-Modified: ".gmdate('D, d M Y H:i:s', $last_modified)." GMT");
|
||||
header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($this_file) + 691200))." GMT");
|
||||
|
||||
echo $js;
|
||||
|
||||
ob_end_flush();
|
||||
//end of js.php
|
@ -20,11 +20,8 @@ define('MOD_PATH', __DIR__.'/modules/');
|
||||
define('APP_PATH', __DIR__.'/app/');
|
||||
|
||||
// Require the most important files
|
||||
require(SYS_PATH . "common.php");
|
||||
require(SYS_PATH . "miniMVC.php");
|
||||
require(SYS_PATH . 'db.php');
|
||||
|
||||
$MM =& get_instance();
|
||||
|
||||
$MM->db = new MM_db('pgsql');
|
||||
|
||||
echo $MM->__toString();
|
||||
echo get_instance()->__toString();
|
||||
|
173
sys/common.php
173
sys/common.php
@ -23,11 +23,44 @@ function get_instance($params=array())
|
||||
* Function to search through the tree to find the necessary file
|
||||
*
|
||||
* @param string $file
|
||||
* @return string
|
||||
* @param string $module
|
||||
* @param string $curr_path
|
||||
* @return void
|
||||
*/
|
||||
function _find_file($file)
|
||||
function load_file($file, $curr_path="", $module="")
|
||||
{
|
||||
|
||||
if($curr_path === "app")
|
||||
{
|
||||
$path = APP_PATH."{$file}.php";
|
||||
}
|
||||
else if($curr_path === "sys")
|
||||
{
|
||||
$path = SYS_PATH."{$file}.php";
|
||||
}
|
||||
|
||||
if($module !== "")
|
||||
{
|
||||
$path = MOD_PATH."{$module}/{$file}.php";
|
||||
}
|
||||
else
|
||||
{
|
||||
if($curr_path !== "")
|
||||
{
|
||||
$matches = array();
|
||||
if(preg_match("`modules/([a-z 0-9~%.:_\-])/?`i", $curr_path, $matches))
|
||||
{
|
||||
$module = $matches[1];
|
||||
|
||||
$path = MOD_PATH."{$module}/{$file}.php";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$path = MOD_PATH."default/{$file}.php";
|
||||
}
|
||||
}
|
||||
|
||||
require_once($path);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -45,16 +78,51 @@ function load_view($file, $data=array(), $return=FALSE)
|
||||
// Extract the data array
|
||||
extract($data);
|
||||
|
||||
//
|
||||
// Include the file
|
||||
_find_file($file);
|
||||
|
||||
$buffer = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
if($return)
|
||||
{
|
||||
return $buffer;
|
||||
}
|
||||
|
||||
echo $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom error handler
|
||||
*/
|
||||
function on_error($num, $string, $file, $line, $context)
|
||||
function on_error($severity, $message, $filepath, $line, $context)
|
||||
{
|
||||
$data = array(
|
||||
$levels = array(
|
||||
E_ERROR => 'Error',
|
||||
E_WARNING => 'Warning',
|
||||
E_PARSE => 'Parsing Error',
|
||||
E_NOTICE => 'Notice',
|
||||
E_CORE_ERROR => 'Core Error',
|
||||
E_CORE_WARNING => 'Core Warning',
|
||||
E_COMPILE_ERROR => 'Compile Error',
|
||||
E_COMPILE_WARNING => 'Compile Warning',
|
||||
E_USER_ERROR => 'User Error',
|
||||
E_USER_WARNING => 'User Warning',
|
||||
E_USER_NOTICE => 'User Notice',
|
||||
E_STRICT => 'Runtime Notice'
|
||||
);
|
||||
|
||||
$severity = (isset($levels[$severity])) ? $levels[$severity] : $severity;
|
||||
|
||||
// Contain the content for buffering
|
||||
ob_start();
|
||||
|
||||
// Extract the data array
|
||||
include(APP_PATH.'/errors/error.php');
|
||||
|
||||
$buffer = ob_get_contents();
|
||||
ob_end_clean();
|
||||
echo $buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -104,4 +172,95 @@ function on_exception($exception)
|
||||
echo $msg;
|
||||
}
|
||||
|
||||
set_exception_handler('on_exception');
|
||||
//Set error handlers
|
||||
set_error_handler('on_error');
|
||||
set_exception_handler('on_exception');
|
||||
|
||||
/**
|
||||
* JSObject
|
||||
*
|
||||
* Class for creating object-literal-like constructs in PHP
|
||||
*/
|
||||
class JSObject {
|
||||
|
||||
/**
|
||||
* Constructor for creating the objects
|
||||
*/
|
||||
function __construct()
|
||||
{
|
||||
$args = func_get_args();
|
||||
|
||||
$members = $args[0];
|
||||
|
||||
// Add the passed parameters to the object
|
||||
foreach($members as $name => $value)
|
||||
{
|
||||
if(is_array($value))
|
||||
{
|
||||
$value = new JSObject($value);
|
||||
}
|
||||
|
||||
$this->$name = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* PHP magic method to facilitate dynamic methods
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $args
|
||||
*/
|
||||
function __call($name, $args)
|
||||
{
|
||||
if(is_callable($this->$name))
|
||||
{
|
||||
//Call the dynamic function
|
||||
return call_user_func_array($this->$name, $args);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prints out the contents of the object when used as a string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function __toString()
|
||||
{
|
||||
$args = func_get_args();
|
||||
$method = ( ! empty($args)) ? $args[0] : "print_r";
|
||||
|
||||
$output = '<pre>';
|
||||
|
||||
if($method == "var_dump")
|
||||
{
|
||||
ob_start();
|
||||
var_dump($this);
|
||||
$output .= ob_get_contents();
|
||||
ob_end_clean();
|
||||
}
|
||||
else if($method == "var_export")
|
||||
{
|
||||
ob_start();
|
||||
var_export($this);
|
||||
$output .= ob_get_contents();
|
||||
ob_end_clean();
|
||||
}
|
||||
else
|
||||
{
|
||||
$output .= print_r($this, TRUE);
|
||||
}
|
||||
|
||||
return $output . '</pre>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor without the "new"
|
||||
*
|
||||
* @param array $members
|
||||
*/
|
||||
function __invoke($members=array())
|
||||
{
|
||||
return new JSObject($members);
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
//Include the database config file
|
||||
require(APP_PATH.'config/db.php');
|
||||
load_file('config/db','app');
|
||||
|
||||
/**
|
||||
* Just for giggles extend PHP's PDO class
|
||||
|
112
sys/miniMVC.php
112
sys/miniMVC.php
@ -1,102 +1,13 @@
|
||||
<?php
|
||||
|
||||
//Include common function
|
||||
require('common.php');
|
||||
|
||||
/**
|
||||
* JSObject
|
||||
*
|
||||
* Class for creating object-literal-like constructs in PHP
|
||||
*/
|
||||
class JSObject {
|
||||
|
||||
/**
|
||||
* Constructor for creating the objects
|
||||
*/
|
||||
function __construct()
|
||||
{
|
||||
$args = func_get_args();
|
||||
|
||||
$members = $args[0];
|
||||
|
||||
// Add the passed parameters to the object
|
||||
foreach($members as $name => $value)
|
||||
{
|
||||
if(is_array($value))
|
||||
{
|
||||
$value = new JSObject($value);
|
||||
}
|
||||
|
||||
$this->$name = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* PHP magic method to facilitate dynamic methods
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $args
|
||||
*/
|
||||
function __call($name, $args)
|
||||
{
|
||||
if(is_callable($this->$name))
|
||||
{
|
||||
//Call the dynamic function
|
||||
return call_user_func_array($this->$name, $args);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Prints out the contents of the object when used as a string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function __toString()
|
||||
{
|
||||
$args = func_get_args();
|
||||
$method = ( ! empty($args)) ? $args[0] : "print_r";
|
||||
|
||||
$output = '<pre>';
|
||||
|
||||
if($method == "var_dump")
|
||||
{
|
||||
ob_start();
|
||||
var_dump($this);
|
||||
$output .= ob_get_contents();
|
||||
ob_end_clean();
|
||||
}
|
||||
else if($method == "var_export")
|
||||
{
|
||||
ob_start();
|
||||
var_export($this);
|
||||
$output .= ob_get_contents();
|
||||
ob_end_clean();
|
||||
}
|
||||
else
|
||||
{
|
||||
$output .= print_r($this, TRUE);
|
||||
}
|
||||
|
||||
return $output . '</pre>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor without the "new"
|
||||
*
|
||||
* @param array $members
|
||||
*/
|
||||
function __invoke($members=array())
|
||||
{
|
||||
return new JSObject($members);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base class for the framework
|
||||
*/
|
||||
class miniMVC extends JSObject {
|
||||
|
||||
protected $buffer;
|
||||
protected $headers;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->buffer = "";
|
||||
@ -129,10 +40,16 @@ class miniMVC extends JSObject {
|
||||
// Set headers
|
||||
foreach($this->headers as $key => $val)
|
||||
{
|
||||
@header("$key: $val");
|
||||
if( ! isset($val))
|
||||
{
|
||||
@header($key);
|
||||
}
|
||||
else
|
||||
{
|
||||
@header("$key: $val");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -145,15 +62,12 @@ class miniMVC extends JSObject {
|
||||
$path = SYS_PATH."{$name}.php";
|
||||
$class = "MM_{$key}";
|
||||
|
||||
//if(is_file($path))
|
||||
//{
|
||||
include($path);
|
||||
load_file($name, 'sys');
|
||||
|
||||
if(class_exists($class, FALSE))
|
||||
{
|
||||
$this->$name = new $class();
|
||||
}
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
516
sys/page.php
Normal file
516
sys/page.php
Normal file
@ -0,0 +1,516 @@
|
||||
<?php
|
||||
(defined('BASEPATH')) OR exit('No direct script access allowed');
|
||||
/**
|
||||
* Class for building pages
|
||||
*
|
||||
* All methods are chainable, with the exception of the constructor,
|
||||
* build_header(), build_footer(), build_page() and _headers() methods.
|
||||
*/
|
||||
class Page extends JSObject
|
||||
{
|
||||
private static $meta, $head_js, $foot_js, $css, $title, $head_tags, $body_class, $body_id, $base;
|
||||
private $CI;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->meta = "";
|
||||
$this->head_js = "";
|
||||
$this->foot_js = "";
|
||||
$this->css = "";
|
||||
$this->title = "";
|
||||
$this->head_tags = "";
|
||||
$this->body_class = "";
|
||||
$this->body_id = "";
|
||||
$this->base = "";
|
||||
$this->MM =& get_instance();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets server headers and doctype
|
||||
*
|
||||
* Also sets page mime type, based on if sent as
|
||||
* html or xhtml, and what the target browser
|
||||
* supports
|
||||
*
|
||||
* @param bool $xhtml
|
||||
* @param bool $html5
|
||||
* @return Page
|
||||
*/
|
||||
private function _headers($xhtml, $html5)
|
||||
{
|
||||
$this->MM->output->set_header("Cache-Control", "must-revalidate, public");
|
||||
|
||||
$this->MM->output->set_header("Vary", "Accept");
|
||||
$mime = "";
|
||||
|
||||
//Variable for accept keyword
|
||||
$accept = (!empty($_SERVER['HTTP_ACCEPT'])) ? $_SERVER['HTTP_ACCEPT'] : "";
|
||||
|
||||
//Predefine doctype
|
||||
$doctype_string = ($html5 == TRUE) ? '<!DOCTYPE html>' : '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">';
|
||||
|
||||
//Predefine charset
|
||||
$charset = "UTF-8";
|
||||
|
||||
//If xhtml flag is false, set html5 header
|
||||
if ($xhtml == TRUE)
|
||||
{
|
||||
//Check that the user agent accepts application/xhtml+xml, or if it's the W3C Validator
|
||||
if (stristr($accept, "application/xhtml+xml") || stristr($_SERVER["HTTP_USER_AGENT"], "W3C_Validator"))
|
||||
{
|
||||
$mime = "application/xhtml+xml";
|
||||
}
|
||||
//Or if it supports application/xml
|
||||
else if (stristr($accept, "application/xml"))
|
||||
{
|
||||
$mime = "application/xml";
|
||||
}
|
||||
//Or if it supports text/xml
|
||||
else if (stristr($accept, "text/xml"))
|
||||
{
|
||||
$mime = "text/xml";
|
||||
}
|
||||
else //Otherwise, it's tag soup
|
||||
{
|
||||
$mime = "text/html";
|
||||
|
||||
if ($html5 == FALSE) //If it's not HTML5, it's HTML4
|
||||
{
|
||||
$doctype_string = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">';
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$mime = "text/html";
|
||||
|
||||
if ($html5 == FALSE)
|
||||
{
|
||||
$doctype_string = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">';
|
||||
}
|
||||
}
|
||||
|
||||
// set the doctype according to the mime type which was determined
|
||||
if ($mime == "application/xhtml+xml" || $mime == "text/xml" || $mime == "application/xml")
|
||||
{
|
||||
if ($html5 == TRUE)
|
||||
{
|
||||
$doctype_string = '';
|
||||
}
|
||||
|
||||
$doctype_string = "<?xml version='1.0' encoding='$charset' ?>\n"
|
||||
. $doctype_string
|
||||
. "\n<html xmlns='http://www.w3.org/1999/xhtml'" . " xml:lang='en'>";
|
||||
}
|
||||
else
|
||||
{
|
||||
$doctype_string .= "\n<html lang='en'>";
|
||||
}
|
||||
|
||||
// finally, output the mime type and prolog type
|
||||
$this->MM->output->set_header("Content-Type", "$mime;charset=$charset");
|
||||
$this->MM->output->set_header("X-UA-Compatible", "chrome=1, IE=edge");
|
||||
$this->MM->output->set_output($doctype_string);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Set Meta
|
||||
*
|
||||
* Sets meta tags, with codeigniter native meta tag helper
|
||||
*
|
||||
* @param array $meta
|
||||
* @return Page
|
||||
*/
|
||||
public function set_meta($meta)
|
||||
{
|
||||
$this->meta .= $this->_meta($meta);
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets minified javascript group in header
|
||||
* @param string $group
|
||||
* @param bool $debug
|
||||
* @return Page
|
||||
*/
|
||||
public function set_head_js_group($group, $debug = FALSE)
|
||||
{
|
||||
if ($group === FALSE)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
$file = $this->MM->config->item('group_js_path') . $group;
|
||||
$file .= ($debug == TRUE) ? "/debug/1" : "";
|
||||
$this->head_js .= $this->script_tag($file, FALSE);
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Set an individual js file in header
|
||||
* @param string $js
|
||||
* @param bool $domain
|
||||
* @return Page
|
||||
*/
|
||||
public function set_head_js($js, $domain = TRUE)
|
||||
{
|
||||
$this->head_js .= $this->script_tag($js, $domain);
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets a minified css group
|
||||
* @param string $group
|
||||
* @return Page
|
||||
*/
|
||||
public function set_css_group($group)
|
||||
{
|
||||
$link = array(
|
||||
'href' => $this->MM->config->item('group_style_path') . $group,
|
||||
'rel' => 'stylesheet',
|
||||
'type' => 'text/css'
|
||||
);
|
||||
$this->css .= link_tag($link);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets a minified javascript group for the page footer
|
||||
* @param string $group
|
||||
* @return Page
|
||||
*/
|
||||
public function set_foot_js_group($group, $debug = FALSE)
|
||||
{
|
||||
$file = $this->MM->config->item('group_js_path') . $group;
|
||||
$file .= ($debug == TRUE) ? "/debug/1" : "";
|
||||
$this->foot_js .= $this->script_tag($file, FALSE);
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets js in footer; multiple files are combined and minified.
|
||||
* @param array $args
|
||||
* @return Page
|
||||
*/
|
||||
public function set_foot_js($js, $domain)
|
||||
{
|
||||
$this->foot_js .= $this->script_tag($js, $domain);
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets html title string
|
||||
* @param string $title
|
||||
* @return Page
|
||||
*/
|
||||
public function set_title($title = "")
|
||||
{
|
||||
$title = ($title == "") ? $this->MM->config->item('default_title') : $title;
|
||||
|
||||
$this->title = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets custom body class
|
||||
* @param string $class
|
||||
* @return Page
|
||||
*/
|
||||
public function set_body_class($class = "")
|
||||
{
|
||||
$this->body_class = $class;
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets custom body id
|
||||
* @param string $id
|
||||
* @return Page
|
||||
*/
|
||||
public function set_body_id($id = "")
|
||||
{
|
||||
$this->body_id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets custom base href
|
||||
* @param string href
|
||||
* @return Page
|
||||
*/
|
||||
public function set_base($href)
|
||||
{
|
||||
$this->base = $href;
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets custom css tags
|
||||
* @param string $name
|
||||
* @param string $media
|
||||
* @return Page
|
||||
*/
|
||||
public function set_css_tag($name, $domain = TRUE, $media = "all")
|
||||
{
|
||||
$path = $this->MM->config->item('content_domain');
|
||||
$css_file = $path . "/css/" . $name . ".css";
|
||||
|
||||
if ($domain == FALSE)
|
||||
$css_file = $name;
|
||||
|
||||
$this->css_tags .= link_tag($name, "stylesheet", "text/css", "", $media);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets uncompressed js file in footer
|
||||
* @param string $name
|
||||
* @param bool $domain
|
||||
* @return Page
|
||||
*/
|
||||
public function set_foot_js_tag($name, $domain = TRUE)
|
||||
{
|
||||
$this->foot_js .= $this->script_tag($name, $domain);
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets a custom tag in the header
|
||||
* @param string $tag
|
||||
* @return Page
|
||||
*/
|
||||
public function set_head_tag($tag)
|
||||
{
|
||||
$this->head_tags .= $tag . "\n";
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Sets custom page header
|
||||
* @param mixed $xhtml
|
||||
* @param bool $html5
|
||||
* @param bool $fbml
|
||||
* @return $this
|
||||
*/
|
||||
public function build_header($xhtml = FALSE, $html5 = TRUE)
|
||||
{
|
||||
$data = array();
|
||||
|
||||
//Set Meta Tags
|
||||
$this->meta = ($html5 == TRUE)
|
||||
? '<meta charset="utf-8" />'. $this->meta
|
||||
: meta('content-type', 'text/html; charset=utf-8', 'equiv') . $this->meta;
|
||||
|
||||
$data['meta'] = $this->meta;
|
||||
|
||||
//Set CSS
|
||||
if ($this->css != "")
|
||||
{
|
||||
$data['css'] = $this->css;
|
||||
}
|
||||
else
|
||||
{
|
||||
//Set default CSS group
|
||||
$this->set_css_group($this->MM->config->item('default_css_group'));
|
||||
$data['css'] = $this->css;
|
||||
}
|
||||
|
||||
//Set head javascript
|
||||
if ($this->head_js != "")
|
||||
{
|
||||
$data['head_js'] = $this->head_js;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->set_head_js_group($this->MM->config->item('default_head_js_group'));
|
||||
$data['head_js'] = $this->head_js;
|
||||
}
|
||||
|
||||
//Set Page Title
|
||||
$data['title'] = ($this->title != '') ? $this->title : $this->MM->config->item('default_title');
|
||||
|
||||
//Set Body Class
|
||||
$data['body_class'] = $this->body_class;
|
||||
|
||||
//Set Body Id
|
||||
$data['body_id'] = $this->body_id;
|
||||
|
||||
//Set Base HREF
|
||||
$data['base'] = $this->base;
|
||||
|
||||
//Set individual head tags
|
||||
$data['head_tags'] = $this->head_tags;
|
||||
|
||||
//Set Server Headers and Doctype
|
||||
$this->_headers($xhtml, $html5);
|
||||
|
||||
//Output Header
|
||||
$this->MM->load->view('header', $data);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
|
||||
/**
|
||||
* Builds common footer with any additional js
|
||||
*/
|
||||
public function build_footer()
|
||||
{
|
||||
$data = array();
|
||||
|
||||
$data['foot_js'] = ($this->foot_js != "") ? $this->foot_js : '';
|
||||
|
||||
$this->MM->load->view('footer', $data);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Script Tag
|
||||
*
|
||||
* Helper function for making script tags
|
||||
*
|
||||
* @param string $js
|
||||
* @param bool $domain
|
||||
* @return string
|
||||
*/
|
||||
private function script_tag($js, $domain = TRUE)
|
||||
{
|
||||
$path = $this->MM->config->item('content_domain');
|
||||
$js_file = $path . "/js/" . $js . ".js";
|
||||
|
||||
if ($domain == FALSE)
|
||||
$js_file = $js;
|
||||
|
||||
$tag = '<script src="' . $js_file . '"></script>';
|
||||
|
||||
return $tag;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Quick Build
|
||||
*
|
||||
* A function to make building pages faster
|
||||
* @param mixed $view
|
||||
* @param mixed $data
|
||||
* @param bool $xhtml
|
||||
* @param bool $html5
|
||||
*/
|
||||
public function quick_build($view, $data, $xhtml = TRUE, $html5 = TRUE)
|
||||
{
|
||||
//Set up header
|
||||
if ($title != '')
|
||||
{
|
||||
$this->set_title($title);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->set_title($this->MM->config->item('default_title'));
|
||||
}
|
||||
|
||||
$this->build_header($xhtml, $html5);
|
||||
|
||||
//Load view(s)
|
||||
if (is_array($view))
|
||||
{
|
||||
foreach ($view as $v)
|
||||
{
|
||||
$this->MM->load->view($v, $data);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->MM->load->view($view, $data);
|
||||
}
|
||||
|
||||
//Create footer
|
||||
$this->build_footer();
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Set Message
|
||||
*
|
||||
* Adds a message to the page
|
||||
* @param string $type
|
||||
* @param string $message
|
||||
* @return void
|
||||
*/
|
||||
public function set_message($type, $message)
|
||||
{
|
||||
$data['stat_class'] = $type;
|
||||
$data['message'] = $message;
|
||||
|
||||
$this->MM->load->view('message', $data);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Redirect 303
|
||||
*
|
||||
* Shortcut function for 303 redirect
|
||||
* @param string $url
|
||||
*/
|
||||
function redirect_303($url)
|
||||
{
|
||||
$this->MM->output->set_header("HTTP/1.1 303 See Other");
|
||||
$this->MM->output->set_header("Location:" . $url);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
private function _meta($params)
|
||||
{
|
||||
$string = "<meta ";
|
||||
|
||||
foreach($params as $k => $v)
|
||||
{
|
||||
$string .= $k.'="'.$v.'" ';
|
||||
}
|
||||
|
||||
$string .= " />";
|
||||
|
||||
return $string;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user