1. <?php
  2. if( FALSE == isset($_SERVER['REDIRECT_URL']) ) {
  3. if( FALSE == isset($_SERVER['PATH_INFO']) ) {
  4. $_SERVER['REDIRECT_URL'] = $_SERVER['SCRIPT_NAME'];
  5. }
  6. else {
  7. $_SERVER['REDIRECT_URL'] = $_SERVER['PATH_INFO'];
  8. }
  9. }
  10. function Object($object) {
  11. $object = strtolower($object);
  12. static $result = array();
  13. if ( isset($result[$object]) ) return $result[$object];
  14. $file = sprintf('%s/%s.php', __DIR__, str_replace('_', '/', strtolower($object)));
  15. if ( FALSE == file_exists($file) ) {
  16. throw new Exception(sprintf("Cannot find file for object '%s'", $object));
  17. }
  18. require($file);
  19. if ( FALSE == class_exists($object, false) ) {
  20. throw new Exception(sprintf("Cannot find class '%s'", $object));
  21. }
  22. return $result[$object] = new $object();
  23. }
  24. function Redirect($url, $permanent = false) {
  25. header('Location: ' . $url, true, $permanent ? 301 : 302);
  26. exit;
  27. }
  28. function Route($route, $class, $method = NULL, $on = 'GET') {
  29. $method = empty($method) ? 'index' : $method;
  30. $on = strtoupper(empty($on) ? $_SERVER['REQUEST_METHOD'] : $on);
  31. if ( $on === $_SERVER['REQUEST_METHOD'] ) {
  32. $matches = array();
  33. $url = trim($_SERVER['REDIRECT_URL'], '/');
  34. if (preg_match('(^' . $route . '$)i', $url, $matches) > 0) {
  35. exit(call_user_func_array(array(Object($class), $method), array_slice($matches, 1)));
  36. }
  37. }
  38. }
  39. function RestRoute($route, $class, $method = NULL, $on = 'GET') {
  40. $method = empty($method) ? 'index' : $method;
  41. $on = strtoupper(empty($on) ? $_SERVER['REQUEST_METHOD'] : $on);
  42. if ( $on === $_SERVER['REQUEST_METHOD'] ) {
  43. $matches = array();
  44. $url = trim($_SERVER['REDIRECT_URL'], '/');
  45. if (preg_match('(^' . $route . '$)i', $url, $matches) > 0) {
  46. try {
  47. $data = array(
  48. 'OK' => TRUE,
  49. 'ERROR' => NULL,
  50. 'DATA' => call_user_func_array(array(Object($class), $method), array_slice($matches, 1))
  51. );
  52. if( $data['DATA'] instanceof Traversable ) {
  53. $data['DATA'] = iterator_to_array($data['DATA']);
  54. }
  55. }
  56. catch( Exception $ex ) {
  57. $data = array(
  58. 'OK' => FALSE,
  59. 'ERROR' => array(
  60. 'CODE' => $ex->getCode(),
  61. 'MSG' => $ex->getMessage(),
  62. ),
  63. 'DATA' => NULL,
  64. );
  65. }
  66. header('Content-Type: text/javascript; charset=utf-8');
  67. echo json_encode($data);
  68. exit;
  69. }
  70. }
  71. }
  72. function View($__view, $arguments = array()) {
  73. $__file = sprintf('%s/%s.phtml', __DIR__, strtolower(str_replace('_', '/', $__view)));
  74. if( FALSE == file_exists($__file) ) {
  75. throw new Exception(sprintf("Template '%s' not found!", $__view));
  76. }
  77. extract($arguments);
  78. require($__file);
  79. }
  80. function Render($__view, $arguments = array()) {
  81. ob_start();
  82. View($__view, $arguments);
  83. return ob_get_clean();
  84. }
  85. function Param($name, $default = NULL) {
  86. if( isset($_REQUEST[$name]) ) $_REQUEST[$name];
  87. return $default;
  88. }
  89. function IsMethod($name) {
  90. return $_SERVER['REQUEST_METHOD'] == strtoupper($name);
  91. }
  92. function IsPOST() {
  93. return IsMethod('POST');
  94. }
  95. function IsGET() {
  96. return IsMethod('GET');
  97. }
  98. function IsPUT() {
  99. return IsMethod('PUT');
  100. }
  101. function IsDELETE() {
  102. return IsMethod('DELETE');
  103. }
  104. function GetPostJSON() {
  105. if( IsPost() ) {
  106. return json_decode(file_get_contents('php://stdin'));
  107. }
  108. }
  109. function Url($path = NULL, $params = array()) {
  110. if( NULL === $path ) $path = $_SERVER['REDIRECT_URL'];
  111. if( NULL === $params ) $params = $_GET;
  112. return sprintf("%s?%s", $path, http_build_query($params));
  113. }
  114. function Config($name, $default = NULL) {
  115. if( isset($_ENV[$name]) ) return $_ENV[$name];
  116. if( function_exists('apache_getenv') ) {
  117. $value = apache_getenv($name);
  118. if( FALSE === $value ) return $default;
  119. return $value;
  120. }
  121. return $default;
  122. }
  123. function get_public_vars($obj) {
  124. return (array)$obj;
  125. }