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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. error_reporting(0);
  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 setupDatabases()
  76. {
  77. $capsule = new Capsule;
  78. foreach ($this->_config['databases'] as $database) {
  79. $default = [
  80. 'name' => 'mydb',
  81. 'driver' => 'pgsql',
  82. 'host' => '172.17.0.1',
  83. 'database' => 'luticate2',
  84. 'username' => 'dev',
  85. 'password' => 'dev',
  86. 'charset' => 'utf8',
  87. 'collation' => 'utf8_unicode_ci',
  88. 'prefix' => ''
  89. ];
  90. $db = array_merge($default, $database);
  91. if ($db['driver'] == 'pgsql' || $db['driver'] == 'mysql' ||
  92. $db['driver'] == 'sqlite' || $db['driver'] == 'sqlsrv') {
  93. $capsule->addConnection($db, $database['name']);
  94. }
  95. }
  96. $capsule->setAsGlobal();
  97. }
  98. private function dispatch($httpMethod, $url, $parameters)
  99. {
  100. $r = new LuRequestDbo();
  101. $r->setVersion($this->getVersion());
  102. try {
  103. $result = $this->_router->dispatch($httpMethod, $url, $parameters);
  104. $r->setData($result);
  105. $r->setStatusCode(200);
  106. }
  107. catch (LuBusinessException $e)
  108. {
  109. LuLog::log($e);
  110. $r->setStatusCode($e->getCode());
  111. $r->setMessage($e->getMessage());
  112. }
  113. catch (\Exception $e)
  114. {
  115. LuLog::log($e);
  116. $r->setStatusCode(500);
  117. $r->setMessage("Internal Error");
  118. }
  119. catch (\Error $e)
  120. {
  121. LuLog::log($e);
  122. $r->setStatusCode(500);
  123. $r->setMessage("Internal Error");
  124. }
  125. return $r;
  126. }
  127. public function runHttp()
  128. {
  129. $httpMethod = $_SERVER['REQUEST_METHOD'];
  130. $url = strtok($_SERVER["REQUEST_URI"],'?');
  131. $parameters = array_merge($_GET, $_POST);
  132. $r = $this->dispatch($httpMethod, $url, $parameters);
  133. http_response_code($r->getStatusCode());
  134. header("content-type: application/json");
  135. echo $r->__toString();
  136. }
  137. public function runWs()
  138. {
  139. $server = IoServer::factory(
  140. new HttpServer(
  141. new WsServer(
  142. $this
  143. )
  144. ),
  145. $this->_config['websocket']['port'],
  146. $this->_config['websocket']['address']
  147. );
  148. $server->run();
  149. }
  150. function onMessage(ConnectionInterface $from, $msg)
  151. {
  152. // TODO: Implement onMessage() method.
  153. }
  154. function onOpen(ConnectionInterface $conn)
  155. {
  156. $this->_clients->attach($conn);
  157. }
  158. function onClose(ConnectionInterface $conn)
  159. {
  160. $this->_clients->detach($conn);
  161. }
  162. function onError(ConnectionInterface $conn, \Exception $e)
  163. {
  164. $conn->close();
  165. }
  166. }