Skip to content

Commit

Permalink
重构控制器生成命令,使用原生注解
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Oct 25, 2023
1 parent 7945f8e commit 0929b1b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 52 deletions.
9 changes: 8 additions & 1 deletion src/Server/Http/Cli/ControllerGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

namespace Imi\Server\Http\Cli;

use Imi\Bean\Util\AttributeUtil;
use Imi\Cli\Annotation\Argument;
use Imi\Cli\Annotation\Command;
use Imi\Cli\Annotation\CommandAction;
use Imi\Cli\Annotation\Option;
use Imi\Cli\ArgType;
use Imi\Cli\Contract\BaseCommand;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\View\Annotation\View;
use Imi\Util\File;
use Imi\Util\Imi;

Expand Down Expand Up @@ -37,7 +40,11 @@ public function generate(string $name, string $namespace, ?string $prefix, ?stri
{
$prefix = '/' . $name . '/';
}
$data = compact('name', 'namespace', 'prefix', 'render', 'override');
$classAttributesCode = AttributeUtil::generateAttributesCode([
new Controller(prefix: $prefix),
new View(renderType: $render),
]);
$data = compact('name', 'namespace', 'prefix', 'render', 'override', 'classAttributesCode');
if ($rest)
{
$content = $this->renderTemplate($data, 'restTemplate');
Expand Down
71 changes: 32 additions & 39 deletions src/Server/Http/Cli/restTemplate.tpl
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
<?='<?php'; ?>
<?php declare(strict_types=1);
echo '<?php'; ?>

declare(strict_types=1);

namespace <?= $namespace; ?>;
namespace <?php echo $namespace; ?>;

use Imi\Controller\HttpController;
use Imi\Server\Http\Controller\HttpController;
use Imi\Server\View\Annotation\View;
use Imi\Server\Http\Route\Annotation\Route;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;

/**
* @Controller("<?= $prefix; ?>")
* @View(renderType="<?= $render; ?>")
*/
class <?= $name; ?> extends HttpController
<?php echo $classAttributesCode; ?>

class <?php echo $name; ?> extends HttpController
{
/**
* query
*
* @Action
* @Route(url="", method={"GET"})
* @return void
*/
public function query()
#[
Action,
Route(url: '', method=['GET'])
]
public function query(): array
{
return [1, 2, 3];
}

/**
* find
*
* @Action
* @Route(url="./{id}", method={"GET"})
*
* @param int $id
* @return void
*/
public function find($id)
#[
Action,
Route(url: './{id}', method=['GET'])
]
public function find(int $id): array
{
return [
'id' => $id,
Expand All @@ -46,12 +43,12 @@ class <?= $name; ?> extends HttpController

/**
* create
*
* @Action
* @Route(url="", method={"POST"})
* @return void
*/
public function create()
#[
Action,
Route(url: '', method=['POST'])
]
public function create(): array
{
return [
'operation' => 'create',
Expand All @@ -62,14 +59,12 @@ class <?= $name; ?> extends HttpController

/**
* update
*
* @Action
* @Route(url="./{id}", method={"PUT"})
*
* @param int $id
* @return void
*/
public function update($id)
#[
Action,
Route(url: './{id}', method=['PUT'])
]
public function update(int $id): array
{
return [
'id' => $id,
Expand All @@ -80,14 +75,12 @@ class <?= $name; ?> extends HttpController

/**
* delete
*
* @Action
* @Route(url="./{id}", method={"DELETE"})
*
* @param int $id
* @return void
*/
public function delete($id)
#[
Action,
Route(url: './{id}', method=['DELETE'])
]
public function delete(int $id): array
{
return [
'id' => $id,
Expand Down
21 changes: 9 additions & 12 deletions src/Server/Http/Cli/template.tpl
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
<?='<?php'; ?>
<?php declare(strict_types=1);
echo '<?php'; ?>

declare(strict_types=1);

namespace <?= $namespace; ?>;
namespace <?php echo $namespace; ?>;

use Imi\Controller\HttpController;
use Imi\Server\Http\Controller\HttpController;
use Imi\Server\View\Annotation\View;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;

/**
* @Controller("<?= $prefix; ?>")
* @View(renderType="<?= $render; ?>")
*/
class <?= $name; ?> extends HttpController
<?php echo $classAttributesCode; ?>

class <?php echo $name; ?> extends HttpController
{
/**
* index
*
* @Action
* @return void
*/
public function index()
#[Action]
public function index(): array
{
return ['success'=>true];
}
Expand Down

0 comments on commit 0929b1b

Please sign in to comment.