|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://hyperf.wiki |
| 9 | + * @contact group@hyperf.io |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | +namespace Hyperf\RpcMultiplex\Listener; |
| 13 | + |
| 14 | +use Hyperf\Event\Contract\ListenerInterface; |
| 15 | +use Hyperf\RpcMultiplex\Constant; |
| 16 | +use Hyperf\RpcServer\Event\AfterPathRegister; |
| 17 | +use Hyperf\ServiceGovernance\ServiceManager; |
| 18 | + |
| 19 | +class RegisterServiceListener implements ListenerInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var ServiceManager |
| 23 | + */ |
| 24 | + private $serviceManager; |
| 25 | + |
| 26 | + public function __construct(ServiceManager $serviceManager) |
| 27 | + { |
| 28 | + $this->serviceManager = $serviceManager; |
| 29 | + } |
| 30 | + |
| 31 | + public function listen(): array |
| 32 | + { |
| 33 | + return [ |
| 34 | + AfterPathRegister::class, |
| 35 | + ]; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * All official rpc protocols should register in here, |
| 40 | + * and the others non-official protocols should register in their own component via listener. |
| 41 | + * |
| 42 | + * @param AfterPathRegister $event |
| 43 | + */ |
| 44 | + public function process(object $event) |
| 45 | + { |
| 46 | + $annotation = $event->annotation; |
| 47 | + if (! in_array($annotation->protocol, $this->getProtocols(), true)) { |
| 48 | + return; |
| 49 | + } |
| 50 | + $metadata = $event->toArray(); |
| 51 | + $annotationArray = $metadata['annotation']; |
| 52 | + unset($metadata['path'], $metadata['annotation'], $annotationArray['name']); |
| 53 | + $this->serviceManager->register($annotation->name, $event->path, array_merge($metadata, $annotationArray)); |
| 54 | + } |
| 55 | + |
| 56 | + protected function getProtocols(): array |
| 57 | + { |
| 58 | + return [ |
| 59 | + Constant::PROTOCOL_DEFAULT, |
| 60 | + ]; |
| 61 | + } |
| 62 | +} |
0 commit comments