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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. use Illuminate\Database\Capsule\Manager as Capsule;
  18. class LuticateApplication implements MessageComponentInterface
  19. {
  20. private $_version = 1.0;
  21. private $_config = null;
  22. private $_clients = null;
  23. private $_router = null;
  24. private static $_app = null;
  25. public static function getInstance()
  26. {
  27. return self::$_app;
  28. }
  29. /**
  30. * @param $configuration array
  31. */
  32. public function __construct($configuration)
  33. {
  34. ini_set("display_errors", "stderr");
  35. $this->_config = $configuration;
  36. $this->_clients = new \SplObjectStorage;
  37. $this->_router = new LuRoute();
  38. LuLog::setLogFilePath(__DIR__ . "/../../../../../../". $this->_config["logs"]);
  39. self::$_app = $this;
  40. }
  41. /**
  42. * @return LuRoute
  43. */
  44. public function getRouter()
  45. {
  46. return $this->_router;
  47. }
  48. /**
  49. * @param LuRoute|null $router
  50. */
  51. public function setRouter($router)
  52. {
  53. $this->_router = $router;
  54. }
  55. /**
  56. * @param $version float
  57. */
  58. public function setVersion($version)
  59. {
  60. $this->_version = $version;
  61. }
  62. /**
  63. * @return float
  64. */
  65. public function getVersion()
  66. {
  67. return $this->_version;
  68. }
  69. public function setupRoutes()
  70. {
  71. $app = $this;
  72. require_once __DIR__ . '/../../../../../../app/routes.php';
  73. $this->_router->setup();
  74. }
  75. public function addDatabaseProvider($dbProviderClass)
  76. {
  77. call_user_func([$dbProviderClass, "setupDatabases"], $this->_config['databases']);
  78. }
  79. private function dispatch($httpMethod, $url, $parameters)
  80. {
  81. $r = new LuRequestDbo();
  82. $r->setVersion($this->getVersion());
  83. try {
  84. $result = $this->_router->dispatch($httpMethod, $url, $parameters);
  85. $r->setData($result);
  86. $r->setStatusCode(200);
  87. }
  88. catch (LuBusinessException $e)
  89. {
  90. LuLog::log($e);
  91. $r->setStatusCode($e->getCode());
  92. $r->setMessage($e->getMessage());
  93. }
  94. catch (\Exception $e)
  95. {
  96. LuLog::log($e);
  97. $r->setStatusCode(500);
  98. $r->setMessage("Internal Error");
  99. }
  100. catch (\Error $e)
  101. {
  102. LuLog::log($e);
  103. $r->setStatusCode(500);
  104. $r->setMessage("Internal Error");
  105. }
  106. return $r;
  107. }
  108. public function runHttp()
  109. {
  110. $httpMethod = $_SERVER['REQUEST_METHOD'];
  111. $url = strtok($_SERVER["REQUEST_URI"],'?');
  112. $parameters = array_merge($_GET, $_POST);
  113. $r = $this->dispatch($httpMethod, $url, $parameters);
  114. http_response_code($r->getStatusCode());
  115. header("content-type: application/json");
  116. echo $r->__toString();
  117. }
  118. public function runWs()
  119. {
  120. $server = IoServer::factory(
  121. new HttpServer(
  122. new WsServer(
  123. $this
  124. )
  125. ),
  126. $this->_config['websocket']['port'],
  127. $this->_config['websocket']['address']
  128. );
  129. $server->run();
  130. }
  131. function onMessage(ConnectionInterface $from, $msg)
  132. {
  133. // TODO: Implement onMessage() method.
  134. }
  135. function onOpen(ConnectionInterface $conn)
  136. {
  137. $this->_clients->attach($conn);
  138. }
  139. function onClose(ConnectionInterface $conn)
  140. {
  141. $this->_clients->detach($conn);
  142. }
  143. function onError(ConnectionInterface $conn, \Exception $e)
  144. {
  145. $conn->close();
  146. }
  147. }