Commit 95bd7d78 by feixiang

feat: gen controller command

parent 126c8dda
./*
\ No newline at end of file
<?php
namespace {{NAMESPACE}};
use Swoft\Http\Server\Bean\Annotation\Controller;
use Swoft\Http\Server\Bean\Annotation\RequestMapping;
use Swoft\Http\Server\Bean\Annotation\RequestMethod;
/**
* @Controller(prefix="")
*/
class {{CLASS_NAME}} extends BaseController
{
/**
* @RequestMapping(route="", method="")
*
* @return void
*/
public function index()
{
}
}
\ No newline at end of file
<?php
/**
* This file is part of Swoft.
*
* @link https://swoft.org
* @document https://doc.swoft.org
* @contact group@swoft.org
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/
namespace App\Commands\Common\Generate;
use Swoft\Console\Bean\Annotation\Command;
use Swoft\Console\Bean\Annotation\Mapping;
use Swoft\Console\Input\Input;
use Swoft\Console\Output\Output;
/**
* generate controller layer commands
*
* @Command(name="gen-control",coroutine=false)
*/
class GenerateControlCommand
{
private $templatesPath = "/Commands/Common/Generate/ClassTemplate";
/**
* this generate controller command
*
* @Usage
* generate:controller [options]
*
* @Options
* -c,--c the controller name with namespace
*
* @Example
* php swoft gen-control:controller -c "App\Controllers\V1\DemoController"
*
* @param Input $input
* @param Output $output
*
* @Mapping("controller")
*/
public function generateController(Input $input, Output $output)
{
$controllerName = $input->getOpt('c');
if (empty($controllerName)) {
$output->writeln("please input option -c", true, true);
}
$controllerParams = explode('\\', $controllerName);
$controllerDir = alias("@app");
$namespace = null;
$fileName = array_pop($controllerParams);
foreach( $controllerParams as $param ) {
if ($param == "App") {
$namespace = $param;
continue;
}
$controllerDir .= '/'.$param;
$namespace .= '\\'.$param;
}
if ( ! is_dir($controllerDir) ) {
mkdir($controllerDir, 0777);
}
$appPath = alias("@app");
$controllerPath = $controllerDir.'/'.$fileName.'.php';
$templateContent = file_get_contents($appPath . $this->templatesPath . '/Controller/classTemplate');
$templateContent = str_replace(
[
"{{NAMESPACE}}",
"{{CLASS_NAME}}"
],
[
$namespace,
ucfirst($fileName),
],
$templateContent
);
file_put_contents($controllerPath, $templateContent);
}
}
\ No newline at end of file
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE * @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/ */
namespace App\Commands\Common; namespace App\Commands\Common\Generate;
use Swoft\Console\Bean\Annotation\Command; use Swoft\Console\Bean\Annotation\Command;
use Swoft\Console\Bean\Annotation\Mapping; use Swoft\Console\Bean\Annotation\Mapping;
...@@ -18,10 +18,13 @@ use Swoft\Console\Output\Output; ...@@ -18,10 +18,13 @@ use Swoft\Console\Output\Output;
/** /**
* generate dao,data layer commands * generate dao,data layer commands
* *
* @Command(coroutine=false) * @Command(name="gen-models",coroutine=false)
*/ */
class GenerateCommand class GenerateModelsCommand
{ {
private $templatesPath = "/Commands/Common/Generate/ClassTemplate";
/** /**
* this generate command * this generate command
* *
...@@ -33,7 +36,7 @@ class GenerateCommand ...@@ -33,7 +36,7 @@ class GenerateCommand
* -en,--en the entity name * -en,--en the entity name
* *
* @Example * @Example
* php swoft generate:dao -e entityName [-n daoName] * php swoft gen-models:dao -e entityName [-n daoName]
* *
* @param Input $input * @param Input $input
* @param Output $output * @param Output $output
...@@ -59,7 +62,7 @@ class GenerateCommand ...@@ -59,7 +62,7 @@ class GenerateCommand
* -e,--e the entity name * -e,--e the entity name
* *
* @Example * @Example
* php swoft generate:dao -e entityName [-n dataName] [--dao-name daoName] * php swoft gen-models:dao -e entityName [-n dataName] [--dao-name daoName]
* *
* @param Input $input * @param Input $input
* @param Output $output * @param Output $output
...@@ -81,7 +84,7 @@ class GenerateCommand ...@@ -81,7 +84,7 @@ class GenerateCommand
//generateData //generateData
$dataPath = $appPath.'/Models/Data/'.$dataName.'.php'; $dataPath = $appPath.'/Models/Data/'.$dataName.'.php';
$templatePath = $appPath.'/Commands/Common/ClassTemplate/Data/ClassTemplate'; $templatePath = $appPath . $this->templatesPath.'/Data/ClassTemplate';
$content = file_get_contents($templatePath); $content = file_get_contents($templatePath);
preg_match_all('/(?<={)\w+(?=})/', $content, $vars); preg_match_all('/(?<={)\w+(?=})/', $content, $vars);
...@@ -119,7 +122,7 @@ class GenerateCommand ...@@ -119,7 +122,7 @@ class GenerateCommand
$daoPath = alias("@app").'/Models/Dao/'.$daoName.'.php'; $daoPath = alias("@app").'/Models/Dao/'.$daoName.'.php';
$templatePath = alias("@app").'/Commands/Common/ClassTemplate/Dao/ClassTemplate'; $templatePath = alias("@app") . $this->templatesPath.'/Dao/ClassTemplate';
$content = file_get_contents($templatePath); $content = file_get_contents($templatePath);
...@@ -169,4 +172,5 @@ class GenerateCommand ...@@ -169,4 +172,5 @@ class GenerateCommand
return $content; return $content;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment