PcntlScanHandler.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Di\ScanHandler;
  12. use Hyperf\Di\Exception\Exception;
  13. class PcntlScanHandler implements ScanHandlerInterface
  14. {
  15. public function __construct()
  16. {
  17. if (! extension_loaded('pcntl')) {
  18. throw new Exception('Missing pcntl extension.');
  19. }
  20. if (extension_loaded('grpc')) {
  21. $grpcForkSupport = ini_get_all('grpc')['grpc.enable_fork_support']['local_value'];
  22. $grpcForkSupport = strtolower(trim(str_replace('0', '', $grpcForkSupport)));
  23. if (in_array($grpcForkSupport, ['', 'off', 'false'], true)) {
  24. throw new Exception(' Grpc fork support must be enabled before the server starts, please set grpc.enable_fork_support = 1 in your php.ini.');
  25. }
  26. }
  27. }
  28. public function scan(): Scanned
  29. {
  30. $pid = pcntl_fork();
  31. if ($pid == -1) {
  32. throw new Exception('The process fork failed');
  33. }
  34. if ($pid) {
  35. pcntl_wait($status);
  36. if ($status !== 0) {
  37. exit(-1);
  38. }
  39. return new Scanned(true);
  40. }
  41. return new Scanned(false);
  42. }
  43. }