miniMVC/sys/common.php

504 lines
10 KiB
PHP
Raw Permalink 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
2012-05-23 12:01:13 -04:00
* @link https://github.com/aviat4ion/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license
2012-01-13 11:58:06 -05:00
*/
// --------------------------------------------------------------------------
/**
* File including common framework-wide functions
*
* @package miniMVC
* @subpackage System
2012-01-13 11:58:06 -05:00
*/
2012-05-22 11:11:36 -04:00
namespace miniMVC;
2012-01-13 11:58:06 -05:00
// --------------------------------------------------------------------------
2012-05-21 14:49:33 -04:00
// ! Autoloading
2012-05-18 08:04:01 -04:00
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
2012-05-21 14:29:36 -04:00
/**
2012-05-21 14:49:33 -04:00
* Function to autoload system libraries
*
* @param string
*/
function _autoload($name)
2012-05-22 11:11:36 -04:00
{
if ($name == '') return;
// strip off namespaces - they all go to the same folder
$names = explode('\\', trim($name));
$name = end($names);
// Paths to load from
$sys_path = MM_SYS_PATH . "core/{$name}.php";
$lib_path = MM_SYS_PATH . "libraries/{$name}.php";
$class_path = MM_APP_PATH . "classes/{$name}.php";
if (is_file($sys_path))
2012-05-21 14:49:33 -04:00
{
require_once($sys_path);
2012-05-21 14:49:33 -04:00
}
elseif (is_file($lib_path))
2012-05-21 14:29:36 -04:00
{
require_once($lib_path);
}
if (is_file($class_path))
{
require_once($class_path);
2012-05-21 14:29:36 -04:00
}
}
// Start the autoloader
spl_autoload_register('miniMVC\_autoload');
2012-05-22 11:11:36 -04:00
2012-05-21 14:49:33 -04:00
// --------------------------------------------------------------------------
// ! Error handling / messages
2012-05-21 14:29:36 -04:00
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
* Function to run on script shutdown
* -used to catch most fatal errors, and
2012-01-13 11:58:06 -05:00
* display them cleanly
2012-05-01 15:22:29 -04:00
*
* @return void
2012-01-13 11:58:06 -05:00
*/
function shutdown()
{
// Catch the last error
$error = error_get_last();
// types of errors that are fatal
$fatal = array(E_ERROR, E_PARSE, E_RECOVERABLE_ERROR);
2012-01-13 11:58:06 -05:00
// Display pretty error page
2012-05-03 13:26:09 -04:00
if (in_array($error['type'], $fatal))
2012-01-13 11:58:06 -05:00
{
$file = str_replace(MM_BASE_PATH, "", $error['file']);
2012-05-18 08:04:01 -04:00
$err_msg = "<h2>Fatal Error: </h2>
{$error['message']}<br /><br />
<strong>File:</strong> {$file}<br /><br />
<strong>Line:</strong> {$error['line']}";
2012-01-13 11:58:06 -05:00
show_error($err_msg);
}
}
// --------------------------------------------------------------------------
/**
* Custom error handler
*
* @param int $severity
* @param string $message
* @param string $filepath
* @param int $line
2012-04-27 16:28:25 -04:00
* @return ErrorException
2012-01-13 11:58:06 -05:00
*/
2012-04-27 16:28:25 -04:00
function on_error($severity, $message, $filepath, $line)
2012-01-13 11:58:06 -05:00
{
2012-05-22 11:11:36 -04:00
throw new \ErrorException($message, 0, $severity, $filepath, $line);
2012-01-13 11:58:06 -05:00
}
// --------------------------------------------------------------------------
/**
* Custom exception handlererror_get_last
*
* @param Exception $exception
* @return void
2012-01-13 11:58:06 -05:00
*/
function on_exception($exception)
{
// This is passed to the error template
2012-01-13 11:58:06 -05:00
$message = $exception->getMessage();
2012-01-13 11:58:06 -05:00
// Contain the content for buffering
ob_start();
2012-05-18 08:04:01 -04:00
include(MM_APP_PATH . '/views/errors/error_php_exception.php');
2012-01-13 11:58:06 -05:00
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
}
// --------------------------------------------------------------------------
/**
* General 404 function
2012-05-01 15:22:29 -04:00
*
* @return void
2012-01-13 11:58:06 -05:00
*/
function show_404()
{
@header('HTTP/1.1 404 Not Found', TRUE, 404);
// Contain the content for buffering
ob_start();
// This is passed to the error template
$message = '404 Not Found';
2012-05-18 08:04:01 -04:00
include(MM_APP_PATH . '/views/errors/error_404.php');
$buffer = ob_get_contents();
ob_end_clean();
die($buffer);
2012-01-13 11:58:06 -05:00
}
// --------------------------------------------------------------------------
/**
* Fatal Error page function
*
2012-01-13 11:58:06 -05:00
* @param string $message
* @param int $status_code
*/
function show_error($message, $status_code=null)
{
2012-05-03 13:26:09 -04:00
if ( ! is_null($status_code))
2012-01-13 11:58:06 -05:00
{
2012-05-15 16:53:10 -04:00
@header("HTTP/1.1 {$status_code}", TRUE, (int) $status_code);
2012-01-13 11:58:06 -05:00
}
// Contain the content for buffering
ob_start();
2012-05-18 08:04:01 -04:00
include(MM_APP_PATH . '/views/errors/error_general.php');
2012-01-13 11:58:06 -05:00
$buffer = ob_get_contents();
ob_end_clean();
die($buffer);
}
2012-05-18 08:04:01 -04:00
// --------------------------------------------------------------------------
// ! Utility Functions
// --------------------------------------------------------------------------
/**
* Utility function to check if a variable is set, and is an array or object
*
* @param mixed $var
* @return bool
*/
function is_like_array(&$var)
{
if ( ! isset($var))
{
return FALSE;
}
return (is_array($var) OR is_object($var)) && ( ! empty($var));
}
2012-01-13 11:58:06 -05:00
// --------------------------------------------------------------------------
/**
* Returns routable methods for the specified controller class
*
* @param string $controller
* @return array
*/
function controller_methods($controller)
{
2012-05-22 11:11:36 -04:00
$methods = \get_class_methods($controller);
2012-05-01 15:22:29 -04:00
// Eliminate methods from Controller and Model classes
2012-05-22 11:11:36 -04:00
$skip_methods = array_merge(\get_class_methods('miniMVC\Controller'), \get_class_methods('miniMVC\Model'));
2012-05-01 15:22:29 -04:00
$methods = array_diff($methods, $skip_methods);
2012-01-13 11:58:06 -05:00
return $methods;
}
// --------------------------------------------------------------------------
2012-05-18 08:04:01 -04:00
/**
* Returns a full url from a url segment
*
* @param string $segment
* @return string
*/
function site_url($segment)
{
return $url = BASE_URL . URL_INDEX_FILE . $segment;
2012-05-18 08:04:01 -04:00
}
// --------------------------------------------------------------------------
/**
* Prints out the contents of the object
*
2012-05-18 08:04:01 -04:00
* @param object/array $data
* @param string $method
* @return string
*/
function to_string($data, $method='print_r')
{
$output = '<pre>';
2012-05-18 08:04:01 -04:00
if ($method == "var_dump")
{
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-18 08:04:01 -04:00
{
ob_start();
var_export($data);
$output .= ob_get_contents();
ob_end_clean();
}
2012-05-18 08:04:01 -04:00
else
{
$output .= print_r($data, TRUE);
}
return $output . '</pre>';
}
// --------------------------------------------------------------------------
2012-05-18 16:20:47 -04:00
if ( ! function_exists('do_include'))
2012-05-18 08:04:01 -04:00
{
2012-05-18 16:20:47 -04:00
/**
* Array_map callback to load a folder of classes at once
*
* @param string $path
* @return void
*/
function do_include($path)
{
require_once($path);
}
2012-05-18 08:04:01 -04:00
}
// --------------------------------------------------------------------------
// ! Bootstrap functions
// --------------------------------------------------------------------------
/**
* Load required classes for bootstraping
*
* @return void
*/
function init()
{
// Catch fatal errors, don't show them
if (function_exists('error_get_last'))
{
register_shutdown_function('miniMVC\shutdown');
}
2012-05-18 08:04:01 -04:00
//Set error handlers
2012-05-22 11:11:36 -04:00
set_error_handler('miniMVC\on_error');
set_exception_handler('miniMVC\on_exception');
2012-05-18 08:04:01 -04:00
// Load Database classes
require_once(MM_SYS_PATH . 'db/autoload.php');
2012-05-18 08:04:01 -04:00
// Map to the appropriate module/controller/function
route();
}
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
* Returns the last segment of the current url
2012-05-01 15:22:29 -04:00
*
* @return string
2012-01-13 11:58:06 -05:00
*/
function get_last_segment()
{
$array = get_segments();
return end($array);
}
// --------------------------------------------------------------------------
/**
* Gets an array of the segments of the current url
*
* @return array
*/
function get_segments()
2012-01-13 11:58:06 -05:00
{
2012-05-01 15:22:29 -04:00
$sn = $_SERVER['SCRIPT_NAME'];
$ru = $_SERVER['REQUEST_URI'];
// Get the equivalent to path info
$pi = (isset($_SERVER['PATH_INFO']))
? str_replace($sn, '', $ru)
2012-05-01 09:39:49 -04:00
: '/';
// Correct for being in a sub-directory
if (strlen($sn) > strlen($ru))
{
$pi = '/';
}
return explode('/', $pi);
}
// --------------------------------------------------------------------------
/**
* Calls the appropriate module/controller/function based on the url
*
* @return void
*/
function route()
{
// Get the path info
$pi = $_SERVER['PATH_INFO'];
$ru = $_SERVER['REQUEST_URI'];
$sn = $_SERVER['SCRIPT_NAME'];
2012-05-01 15:22:29 -04:00
// Make sure the home page works when in a sub_directory
if (strlen($sn) > strlen($ru))
{
$pi = '/';
}
2012-01-13 11:58:06 -05:00
// Load the routes config file
2012-05-18 08:04:01 -04:00
$routes = require_once(MM_APP_PATH . 'config/routes.php');
2012-01-13 11:58:06 -05:00
// Set the default route
$module = $routes['default_module'];
$controller = $routes['default_controller'];
$func = "index";
$route_set = FALSE;
2012-05-15 16:53:10 -04:00
// If it isn't the index page
2012-05-03 13:26:09 -04:00
if ( ! empty($pi) && $pi !== "/")
2012-01-13 11:58:06 -05:00
{
//Remove trailing slash and begining slash
$pi = trim($pi, '/');
$segments = explode("/", $pi);
2012-01-13 11:58:06 -05:00
// URL matches the route exactly? Cool, that was easy
2012-05-03 13:26:09 -04:00
if (isset($routes[$pi]))
2012-01-13 11:58:06 -05:00
{
list($module, $controller, $func) = explode("/", $routes[$pi]);
run($module, $controller, $func);
return;
2012-01-13 11:58:06 -05:00
}
else
{
$custom_routes = $routes;
2012-01-13 11:58:06 -05:00
// Skip required routes
unset($custom_routes['default_module']);
unset($custom_routes['default_controller']);
unset($custom_routes['404_handler']);
foreach($custom_routes as $uri => $map)
2012-01-13 11:58:06 -05:00
{
2012-05-03 13:26:09 -04:00
if (preg_match("`{$uri}`i", $pi))
2012-01-13 11:58:06 -05:00
{
list($module, $controller, $func) = explode("/", $map);
run($module, $controller, $func);
return;
2012-01-13 11:58:06 -05:00
}
}
}
// Doesn't match a predefined route?
2012-05-15 16:53:10 -04:00
// Match on module/controller/method, module/controller, controller/method, or method
2012-05-03 13:26:09 -04:00
if ( ! $route_set)
2012-01-13 11:58:06 -05:00
{
$num_segments = 0;
2012-05-03 13:26:09 -04:00
if (strpos($pi, '/') === FALSE && ! empty($pi))
2012-01-13 11:58:06 -05:00
{
$num_segments = 1;
}
else
{
$segments = explode('/', $pi);
$num_segments = count($segments);
}
2012-05-15 11:22:16 -04:00
// Determine route based on uri segments
2012-05-03 13:26:09 -04:00
if ($num_segments === 1)
2012-01-13 11:58:06 -05:00
{
$func = $pi;
}
2012-05-15 11:22:16 -04:00
elseif ($num_segments === 2)
2012-01-13 11:58:06 -05:00
{
2012-05-15 11:22:16 -04:00
list($module, $controller) = $segments;
2012-05-15 11:22:16 -04:00
// If it's just controller/function
if ($controller == 'index')
{
$controller = $module;
$module = $routes['default_module'];
$func = 'index';
}
2012-01-13 11:58:06 -05:00
}
else
2012-01-13 11:58:06 -05:00
{
list($module, $controller, $func) = $segments;
}
}
}
run($module, $controller, $func);
return;
}
// --------------------------------------------------------------------------
/**
* Instantiate the appropriate controller
*
* @param string
* @param string
* @param string
* @param array
*/
function run($module, $controller, $func, $args = array())
{
2012-05-18 08:04:01 -04:00
$path = MM_MOD_PATH . "{$module}/controllers/{$controller}.php";
2012-05-03 13:26:09 -04:00
if (is_file($path))
2012-01-13 11:58:06 -05:00
{
require_once($path);
2012-01-13 11:58:06 -05:00
// Get the list of valid methods for that controller
$methods = controller_methods($controller);
2012-05-03 13:26:09 -04:00
if (in_array($func, $methods))
2012-01-13 11:58:06 -05:00
{
// Define the name of the current module for file loading
if ( ! defined('MM_MOD'))
{
define('MM_MOD', $module);
}
2012-05-01 16:39:14 -04:00
$class = new $controller();
return call_user_func_array(array(&$class, $func), $args);
2012-01-13 11:58:06 -05:00
}
}
// Function doesn't exist...404
2012-01-13 11:58:06 -05:00
show_404();
}
// End of common.php