123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- /**
- * Created by PhpStorm.
- * User: robin
- * Date: 6/4/16
- * Time: 10:06 PM
- */
-
- namespace Luticate\Utils\Controller;
-
- use Luticate\Utils\Business\LuBusinessException;
- use Luticate\Utils\Business\LuLog;
- use Luticate\Utils\Dbo\LuRequestDbo;
- use Ratchet\ConnectionInterface;
- use Ratchet\MessageComponentInterface;
- use Ratchet\Http\HttpServer;
- use Ratchet\Server\IoServer;
- use Ratchet\WebSocket\WsServer;
- use Illuminate\Database\Capsule\Manager as Capsule;
-
- class LuticateApplication implements MessageComponentInterface
- {
- private $_version = 1.0;
- private $_config = null;
- private $_clients = null;
- private $_router = null;
-
- private static $_app = null;
-
- public static function getInstance()
- {
- return self::$_app;
- }
-
- /**
- * @param $configuration array
- */
- public function __construct($configuration)
- {
- error_reporting(0);
- $this->_config = $configuration;
- $this->_clients = new \SplObjectStorage;
- $this->_router = new LuRoute();
- LuLog::setLogFilePath(__DIR__ . "/../../../../../../". $this->_config["logs"]);
- self::$_app = $this;
- }
-
- /**
- * @return LuRoute
- */
- public function getRouter()
- {
- return $this->_router;
- }
-
- /**
- * @param LuRoute|null $router
- */
- public function setRouter($router)
- {
- $this->_router = $router;
- }
-
- /**
- * @param $version float
- */
- public function setVersion($version)
- {
- $this->_version = $version;
- }
-
- /**
- * @return float
- */
- public function getVersion()
- {
- return $this->_version;
- }
-
- public function setupRoutes()
- {
- $app = $this;
- require_once __DIR__ . '/../../../../../../app/routes.php';
-
- $this->_router->setup();
- }
-
- public function getDatabases()
- {
- return $this->_config['databases'];
- }
-
- public function setupDatabases()
- {
- $capsule = new Capsule;
- foreach (static::getDatabases() as $database) {
-
- if ($database['driver'] == 'pgsql' || $database['driver'] == 'mysql' ||
- $database['driver'] == 'sqlite' || $database['driver'] == 'sqlsrv') {
- $default = [
- 'name' => 'mydb',
- 'host' => '172.17.0.1',
- 'database' => 'luticate2',
- 'username' => 'dev',
- 'password' => 'dev',
- 'charset' => 'utf8',
- 'collation' => 'utf8_unicode_ci',
- 'prefix' => ''
- ];
- $db = array_merge($default, $database);
- $capsule->addConnection($db, $database['name']);
- }
- }
- $capsule->setAsGlobal();
- }
-
- private function dispatch($httpMethod, $url, $parameters)
- {
- $r = new LuRequestDbo();
- $r->setVersion($this->getVersion());
-
- try {
- $result = $this->_router->dispatch($httpMethod, $url, $parameters);
- $r->setData($result);
- $r->setStatusCode(200);
- }
- catch (LuBusinessException $e)
- {
- LuLog::log($e);
- $r->setStatusCode($e->getCode());
- $r->setMessage($e->getMessage());
- }
- catch (\Exception $e)
- {
- LuLog::log($e);
- $r->setStatusCode(500);
- $r->setMessage("Internal Error");
- }
- catch (\Error $e)
- {
- LuLog::log($e);
- $r->setStatusCode(500);
- $r->setMessage("Internal Error");
- }
- return $r;
- }
-
- public function runHttp()
- {
- $httpMethod = $_SERVER['REQUEST_METHOD'];
- $url = strtok($_SERVER["REQUEST_URI"],'?');
- $parameters = array_merge($_GET, $_POST);
-
- $r = $this->dispatch($httpMethod, $url, $parameters);
- http_response_code($r->getStatusCode());
- header("content-type: application/json");
- echo $r->__toString();
- }
-
- public function runWs()
- {
- $server = IoServer::factory(
- new HttpServer(
- new WsServer(
- $this
- )
- ),
- $this->_config['websocket']['port'],
- $this->_config['websocket']['address']
- );
-
- $server->run();
- }
-
- function onMessage(ConnectionInterface $from, $msg)
- {
- // TODO: Implement onMessage() method.
- }
-
- function onOpen(ConnectionInterface $conn)
- {
- $this->_clients->attach($conn);
- }
-
- function onClose(ConnectionInterface $conn)
- {
- $this->_clients->detach($conn);
- }
-
- function onError(ConnectionInterface $conn, \Exception $e)
- {
- $conn->close();
- }
- }
|