Router.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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\RpcServer\Router;
  12. /**
  13. * @method static void addRoute(string $route, $handler, array $options = [])
  14. * @method static void addGroup($prefix, callable $callback)
  15. */
  16. class Router
  17. {
  18. protected static string $serverName = 'rpc';
  19. protected static ?DispatcherFactory $factory = null;
  20. public static function __callStatic($name, $arguments)
  21. {
  22. $router = static::$factory->getRouter(static::$serverName);
  23. return $router->{$name}(...$arguments);
  24. }
  25. public static function addServer(string $serverName, callable $callback)
  26. {
  27. $temp = $serverName;
  28. static::$serverName = $serverName;
  29. $callback();
  30. static::$serverName = $temp;
  31. unset($temp);
  32. }
  33. public static function init(DispatcherFactory $factory)
  34. {
  35. static::$factory = $factory;
  36. }
  37. public static function add(string $route, $handler, array $options = []): void
  38. {
  39. static::addRoute($route, $handler, $options);
  40. }
  41. }