Files
kiri-core/Annotation/Route/Socket.php
T

53 lines
987 B
PHP
Raw Normal View History

2020-12-14 19:03:05 +08:00
<?php
2020-12-15 12:24:38 +08:00
namespace Annotation\Route;
2020-12-14 19:03:05 +08:00
2021-03-03 18:35:04 +08:00
use Annotation\Attribute;
2021-04-19 16:33:58 +08:00
use Exception;
2021-08-17 16:43:50 +08:00
use Http\Route\Router;
2021-08-11 01:04:57 +08:00
use Kiri\Kiri;
2020-12-14 19:03:05 +08:00
2020-12-15 12:24:38 +08:00
/**
* Class Socket
* @package Annotation
*/
2021-03-03 18:35:04 +08:00
#[\Attribute(\Attribute::TARGET_METHOD)] class Socket extends Attribute
2020-12-14 19:03:05 +08:00
{
2020-12-15 12:24:38 +08:00
const CLOSE = 'CLOSE';
const MESSAGE = 'MESSAGE';
const HANDSHAKE = 'HANDSHAKE';
2020-12-14 19:03:05 +08:00
/**
2020-12-15 12:24:38 +08:00
* Socket constructor.
* @param string $event
* @param string|null $uri
2021-03-03 18:35:04 +08:00
* @param string $version
2020-12-14 19:03:05 +08:00
*/
2021-06-25 11:20:38 +08:00
public function __construct(public string $event, public ?string $uri = null, public string $version = 'v.1.0')
2020-12-14 19:03:05 +08:00
{
}
/**
2021-06-25 11:20:38 +08:00
* @param mixed $class
* @param mixed|null $method
2021-02-22 17:44:24 +08:00
* @return Router
2021-04-19 16:33:58 +08:00
* @throws Exception
2020-12-14 19:03:05 +08:00
*/
2021-06-25 11:20:38 +08:00
public function execute(mixed $class, mixed $method = null): Router
2020-12-14 19:03:05 +08:00
{
// TODO: Implement setHandler() method.
2021-08-11 01:04:57 +08:00
$router = Kiri::app()->getRouter();
2020-12-14 19:03:05 +08:00
2021-05-03 04:08:55 +08:00
$path = $this->event . '::' . (is_null($this->uri) ? 'event' : $this->uri);
2020-12-15 16:55:50 +08:00
2021-08-13 15:19:07 +08:00
$router->addRoute($path, [di($class), $method], 'sw::socket');
2021-02-22 17:44:24 +08:00
return $router;
2020-12-14 19:03:05 +08:00
}
}