Commit a70c5e94 by feixiang

命令行生成 data dao 优化

parent d88619db
...@@ -12,10 +12,10 @@ use Hdll\Services\Common\Exception\CommonException; ...@@ -12,10 +12,10 @@ use Hdll\Services\Common\Exception\CommonException;
/** /**
* @Bean() * @Bean()
* Class {className} * Class {daoName}
* @package App\Models\Dao * @package App\Models\Dao
*/ */
class {className} class {daoName}
{ {
public function create(array $info) : {entityName} public function create(array $info) : {entityName}
......
...@@ -30,7 +30,7 @@ class GenerateCommand ...@@ -30,7 +30,7 @@ class GenerateCommand
* *
* @Options * @Options
* -n,--n the dao name * -n,--n the dao name
* -e,--e the entity name * -en,--en the entity name
* *
* @Example * @Example
* php swoft generate:dao -e entityName [-n daoName] * php swoft generate:dao -e entityName [-n daoName]
...@@ -83,9 +83,23 @@ class GenerateCommand ...@@ -83,9 +83,23 @@ class GenerateCommand
$dataPath = $appPath.'/Models/Data/'.$dataName.'.php'; $dataPath = $appPath.'/Models/Data/'.$dataName.'.php';
$templatePath = $appPath.'/Commands/Common/ClassTemplate/Data/ClassTemplate'; $templatePath = $appPath.'/Commands/Common/ClassTemplate/Data/ClassTemplate';
$content = file_get_contents($templatePath); $content = file_get_contents($templatePath);
$content = str_replace('{dataName}', $dataName, $content);
$content = str_replace("{daoName}", $daoName, $content); preg_match_all('/(?<={)\w+(?=})/', $content, $vars);
$content = str_replace("{varDaoName}", lcfirst($daoName), $content); $vars = isset($vars[0])?$vars[0]:[];
foreach ( $vars as $var ) {
if ( strpos($var, 'var') !== false ) {
$name = str_replace('var', '', $var);
$name = lcfirst($name);
$$var = lcfirst($$name);
var_dump($var, $$var);
}
$content = str_replace("{{$var}}", $$var,$content);
}
$dataPath = file_exists($dataPath)?$dataPath.'.gen':$dataPath;
file_put_contents($dataPath, $content); file_put_contents($dataPath, $content);
...@@ -109,12 +123,50 @@ class GenerateCommand ...@@ -109,12 +123,50 @@ class GenerateCommand
$content = file_get_contents($templatePath); $content = file_get_contents($templatePath);
$content = str_replace("{className}", $daoName,$content); preg_match_all('/(?<={)\w+(?=})/', $content, $vars);
$vars = isset($vars[0])?$vars[0]:[];
foreach ( $vars as $var ) {
if ( strpos($var, 'var') !== false ) {
$name = str_replace('var', '', $var);
$name = lcfirst($name);
$$var = lcfirst($$name);
var_dump($var, $$var);
}
$content = str_replace("{entityName}", $entityName, $content); $content = str_replace("{{$var}}", $$var,$content);
$content = str_replace("{varEntityName}", lcfirst($entityName), $content);
}
$daoPath = file_exists($daoPath)?$daoPath.'.gen':$daoPath;
file_put_contents($daoPath,$content); file_put_contents($daoPath,$content);
} }
/**
* 解析模板中变量
*
* @param string $content
* @return string
*/
private function parseVar(string $content):string
{
preg_match_all('/(?<={)\w+(?=})/', $content, $vars);
$vars = isset($vars[0])?$vars[0]:[];
foreach ( $vars as $var ) {
if ( strpos($var, 'var') !== false ) {
$name = str_replace('var', '', $var);
$name = lcfirst($name);
$$var = lcfirst($$name);
var_dump($var, $$var);
}
$content = str_replace("{{$var}}", $$var,$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