Advertisement
Guest User

Untitled

a guest
Mar 31st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Jsor\MysqlAsync;
  4.  
  5. use React\EventLoop\LoopInterface;
  6. use React\Promise\Deferred;
  7.  
  8. class Connection
  9. {
  10. private $loop;
  11. private $parameters;
  12. private $mysqli;
  13. private $jobs;
  14. private $connectDeferred;
  15.  
  16. public function __construct(LoopInterface $loop, array $parameters = array(), array $options = array())
  17. {
  18. $this->loop = $loop;
  19. $this->parameters = $parameters;
  20.  
  21. $this->jobs = new \SplQueue();
  22.  
  23. $this->mysqli = mysqli_init();
  24.  
  25. foreach ($options as $name => $option) {
  26. $this->mysqli->options($name, $option);
  27. }
  28. }
  29.  
  30. public function connect()
  31. {
  32. if ($this->connectDeferred) {
  33. return $this->connectDeferred->promise();
  34. }
  35.  
  36. $this->connectDeferred = new Deferred();
  37.  
  38. $host = isset($this->parameters['host']) ? $this->parameters['host'] : null;
  39. $username = isset($this->parameters['username']) ? $this->parameters['username'] : null;
  40. $password = isset($this->parameters['password']) ? $this->parameters['password'] : null;
  41. $dbname = isset($this->parameters['dbname']) ? $this->parameters['dbname'] : null;
  42. $port = isset($this->parameters['port']) ? $this->parameters['port'] : null;
  43. $socket = isset($this->parameters['socket']) ? $this->parameters['socket'] : null;
  44.  
  45. @$this->mysqli->real_connect($host, $username, $password, $dbname, $port, $socket);
  46.  
  47. if ($this->mysqli->connect_error) {
  48. $this->connectDeferred->reject($this->mysqli->connect_error);
  49. } else {
  50. $this->connectDeferred->resolve($this);
  51. }
  52.  
  53. return $this->connectDeferred->promise();
  54. }
  55.  
  56. public function query($sql)
  57. {
  58. $job = (object) array(
  59. 'type' => 'query',
  60. 'sql' => $sql
  61. );
  62.  
  63. return $this
  64. ->connect()
  65. ->then(function ($connection) use ($job) {
  66. return $connection->enqueue($job);
  67. });
  68. }
  69.  
  70. public function end()
  71. {
  72. $job = (object) array(
  73. 'type' => 'close'
  74. );
  75.  
  76. return $this
  77. ->connect()
  78. ->then(function ($connection) use ($job) {
  79. return $connection->enqueue($job);
  80. });
  81. }
  82.  
  83. public function enqueue($job)
  84. {
  85. $deferred = new Deferred();
  86.  
  87. $job->resolver = $deferred;
  88.  
  89. $this->jobs->enqueue($job);
  90.  
  91. if (1 === count($this->jobs)) {
  92. $this->start();
  93. }
  94.  
  95. return $deferred->promise();
  96. }
  97.  
  98. protected function start()
  99. {
  100. if ($this->jobs->isEmpty()) {
  101. return;
  102. }
  103.  
  104. $job = $this->jobs->bottom();
  105.  
  106. switch ($job->type) {
  107. case 'query':
  108. $this->mysqli->query($job->sql, \MYSQLI_ASYNC);
  109. $this->poll();
  110. break;
  111. case 'close':
  112. if ($this->mysqli->close()) {
  113. $job->resolver->resolve();
  114. } else {
  115. $job->resolver->reject(mysqli_error($this->mysqli));
  116. }
  117.  
  118. $this->dequeue();
  119. break;
  120. }
  121. }
  122.  
  123. public function poll()
  124. {
  125. $links = $errors = $reject = array($this->mysqli);
  126.  
  127. if (0 === mysqli_poll($links, $errors, $reject, 1)) {
  128. $this->loop->addTimer(0.001, array($this, 'poll'));
  129. return;
  130. }
  131.  
  132. $job = $this->jobs->bottom();
  133. $result = $links[0]->reap_async_query();
  134.  
  135. if ($result) {
  136. $job->resolver->resolve($result);
  137. } else {
  138. $job->resolver->reject(mysqli_error($links[0]));
  139. }
  140.  
  141. $this->dequeue();
  142. }
  143.  
  144. protected function dequeue()
  145. {
  146. $this->jobs->dequeue();
  147.  
  148. if ($this->jobs->isEmpty()) {
  149. //$this->emit('drain');
  150. } else {
  151. $this->start();
  152. }
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement