AppContext.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\App
  3. {
  4. use ModulesGarden\ProxmoxAddon\Core\HandlerError\WhmcsErrorManagerWrapper;
  5. class AppContext
  6. {
  7. protected $debugMode = true;
  8. public function __construct()
  9. {
  10. require_once __DIR__ . DIRECTORY_SEPARATOR . 'ErrorHandler.php';
  11. register_shutdown_function([$this, 'handleShutdown']);
  12. set_error_handler([$this, 'handleError'], E_ALL);
  13. $this->loadDebugState();
  14. //require app bootstrap
  15. require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Bootstrap.php';
  16. if ($this->debugMode)
  17. {
  18. spl_autoload_register(array('\ModulesGarden\ProxmoxAddon\Core\App\AppContext', 'loadClassLoader'), true, false);
  19. }
  20. }
  21. public function runApp($callerName = null, $params = [])
  22. {
  23. try
  24. {
  25. $app = new Application();
  26. $result = $app->run($callerName, $params);
  27. restore_error_handler();
  28. }
  29. catch (\Exception $exc)
  30. {
  31. restore_error_handler();
  32. return [
  33. 'status' => 'error',
  34. 'message' => $exc->getMessage()
  35. ];
  36. }
  37. return $result;
  38. }
  39. public function handleError($errno, $errstr, $errfile, $errline, $errcontext = null)
  40. {
  41. if ($this->debugMode || (!in_array($errno, ErrorHandler::WARNINGS) && !in_array($errno, ErrorHandler::NOTICES)))
  42. {
  43. $handler = new ErrorHandler();
  44. $errorToken = md5(time());
  45. $handler->logError($errorToken, $errno, $errstr, $errfile, $errline, $errcontext);
  46. }
  47. return true;
  48. }
  49. public function handleShutdown()
  50. {
  51. $errorInstance = null;
  52. $errManager = WhmcsErrorManagerWrapper::getErrorManager();
  53. if (is_object($errManager) && method_exists($errManager, 'getRunner'))
  54. {
  55. $runner = $errManager->getRunner();
  56. if (is_object($runner) && method_exists($runner, 'getHandlers'))
  57. {
  58. $handlers = $runner->getHandlers();
  59. foreach ($handlers as $handler)
  60. {
  61. $rfHandler = new \ReflectionClass($handler);
  62. $method = $rfHandler->getMethod('getException');
  63. $method->setAccessible(true);
  64. $error = $method->invoke($handler);
  65. if (is_object($error))
  66. {
  67. $errorInstance = $error;
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. if ($errorInstance === null)
  74. {
  75. $errorInstance = error_get_last();
  76. if ($errorInstance === null)
  77. {
  78. return;
  79. }
  80. $this->handleError($errorInstance['type'], $errorInstance['message'], $errorInstance['file'], $errorInstance['line'], '');
  81. return;
  82. }
  83. $handler = new ErrorHandler();
  84. $errorToken = md5(time());
  85. $handler->logError($errorToken, $errorInstance->getCode(), $errorInstance->getMessage(), $errorInstance->getFile(), $errorInstance->getLine(), $errorInstance->getTrace());
  86. }
  87. public function loadDebugState()
  88. {
  89. $path = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'debug';
  90. if (file_exists($path))
  91. {
  92. $this->debugMode = true;
  93. return;
  94. }
  95. $this->debugMode = false;
  96. }
  97. public static function loadClassLoader($class)
  98. {
  99. $rawClass = trim($class, '\\');
  100. $pos = strpos($rawClass, 'ModulesGarden\ProxmoxAddon');
  101. if ($pos === 0)
  102. {
  103. if (!class_exists($class) && self::DEPRECATED[$rawClass])
  104. {
  105. echo 'This class no longer exists: ' . $class . '<br>';
  106. echo 'Use: ' . self::DEPRECATED[$rawClass];
  107. die();
  108. }
  109. }
  110. }
  111. const DEPRECATED = [
  112. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\BaseMassActionButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonMassAction',
  113. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\AddIconModalButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonCreate',
  114. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\BaseSubmitButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonSubmitForm',
  115. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\BaseButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonBase',
  116. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\BaseDatatableModalButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonDatatableShowModal',
  117. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\BaseModalDataTableActionButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonDataTableModalAction',
  118. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\RedirectButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonRedirect',
  119. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\BaseModalButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonModal',
  120. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\RedirectWithOutTooltipButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonRedirect',
  121. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\OnOffAjaxSwitch' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonSwitchAjax',
  122. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\CustomActionButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonCustomAction',
  123. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\CustomAjaxActionButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonAjaxCustomAction',
  124. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\DatatableModalButtonContextLang' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonDatatableModalContextLang',
  125. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\DropdownButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonDropdown',
  126. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\MassActionButtonContextLang' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonMassActionContextLang',
  127. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\Submit' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonSubmitForm',
  128. 'ModulesGarden\ProxmoxAddon\Core\HandlerError\WhmcsRegisterLoggin' => 'ModulesGarden\ProxmoxAddon\Core\HandlerError\WhmcsLogsHandler',
  129. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonDropdown' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdown',
  130. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\Dropdowntems\DropdownItemButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdownItem',
  131. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\Dropdowntems\DropdownItemCustonAjaxButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdownItem',
  132. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\Dropdowntems\DropdownItemCustonButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdownItem',
  133. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\Dropdowntems\DropdownItemDivider' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdownItem',
  134. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\Dropdowntems\DropdownItemModalButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdownItem',
  135. 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\Dropdowntems\DropdownItemRedirectButton' => 'ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\DropdawnButtonWrappers\ButtonDropdownItem',
  136. 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\ApiException' => 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\Exception',
  137. 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\ApiWhmcsException' => 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\Exception',
  138. 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\ControllerException' => 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\Exception',
  139. 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\DependencyInjectionException' => 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\Exception',
  140. 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\MGModuleException' => 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\Exception',
  141. 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\RegisterException' => 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\Exception',
  142. 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\ServiceLocatorException' => 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\Exception',
  143. 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\SmartyException' => 'ModulesGarden\ProxmoxAddon\Core\HandlerError\Exceptions\Exception',
  144. ];
  145. }
  146. }
  147. namespace
  148. {
  149. }