Files
kiri-core/kiri-gii/GiiJsonRpc.php
T

114 lines
2.0 KiB
PHP
Raw Normal View History

2021-10-28 11:09:14 +08:00
<?php
namespace Gii;
class GiiJsonRpc extends GiiBase
{
/**
* @return array
*/
public function create(): array
{
return [
$this->createInterface($this->input->getArgument('name')),
$this->createProducers($this->input->getArgument('name')),
$this->createConsumer($this->input->getArgument('name')),
];
}
2021-10-28 16:33:19 +08:00
private function createInterface($name): string
2021-10-28 11:09:14 +08:00
{
$html = '<?php
namespace Rpc;
2021-10-28 16:58:15 +08:00
2021-10-28 11:09:14 +08:00
interface ' . ucfirst($name) . 'RpcInterface
{
2021-10-29 17:19:57 +08:00
}';
2021-10-28 11:09:14 +08:00
$name = ucfirst($name) . 'RpcInterface.php';
if (!is_dir(APP_PATH . '/rpc/')) {
mkdir(APP_PATH . '/rpc/');
}
file_put_contents(APP_PATH . '/rpc/' . $name, $html);
return $name;
}
2021-10-28 11:26:58 +08:00
private function createProducers($name): string
2021-10-28 11:09:14 +08:00
{
$html = '<?php
2021-10-28 11:26:58 +08:00
namespace Rpc\Producers;
2022-01-08 18:49:08 +08:00
use Kiri\Annotation\Target;
use Kiri\Annotation\Mapping;
2021-10-28 11:09:14 +08:00
use Rpc\\' . ucfirst($name) . 'RpcInterface;
2021-10-28 16:33:19 +08:00
use Exception;
use Kiri\Rpc\JsonRpcConsumers;
2021-10-28 11:09:14 +08:00
#[Target]
2021-10-28 17:11:20 +08:00
#[Mapping(' . ucfirst($name) . 'RpcInterface::class)]
2021-10-28 16:33:19 +08:00
class ' . ucfirst($name) . 'RpcService extends JsonRpcConsumers implements ' . ucfirst($name) . 'RpcInterface
2021-10-28 11:09:14 +08:00
{
2021-10-28 17:31:19 +08:00
protected string $name = \'' . $name . '\';
2021-10-28 11:09:14 +08:00
2021-10-29 17:19:57 +08:00
}';
2021-10-28 11:09:14 +08:00
2021-10-28 16:35:52 +08:00
$name = ucfirst($name) . 'RpcService.php';
2021-10-28 11:09:14 +08:00
if (!is_dir(APP_PATH . '/rpc/Producers/')) {
mkdir(APP_PATH . '/rpc/Producers/');
}
file_put_contents(APP_PATH . '/rpc/Producers/' . $name, $html);
return $name;
}
2021-10-28 11:26:58 +08:00
private function createConsumer($name): string
2021-10-28 11:09:14 +08:00
{
$html = '<?php
2021-10-28 11:26:58 +08:00
namespace Rpc\Consumers;
2022-01-08 18:49:08 +08:00
use Kiri\Annotation\Target;
use Kiri\Rpc\Annotation\JsonRpc;
2021-10-28 16:33:19 +08:00
use Http\Handler\Controller;
2021-10-28 11:09:14 +08:00
use Rpc\\' . ucfirst($name) . 'RpcInterface;
#[Target]
2021-10-29 17:19:57 +08:00
#[JsonRpc(service: \'' . $name . '\', version: \'2.0\')]
2021-10-28 16:33:19 +08:00
class ' . ucfirst($name) . 'RpcConsumer extends Controller implements ' . ucfirst($name) . 'RpcInterface
2021-10-28 11:09:14 +08:00
{
2021-10-29 17:19:57 +08:00
}';
2021-10-28 11:09:14 +08:00
$name = ucfirst($name) . 'RpcConsumer.php';
if (!is_dir(APP_PATH . '/rpc/Consumers/')) {
mkdir(APP_PATH . '/rpc/Consumers/');
}
file_put_contents(APP_PATH . '/rpc/Consumers/' . $name, $html);
return $name;
}
}