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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. $this->_config = $configuration;
  35. $this->_clients = new \SplObjectStorage;
  36. $this->_router = new LuRoute();
  37. LuLog::setLogFilePath(__DIR__ . "/../../../../../../". $this->_config["logs"]);
  38. self::$_app = $this;
  39. }
  40. /**
  41. * @return LuRoute
  42. */
  43. public function getRouter()
  44. {
  45. return $this->_router;
  46. }
  47. /**
  48. * @param LuRoute|null $router
  49. */
  50. public function setRouter($router)
  51. {
  52. $this->_router = $router;
  53. }
  54. /**
  55. * @param $version float
  56. */
  57. public function setVersion($version)
  58. {
  59. $this->_version = $version;
  60. }
  61. /**
  62. * @return float
  63. */
  64. public function getVersion()
  65. {
  66. return $this->_version;
  67. }
  68. public function setupRoutes()
  69. {
  70. $app = $this;
  71. require_once __DIR__ . '/../../../../../../app/routes.php';
  72. $this->_router->setup();
  73. }
  74. public function setupDatabases()
  75. {
  76. $capsule = new Capsule;
  77. foreach ($this->_config['databases'] as $database) {
  78. $default = [
  79. 'name' => 'mydb',
  80. 'driver' => 'pgsql',
  81. 'host' => '172.17.0.1',
  82. 'database' => 'luticate2',
  83. 'username' => 'dev',
  84. 'password' => 'dev',
  85. 'charset' => 'utf8',
  86. 'collation' => 'utf8_unicode_ci',
  87. 'prefix' => ''
  88. ];
  89. $capsule->addConnection(array_merge($default, $database), $database['name']);
  90. }
  91. $capsule->setAsGlobal();
  92. }
  93. private function dispatch($httpMethod, $url, $parameters)
  94. {
  95. $r = new LuRequestDbo();
  96. $r->setVersion($this->getVersion());
  97. try {
  98. $result = $this->_router->dispatch($httpMethod, $url, $parameters);
  99. $r->setData($result);
  100. $r->setStatusCode(200);
  101. }
  102. catch (LuBusinessException $e)
  103. {
  104. LuLog::log($e);
  105. $r->setStatusCode($e->getCode());
  106. $r->setMessage($e->getMessage());
  107. }
  108. catch (\Exception $e)
  109. {
  110. LuLog::log($e);
  111. $r->setStatusCode(500);
  112. $r->setMessage("Internal Error");
  113. }
  114. catch (\Error $e)
  115. {
  116. LuLog::log($e);
  117. $r->setStatusCode(500);
  118. $r->setMessage("Internal Error");
  119. }
  120. return $r;
  121. }
  122. public function runHttp()
  123. {
  124. $httpMethod = $_SERVER['REQUEST_METHOD'];
  125. $url = $_SERVER['REQUEST_URI'];
  126. $parameters = array_merge($_GET, $_POST);
  127. $r = $this->dispatch($httpMethod, $url, $parameters);
  128. http_response_code($r->getStatusCode());
  129. header("content-type: application/json");
  130. echo $r->__toString();
  131. }
  132. public function runWs()
  133. {
  134. $server = IoServer::factory(
  135. new HttpServer(
  136. new WsServer(
  137. $this
  138. )
  139. ),
  140. $this->_config['websocket']['port'],
  141. $this->_config['websocket']['address']
  142. );
  143. $server->run();
  144. }
  145. function onMessage(ConnectionInterface $from, $msg)
  146. {
  147. // TODO: Implement onMessage() method.
  148. }
  149. function onOpen(ConnectionInterface $conn)
  150. {
  151. $this->_clients->attach($conn);
  152. }
  153. function onClose(ConnectionInterface $conn)
  154. {
  155. $this->_clients->detach($conn);
  156. }
  157. function onError(ConnectionInterface $conn, \Exception $e)
  158. {
  159. $conn->close();
  160. }
  161. }