ResponseResolver.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\App\Controllers;
  3. use \ThurData\Servers\KerioEmail\Core\UI\View;
  4. use \ThurData\Servers\KerioEmail\Core\Http\JsonResponse;
  5. use \ThurData\Servers\KerioEmail\Core\Http\RedirectResponse;
  6. use \ThurData\Servers\KerioEmail\Core\Http\Response;
  7. class ResponseResolver
  8. {
  9. use \ThurData\Servers\KerioEmail\Core\Traits\Lang;
  10. use \ThurData\Servers\KerioEmail\Core\Traits\Smarty;
  11. use \ThurData\Servers\KerioEmail\Core\Traits\OutputBuffer;
  12. use \ThurData\Servers\KerioEmail\Core\Traits\IsAdmin;
  13. use \ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;
  14. use \ThurData\Servers\KerioEmail\Core\Traits\Template;
  15. protected $response = null;
  16. /**
  17. * @var null|HttpController
  18. */
  19. protected $pageController = null;
  20. public function __construct($response = null)
  21. {
  22. $this->setResponse($response);
  23. $this->loadSmarty();
  24. }
  25. /**
  26. * @param null $response
  27. * @return $this
  28. */
  29. public function setResponse($response = null)
  30. {
  31. if ($response)
  32. {
  33. $this->response = $response;
  34. }
  35. return $this;
  36. }
  37. /**
  38. * resolves the response
  39. */
  40. public function resolve()
  41. {
  42. if ($this->response instanceof View)
  43. {
  44. $this->resolveView();
  45. }
  46. if ($this->response instanceof JsonResponse)
  47. {
  48. $this->resolveJson();
  49. }
  50. elseif ($this->response instanceof RedirectResponse)
  51. {
  52. $this->resolveRedirect();
  53. }
  54. elseif ($this->response instanceof Response)
  55. {
  56. $this->prepareResponse();
  57. return $this->resolveResponse();
  58. }
  59. }
  60. /**
  61. * resolve View object to the processable response
  62. */
  63. public function resolveView()
  64. {
  65. /**
  66. * @var $this->response \ThurData\Servers\KerioEmail\Core\UI\View
  67. */
  68. $this->response->validateAcl($this->isAdmin());
  69. $this->response = $this->response->getResponse();
  70. }
  71. public function prepareResponse()
  72. {
  73. $this->response->setLang($this->lang);
  74. $this->response->setTemplateName($this->getTemplateName());
  75. $this->response->setTemplateDir($this->getTemplateDir());
  76. //$this->smarty->setTemplateDir();
  77. }
  78. /**
  79. * resolve \ThurData\Servers\KerioEmail\Core\Http\JsonResponse
  80. */
  81. public function resolveJson()
  82. {
  83. $this->cleanOutputBuffer();
  84. /**
  85. * @var \ThurData\Servers\KerioEmail\Core\Http\JsonResponse
  86. */
  87. $this->response->send();
  88. die();
  89. }
  90. public function resolveRedirect()
  91. {
  92. /**
  93. * @var \ThurData\Servers\KerioEmail\Core\Http\RedirectResponse
  94. */
  95. die($this->response->send());
  96. }
  97. public function resolveResponse()
  98. {
  99. return $this->response->getHtmlResponse($this);
  100. }
  101. /**
  102. * @param null|HttpController $pageController
  103. */
  104. public function setPageController($pageController)
  105. {
  106. $this->pageController = $pageController;
  107. return $this;
  108. }
  109. /**
  110. * @return HttpController|null
  111. */
  112. public function getPageController()
  113. {
  114. return $this->pageController;
  115. }
  116. }