Application.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. spl_autoload_register(function($class){
  3. if (file_exists("Interfaces/$class.php")){
  4. include("Interfaces/$class.php");
  5. return;
  6. }
  7. if (file_exists("Model/$class.php"))
  8. include("Model/$class.php");
  9. });
  10. /*include_once 'ApplicationSettings.php';
  11. include_once 'View.php';
  12. include_once 'Helper.php';
  13. include_once 'IConvertible.php';*/
  14. $files=glob("base/*.php");
  15. foreach ($files as $file)
  16. require_once $file;
  17. class Application{
  18. protected $_view, $_controller, $_url;
  19. protected function LoadPage($page,$action,$params){
  20. $controller=self::FindControllerPath($page);
  21. if ($controller===false){
  22. $page="E404";
  23. $controller="Controller/E404.php";
  24. $params=array($this->_url);
  25. }
  26. include_once $controller;
  27. $this->_controller=new $page();
  28. if (!method_exists($this->_controller, $action))
  29. $action="Index";
  30. $this->_view=$this->_controller->$action($params);
  31. }
  32. function __construct($url) {
  33. $this->_url;
  34. // session_start();
  35. $page=ApplicationSettings::GetSetting("general", "default_page");
  36. $action="Index";
  37. $params=array();
  38. $parts=explode("/", $url, 3);
  39. switch (count($parts)){
  40. case 3:
  41. if ($parts[2]!='')
  42. $params=explode("/",$parts[2]);
  43. case 2:
  44. //$action=ucfirst($parts[1]);
  45. $action=$parts[1];
  46. case 1:
  47. if ($parts[0]!="")
  48. $page=$parts[0];
  49. break;
  50. }
  51. foreach ($_GET as $key=>$_)
  52. $params[$key]=filter_input(INPUT_GET, $key);
  53. foreach ($_POST as $key=>$_)
  54. if (!is_array($_))
  55. $params[$key]=filter_input(INPUT_POST, $key);
  56. else
  57. $params[$key]=filter_input(INPUT_POST,$key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
  58. //$params['cookies']=array();;
  59. //foreach ($_COOKIE as $key=>$_)
  60. // $params['cookies'][$key]=filter_input(INPUT_COOKIE, $key);
  61. $this->LoadPage($page, $action, $params);
  62. }
  63. public function GetOutput(){
  64. if (gettype($this->_view)=="object")
  65. return $this->_view->GetHTMLFromTemplate(ApplicationSettings::GetSetting("general", "template"));
  66. switch (gettype($this->_view)){
  67. case "string":
  68. case "integer":
  69. return $this->_view;
  70. }
  71. return "";
  72. }
  73. public static function FindControllerPath($controller){
  74. $controllerLower=strtolower($controller);
  75. $files=glob("Controller/*.php");
  76. foreach ($files as $file)
  77. if (strtolower(basename($file,".php"))==$controllerLower)
  78. return $file;
  79. return false;
  80. }
  81. }