'; if ($method == "var_dump") { ob_start(); var_dump($data); $output .= ob_get_contents(); ob_end_clean(); } elseif ($method == "var_export") { ob_start(); var_export($data); $output .= ob_get_contents(); ob_end_clean(); } else { $output .= print_r($data, TRUE); } return $output . ''; } // -------------------------------------------------------------------------- if ( ! function_exists('do_include')) { /** * Array_map callback to load a folder of classes at once * * @param string $path * @return void */ function do_include($path) { require_once($path); } } // -------------------------------------------------------------------------- // ! Bootstrap functions // -------------------------------------------------------------------------- /** * Load required classes for bootstraping * * @return void */ function init() { // Map to the appropriate module/controller/function route(); } // -------------------------------------------------------------------------- /** * Returns the last segment of the current url * * @return string */ 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() { $sn = $_SERVER['SCRIPT_NAME']; $ru = $_SERVER['REQUEST_URI']; // Get the equivalent to path info $pi = (isset($_SERVER['PATH_INFO'])) ? str_replace($sn, '', $ru) : '/'; // 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() { $router_factory = new RouterFactory; $router = $router_factory->newInstance(); // Load the routes config file to add additional routes $routes = []; require_once(MM_APP_PATH . 'config/routes.php'); // get the incoming request URL path $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); // get the route based on the path and server $route = $router->match($path, $_SERVER); // Set default controller/function $controller = $routes['default_controller']; $action = 'index'; // 404 Condition if (empty($route)) { throw new \Exception("404: Page not found."); //show_404(); return; } // Home if (isset($route->name) && $route->name === 'home') { run($controller, $action); return; } // Gather route parts foreach(array('controller', 'action', 'id') as $param) { if (isset($route->params[$param])) { $$param = $route->params[$param]; } } if ( ! isset($id)) $id = array(); // Dispatch to the appropriate controller run($controller, $action, array($id)); } // -------------------------------------------------------------------------- /** * Instantiate the appropriate controller * * @param string $controller * @param string $func * @param array $args * @return void */ function run($controller, $func, $args = array()) { $path = MM_APP_PATH . "controllers/{$controller}.php"; if (is_file($path)) { require_once($path); // Get the list of valid methods for that controller $methods = controller_methods($controller); if (in_array($func, $methods)) { // Define the name of the current module for file loading if ( ! defined('MM_MOD')) { define('MM_MOD', 'meta'); } if (class_exists($controller)) { $class = new $controller(); } call_user_func_array(array($class, $func), $args); return; } } // Function doesn't exist...404 show_404(); } // End of common.php