/clear/core.php

https://github.com/didierjc/clear · PHP · 118 lines · 72 code · 10 blank · 36 comment · 9 complexity · 65cb9f21022341164e84d407733dc113 MD5 · raw file

  1. <?php
  2. /** Check if environment is development and display errors **/
  3. function setReporting(){
  4. if(DEVELOPMENT_ENVIRONMENT == true){
  5. // error_reporting() | sets the error_reporting directive at runtime; PHP has many levels of errors, using this function sets that level for the duration of your script
  6. error_reporting(E_ALL); // E_ALL | All errors and warnings
  7. ini_set('display_errors', 1); // set the value of a configuration option -- display_errors: determines whether errors should be printed to the screen as part of the output
  8. }else{
  9. error_reporting(E_ALL);
  10. ini_set('display_errors', 0);
  11. ini_set('log_errors', 1); // script error messages will be logged to the server's error log
  12. ini_set('error_log', ROOT.DS.'tmp'.DS.'logs'.DS.'error_'.date("Ymd").'_'.date("His").'.log'); // name of the file where script errors should be logged
  13. }
  14. }
  15. /** Secondary Call Function **/
  16. function performAction($controller,$action,$queryString = null,$render = 0){
  17. $controllerName = strtolower('controller_'.$controller);
  18. $dispatch = new $controllerName($controller,$action);
  19. $dispatch->render = $render;
  20. return call_user_func_array(array($dispatch,$action),$queryString);
  21. }
  22. /** Routing **/
  23. function routeURL($url){
  24. global $routing;
  25. foreach($routing as $pattern => $result){
  26. if(preg_match($pattern, $url)){
  27. return preg_replace($pattern, $result, $url);
  28. }
  29. }
  30. return ($url);
  31. }
  32. /** Main Call Function **/
  33. /*
  34. our URLs will look - yoursite.com/controllerName/actionName/queryString
  35. callHook() basically takes the URL which we have received from index.php and separates it out as $controller, $action and the remaining as $queryString.
  36. $model is the singular version of $controller.
  37. example: if our URL is example.com/items/del/1/first-item, then
  38. Controller => items
  39. Model => item (corresponding database table)
  40. View => del
  41. Action => del
  42. Query String is an array (1,first-item)
  43. After the separation is done, it creates a new object of the class ”controller_”.$controller and calls the method $action of the class.
  44. */
  45. function callHook() {
  46. global $url;
  47. global $default;
  48. $queryString = array();
  49. if(!isset($url)){ // $url is set in public/index.php -- line 24
  50. $controller = $default['controller']; // this is set in configuration/routing.php -- line 16
  51. $action = $default['action']; // this is set in configuration/routing.php -- line 17
  52. }else{
  53. $url = routeURL($url); // look at line 25
  54. $urlArray = array();
  55. $urlArray = explode("/",$url); // break apart the URL, using "/" as the delimiter
  56. // identify the Controller
  57. // Controllers will always be lowercase, plural and begin with "controller_" i.e. ctrl_items, ctrl_cars, ctrl_users
  58. $controller = strtolower($urlArray[0]);
  59. array_shift($urlArray); // array_shift | shifts the first value of the array off and returns the array, minus one element and moving everything down
  60. if(isset($urlArray[0])){
  61. $action = $urlArray[0];
  62. array_shift($urlArray); // array_shift | shifts the first value of the array off and returns the array, minus one element and moving everything down
  63. }else{
  64. $action = 'index'; // Default Action
  65. }
  66. $queryString = $urlArray; // the remainder of the array is the query string
  67. }
  68. $controllerName = strtolower('controller_'.$controller); //$controllerName is the file & class name of the controller
  69. $dispatch = new $controllerName($controller,$action);
  70. // method_exists — Checks if the class method exists -- If in this class: $controllerClass, this method: $action exists...
  71. // the (int) part casts it as an integer so it will display 0 if false and 1 if true
  72. if ((int)method_exists($controllerName, $action)) {
  73. // call_user_func_array — Call a user function given with an array of parameters
  74. /*
  75. * primarily useful as a way to dynamically call functions and methods at runtime without having to use eval()
  76. * call_user_func_array() passes the elements in argument_array to function_name as an argument list. This makes it easy to pass an unknown number
  77. * of arguments to a function.
  78. *
  79. * Normally, functions are called with the following syntax: function_name('arg one', 'arg two', ...);
  80. * Calling the same function using call_user_func_array() would look like this: call_user_func_array ('function_name', array ('arg one', 'arg two', ...));
  81. */
  82. call_user_func_array(array($dispatch,"beforeAction"),$queryString);
  83. call_user_func_array(array($dispatch,$action),$queryString);
  84. call_user_func_array(array($dispatch,"afterAction"),$queryString);
  85. }else{
  86. /* Error Generation Code Here */
  87. }
  88. }
  89. /** Autoload any classes that are required **/
  90. function __autoload($className){
  91. if(file_exists(ROOT.DS.'clear'.DS.strtolower($className).'.class.php')){
  92. require_once(ROOT.DS.'clear'.DS.strtolower($className).'.class.php');
  93. }else if(file_exists(ROOT.DS.'application'.DS.'controllers'.DS.strtolower($className).'.php')){
  94. require_once(ROOT.DS.'application'.DS.'controllers'.DS.strtolower($className).'.php');
  95. }else if(file_exists(ROOT.DS.'application'.DS.'models'.DS.strtolower($className).'.php')){
  96. require_once(ROOT.DS.'application'.DS.'models'.DS.strtolower($className).'.php');
  97. }else{
  98. /* Error Generation Code Here */
  99. }
  100. }
  101. $cache = new Cache();
  102. $inflect = new Inflection();
  103. setReporting();
  104. callHook();
  105. ?>