Router.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\App\Controllers;
  3. class Router
  4. {
  5. use \ThurData\Servers\KerioEmail\Core\Traits\Lang;
  6. use \ThurData\Servers\KerioEmail\Core\Traits\Smarty;
  7. use \ThurData\Servers\KerioEmail\Core\Traits\OutputBuffer;
  8. use \ThurData\Servers\KerioEmail\Core\Traits\IsAdmin;
  9. use \ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;
  10. const ADMIN = 'admin';
  11. const CLIENT = 'client';
  12. protected $controllerClass = null;
  13. protected $controllerMethod = null;
  14. public function __construct()
  15. {
  16. $this->isAdmin();
  17. $this->loadController();
  18. }
  19. /**
  20. * load routing params
  21. */
  22. public function loadController()
  23. {
  24. $this->getControllerClass();
  25. $this->getControllerMethod();
  26. }
  27. /**
  28. * @return string
  29. * class name of the controller
  30. */
  31. public function getControllerClass()
  32. {
  33. if ($this->controllerClass === null)
  34. {
  35. $this->controllerClass = '\ThurData\Servers\KerioEmail\App\Http\\' . ucfirst($this->getControllerType()) . '\\' . ucfirst($this->getController());
  36. }
  37. return $this->controllerClass;
  38. }
  39. /**
  40. * @return string
  41. * admin/client context type
  42. */
  43. public function getControllerType()
  44. {
  45. return $this->isAdminStatus ? self::ADMIN : self::CLIENT;
  46. }
  47. /**
  48. * @return string
  49. * get controller name
  50. */
  51. public function getController()
  52. {
  53. return filter_var($this->getRequestValue('mg-page', 'Home'), FILTER_SANITIZE_SPECIAL_CHARS);
  54. }
  55. /**
  56. * @return string
  57. * controller method name
  58. */
  59. public function getControllerMethod()
  60. {
  61. if ($this->controllerMethod === null)
  62. {
  63. $this->controllerMethod = $this->request->get('mg-action', 'index');
  64. }
  65. return $this->controllerMethod;
  66. }
  67. /**
  68. * @return bool
  69. * determines if controller can be called
  70. */
  71. public function isControllerCallable()
  72. {
  73. if (!class_exists($this->controllerClass))
  74. {
  75. return false;
  76. }
  77. if (!method_exists($this->controllerClass, $this->controllerMethod))
  78. {
  79. return false;
  80. }
  81. if (!is_callable([$this->controllerClass, $this->controllerMethod]))
  82. {
  83. return false;
  84. }
  85. return true;
  86. }
  87. }