Factory.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace React\EventLoop;
  3. /**
  4. * [Deprecated] The `Factory` class exists as a convenient way to pick the best available event loop implementation.
  5. *
  6. * @deprecated 1.2.0 See Loop instead.
  7. * @see Loop
  8. */
  9. final class Factory
  10. {
  11. /**
  12. * [Deprecated] Creates a new event loop instance
  13. *
  14. * ```php
  15. * // deprecated
  16. * $loop = React\EventLoop\Factory::create();
  17. *
  18. * // new
  19. * $loop = React\EventLoop\Loop::get();
  20. * ```
  21. *
  22. * This method always returns an instance implementing `LoopInterface`,
  23. * the actual event loop implementation is an implementation detail.
  24. *
  25. * This method should usually only be called once at the beginning of the program.
  26. *
  27. * @deprecated 1.2.0 See Loop::get() instead.
  28. * @see Loop::get()
  29. *
  30. * @return LoopInterface
  31. */
  32. public static function create()
  33. {
  34. $loop = self::construct();
  35. Loop::set($loop);
  36. return $loop;
  37. }
  38. /**
  39. * @internal
  40. * @return LoopInterface
  41. */
  42. private static function construct()
  43. {
  44. // @codeCoverageIgnoreStart
  45. if (\function_exists('uv_loop_new')) {
  46. // only use ext-uv on PHP 7
  47. return new ExtUvLoop();
  48. }
  49. if (\class_exists('libev\EventLoop', false)) {
  50. return new ExtLibevLoop();
  51. }
  52. if (\class_exists('EvLoop', false)) {
  53. return new ExtEvLoop();
  54. }
  55. if (\class_exists('EventBase', false)) {
  56. return new ExtEventLoop();
  57. }
  58. if (\function_exists('event_base_new') && \PHP_MAJOR_VERSION === 5) {
  59. // only use ext-libevent on PHP 5 for now
  60. return new ExtLibeventLoop();
  61. }
  62. return new StreamSelectLoop();
  63. // @codeCoverageIgnoreEnd
  64. }
  65. }