Router.php 1.4 KB

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