You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuticateApplication.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: robin
  5. * Date: 6/4/16
  6. * Time: 10:06 PM
  7. */
  8. namespace Luticate\Utils\Controller;
  9. use Luticate\Utils\Business\LuBusinessException;
  10. use Luticate\Utils\Business\LuLog;
  11. use Luticate\Utils\Dbo\LuRequestDbo;
  12. use Ratchet\ConnectionInterface;
  13. use Ratchet\MessageComponentInterface;
  14. use Ratchet\Http\HttpServer;
  15. use Ratchet\Server\IoServer;
  16. use Ratchet\WebSocket\WsServer;
  17. class LuticateApplication implements MessageComponentInterface
  18. {
  19. private $_version = 1.0;
  20. private $_config = null;
  21. private $_clients = null;
  22. private $_router = null;
  23. private static $_app = null;
  24. public static function getInstance()
  25. {
  26. return self::$_app;
  27. }
  28. public static function getDatabase($name, $databases)
  29. {
  30. foreach ($databases as $database) {
  31. if (is_array($database) && $database["name"] == $name) {
  32. return $database;
  33. }
  34. }
  35. return null;
  36. }
  37. public static function resolveDatabases($databases) {
  38. $dbs = [];
  39. foreach ($databases as $database) {
  40. if (is_string($database)) {
  41. $database = static::getDatabase($database, $databases);
  42. }
  43. if (is_array($database)) {
  44. $dbs[] = $database;
  45. }
  46. }
  47. return $dbs;
  48. }
  49. /**
  50. * @param $configuration array
  51. */
  52. public function __construct($configuration)
  53. {
  54. if (php_sapi_name() != "cli") {
  55. error_reporting(0);
  56. }
  57. $this->_config = $configuration;
  58. $this->_config["databases"] = static::resolveDatabases($this->_config["databases"]);
  59. if (!isset($this->_config['settings'])) {
  60. $this->_config['settings'] = [];
  61. }
  62. $this->_clients = new \SplObjectStorage;
  63. $this->_router = new LuRoute();
  64. LuLog::setLogFilePath(__DIR__ . "/../../../../../../". $this->_config["logs"]);
  65. self::$_app = $this;
  66. }
  67. public function getSetting($name, $default = null)
  68. {
  69. $value = getenv("lu_setting_" . $name);
  70. if ($value === false) {
  71. if (isset($this->_config['settings'][$name])) {
  72. $value = $this->_config['settings'][$name];
  73. }
  74. else {
  75. $value = $default;
  76. }
  77. }
  78. return $value;
  79. }
  80. /**
  81. * @return LuRoute
  82. */
  83. public function getRouter()
  84. {
  85. return $this->_router;
  86. }
  87. /**
  88. * @param LuRoute|null $router
  89. */
  90. public function setRouter($router)
  91. {
  92. $this->_router = $router;
  93. }
  94. /**
  95. * @param $version float
  96. */
  97. public function setVersion($version)
  98. {
  99. $this->_version = $version;
  100. }
  101. /**
  102. * @return float
  103. */
  104. public function getVersion()
  105. {
  106. return $this->_version;
  107. }
  108. public function setupRoutes()
  109. {
  110. /** @noinspection PhpUnusedLocalVariableInspection */
  111. $app = $this;
  112. /** @noinspection PhpIncludeInspection */
  113. require_once __DIR__ . '/../../../../../../app/routes.php';
  114. $this->_router->setup();
  115. }
  116. public function addDatabaseProvider($dbProviderClass)
  117. {
  118. call_user_func([$dbProviderClass, "setupDatabases"], $this->_config['databases']);
  119. }
  120. private function dispatch($httpMethod, $url, $parameters)
  121. {
  122. $r = new LuRequestDbo();
  123. $r->setVersion($this->getVersion());
  124. try {
  125. $result = $this->_router->dispatch($httpMethod, $url, $parameters);
  126. $r->setData($result);
  127. $r->setStatusCode(200);
  128. }
  129. catch (LuBusinessException $e)
  130. {
  131. LuLog::log($e);
  132. $r->setStatusCode($e->getCode());
  133. $r->setMessage($e->getMessage());
  134. }
  135. catch (\Exception $e)
  136. {
  137. LuLog::log($e);
  138. $r->setStatusCode(500);
  139. $r->setMessage("Internal Error");
  140. }
  141. catch (\Error $e)
  142. {
  143. LuLog::log($e);
  144. $r->setStatusCode(500);
  145. $r->setMessage("Internal Error");
  146. }
  147. return $r;
  148. }
  149. public function runHttp()
  150. {
  151. $httpMethod = $_SERVER['REQUEST_METHOD'];
  152. $url = strtok($_SERVER["REQUEST_URI"],'?');
  153. $parameters = array_merge($_GET, $_POST);
  154. $r = $this->dispatch($httpMethod, $url, $parameters);
  155. http_response_code($r->getStatusCode());
  156. header("content-type: application/json");
  157. echo $r->__toString();
  158. }
  159. public function runWs()
  160. {
  161. $server = IoServer::factory(
  162. new HttpServer(
  163. new WsServer(
  164. $this
  165. )
  166. ),
  167. $this->_config['websocket']['port'],
  168. $this->_config['websocket']['address']
  169. );
  170. $server->run();
  171. }
  172. function onMessage(ConnectionInterface $from, $msg)
  173. {
  174. // TODO: Implement onMessage() method.
  175. }
  176. function onOpen(ConnectionInterface $conn)
  177. {
  178. $this->_clients->attach($conn);
  179. }
  180. function onClose(ConnectionInterface $conn)
  181. {
  182. $this->_clients->detach($conn);
  183. }
  184. function onError(ConnectionInterface $conn, \Exception $e)
  185. {
  186. $conn->close();
  187. }
  188. }