ExtEventLoop.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman\Events\React;
  15. /**
  16. * Class ExtEventLoop
  17. * @package Workerman\Events\React
  18. */
  19. class ExtEventLoop extends \React\EventLoop\ExtEventLoop
  20. {
  21. /**
  22. * Event base.
  23. *
  24. * @var EventBase
  25. */
  26. protected $_eventBase = null;
  27. /**
  28. * All signal Event instances.
  29. *
  30. * @var array
  31. */
  32. protected $_signalEvents = array();
  33. /**
  34. * Construct
  35. */
  36. public function __construct()
  37. {
  38. parent::__construct();
  39. $class = new \ReflectionClass('\React\EventLoop\ExtEventLoop');
  40. $property = $class->getProperty('eventBase');
  41. $property->setAccessible(true);
  42. $this->_eventBase = $property->getValue($this);
  43. }
  44. /**
  45. * Add signal handler.
  46. *
  47. * @param $signal
  48. * @param $callback
  49. * @return bool
  50. */
  51. public function addSignal($signal, $callback)
  52. {
  53. $event = \Event::signal($this->_eventBase, $signal, $callback);
  54. if (!$event||!$event->add()) {
  55. return false;
  56. }
  57. $this->_signalEvents[$signal] = $event;
  58. }
  59. /**
  60. * Remove signal handler.
  61. *
  62. * @param $signal
  63. */
  64. public function removeSignal($signal)
  65. {
  66. if (isset($this->_signalEvents[$signal])) {
  67. $this->_signalEvents[$signal]->del();
  68. unset($this->_signalEvents[$signal]);
  69. }
  70. }
  71. }