- <?php
- if( FALSE == isset($_SERVER['REDIRECT_URL']) ) {
- if( FALSE == isset($_SERVER['PATH_INFO']) ) {
- $_SERVER['REDIRECT_URL'] = $_SERVER['SCRIPT_NAME'];
- }
- else {
- $_SERVER['REDIRECT_URL'] = $_SERVER['PATH_INFO'];
- }
- }
- function Object($object) {
- $object = strtolower($object);
- static $result = array();
- if ( isset($result[$object]) ) return $result[$object];
- $file = sprintf('%s/%s.php', __DIR__, str_replace('_', '/', strtolower($object)));
- if ( FALSE == file_exists($file) ) {
- throw new Exception(sprintf("Cannot find file for object '%s'", $object));
- }
- require($file);
- if ( FALSE == class_exists($object, false) ) {
- throw new Exception(sprintf("Cannot find class '%s'", $object));
- }
- return $result[$object] = new $object();
- }
- function Redirect($url, $permanent = false) {
- header('Location: ' . $url, true, $permanent ? 301 : 302);
- exit;
- }
- function Route($route, $class, $method = NULL, $on = 'GET') {
- $method = empty($method) ? 'index' : $method;
- $on = strtoupper(empty($on) ? $_SERVER['REQUEST_METHOD'] : $on);
- if ( $on === $_SERVER['REQUEST_METHOD'] ) {
- $matches = array();
- $url = trim($_SERVER['REDIRECT_URL'], '/');
- if (preg_match('(^' . $route . '$)i', $url, $matches) > 0) {
- exit(call_user_func_array(array(Object($class), $method), array_slice($matches, 1)));
- }
- }
- }
- function RestRoute($route, $class, $method = NULL, $on = 'GET') {
- $method = empty($method) ? 'index' : $method;
- $on = strtoupper(empty($on) ? $_SERVER['REQUEST_METHOD'] : $on);
- if ( $on === $_SERVER['REQUEST_METHOD'] ) {
- $matches = array();
- $url = trim($_SERVER['REDIRECT_URL'], '/');
- if (preg_match('(^' . $route . '$)i', $url, $matches) > 0) {
- try {
- $data = array(
- 'OK' => TRUE,
- 'ERROR' => NULL,
- 'DATA' => call_user_func_array(array(Object($class), $method), array_slice($matches, 1))
- );
- if( $data['DATA'] instanceof Traversable ) {
- $data['DATA'] = iterator_to_array($data['DATA']);
- }
- }
- catch( Exception $ex ) {
- $data = array(
- 'OK' => FALSE,
- 'ERROR' => array(
- 'CODE' => $ex->getCode(),
- 'MSG' => $ex->getMessage(),
- ),
- 'DATA' => NULL,
- );
- }
- header('Content-Type: text/javascript; charset=utf-8');
- echo json_encode($data);
- exit;
- }
- }
- }
- function View($__view, $arguments = array()) {
- $__file = sprintf('%s/%s.phtml', __DIR__, strtolower(str_replace('_', '/', $__view)));
- if( FALSE == file_exists($__file) ) {
- throw new Exception(sprintf("Template '%s' not found!", $__view));
- }
- extract($arguments);
- require($__file);
- }
- function Render($__view, $arguments = array()) {
- ob_start();
- View($__view, $arguments);
- return ob_get_clean();
- }
- function Param($name, $default = NULL) {
- if( isset($_REQUEST[$name]) ) $_REQUEST[$name];
- return $default;
- }
- function IsMethod($name) {
- return $_SERVER['REQUEST_METHOD'] == strtoupper($name);
- }
- function IsPOST() {
- return IsMethod('POST');
- }
- function IsGET() {
- return IsMethod('GET');
- }
- function IsPUT() {
- return IsMethod('PUT');
- }
- function IsDELETE() {
- return IsMethod('DELETE');
- }
- function GetPostJSON() {
- if( IsPost() ) {
- return json_decode(file_get_contents('php://stdin'));
- }
- }
- function Url($path = NULL, $params = array()) {
- if( NULL === $path ) $path = $_SERVER['REDIRECT_URL'];
- if( NULL === $params ) $params = $_GET;
- return sprintf("%s?%s", $path, http_build_query($params));
- }
- function Config($name, $default = NULL) {
- if( isset($_ENV[$name]) ) return $_ENV[$name];
- if( function_exists('apache_getenv') ) {
- $value = apache_getenv($name);
- if( FALSE === $value ) return $default;
- return $value;
- }
- return $default;
- }
- function get_public_vars($obj) {
- return (array)$obj;
- }