asika / muse
Muse - PHP 代码生成器。她是女神,她是创造者。
Requires
- windwalker/filesystem: ~3.0
- windwalker/structure: ~3.0
- windwalker/utilities: ~3.0
Requires (Dev)
- windwalker/console: ~3.0
- windwalker/database: ~3.0
This package is auto-updated.
Last update: 2024-06-02 16:45:57 UTC
README
一个强大的 PHP 框架,通过自定义模板帮助开发者生成代码。
通过 Composer 安装
在你的 composer.json
中添加此依赖项。
{ "require": { "asika/muse": "1.*", "windwalker/console": "~2.0" } }
或者创建一个项目
php composer.phar create-project asika/muse muse 1.*
入门指南
Muse 是一个基于命令行的程序,我们将通过 CLI 来完成所有操作。请输入
php bin/muse
你会看到以下帮助信息
Muse - The Muse - version: 1
------------------------------------------------------------
[muse Help]
Muse console application.
Usage:
muse <command> [option]
Options:
-h | --help Display this help message.
-q | --quiet Do not output any message.
-v | --verbose Increase the verbosity of messages.
--ansi Set 'off' to suppress ANSI colors on unsupported terminals.
-p | --path Dest path.
-t | --tmpl Sub template name.
Commands:
gen Genarate operation.
tmpl-init Init a new template.
tmpl-convert Convert a directory and files back to a template.
通过 Acme 模板生成代码
Acme 模板是 Muse 中的默认模板,生成代码非常简单,请输入
php bin/muse gen acme test/MyApp
现在你会看到如下信息
$ php bin/muse gen acme test/MyApp File created: /var/www/muse/test/MyApp/admin/article/edit.twig File created: /var/www/muse/test/MyApp/admin/article/index.twig File created: /var/www/muse/test/MyApp/admin/category/edit.twig File created: /var/www/muse/test/MyApp/admin/category/index.twig File created: /var/www/muse/test/MyApp/article.twig File created: /var/www/muse/test/MyApp/global/index.html File created: /var/www/muse/test/MyApp/index.html File created: /var/www/muse/test/MyApp/index.twig
将你的子模板放入 Acme 模板
现在你可以将你的代码放入 src/AcmeTemplate/Template/mytmpl
。
并使用此命令生成你的子模板
php bin/muse gen acme test/MyApp2 -t mytmpl
创建你的项目模板
现在一切都非常简单,但如何创建我们自己的模板呢?我们必须编写一些代码来配置路径和变量。
初始化一个示例模板
使用此命令初始化一个新的模板。
php bin/muse tmpl-init flower
File created: /var/www/muse/src/FlowerTemplate/Action/ConvertAction.php
File created: /var/www/muse/src/FlowerTemplate/Action/CopyAllAction.php
File created: /var/www/muse/src/FlowerTemplate/Task/Convert.php
File created: /var/www/muse/src/FlowerTemplate/Task/Generate.php
File created: /var/www/muse/src/FlowerTemplate/Template/default/DefaultClass.php
File created: /var/www/muse/src/FlowerTemplate/FlowerTemplate.php
好的,我们创建了一个名为 flower
的示例模板,该模板位于 src/FlowerTemplate
,入口类为 FlowerTemplate
。实际上你可以手动创建它,但这会比较复杂,所以我们最好先使用示例。
配置变量和路径
打开 FlowerTemplate
,你可以设置替换字符串和复制路径
注册替换变量
protected $tagVariable = array('{@', '@}'); protected function registerReplaces($io, $replace = array()) { $item = $io->getOption('n', 'sakura'); /* * Replace with your code name. */ // Set item name, default is sakura $replace['item.lower'] = strtolower($item); $replace['item.upper'] = strtoupper($item); $replace['item.cap'] = ucfirst($item); // Set project name $replace['project.class'] = 'Muse'; return $replace; }
此示例意味着我们可以输入 -n {item}
来设置变量名。在模板代码中,{@item.lower@}
/{@item.upper@}
/{@item.cap@}
将被替换为项目名称。
sakura
是不提供 -n
参数时的默认值。这是一个示例,如果找不到 -n
,则退出并提示用户输入此参数
$item = $io->getOption('n') ? : exit('Please give me item using "-n {item_name}"');
你可以在 $replace
数组中添加许多字符串,记得你需要每个小写、大写和首字母大写的版本,并且不要忘记返回它们。
注册配置和路径
protected function registerConfig($io, $config) { /* * Replace with your project path. */ $subTemplate = $io->getOption('t', 'default'); $dest = $io->getArgument(1) ? : 'generated'; $config['path.src'] = __DIR__ . '/Template/' . $subTemplate; $config['path.dest'] = GENERATOR_PATH . '/' . $dest; return $config; }
你可以在此方法中设置一些有用的配置,最重要的是 path.src
和 path.dest
。这两个配置告诉 Muse 代码从哪里来,以及代码复制到哪里。
GENERATOR_PATH
是 Muse 的根路径,而 $io->getArgument(1)
表示获取命令的第二个参数(第一个是 0)。
任务和操作
我们有两个默认的任务控制器,Generate
和 Convert
。
生成任务执行代码生成操作,而转换任务可以帮助我们将代码转换回模板。在任务控制器中,我们可以使用 doAction()
来执行不同的操作来完成我们想要做的操作。
Generate
控制器类
namespace FlowerTemplate\Task; use FlowerTemplate\Action; use Muse\Controller\AbstractTaskController; class Generate extends AbstractTaskController { public function execute() { $this->doAction(new Action\CopyAllAction); } }
CopyAllAction
类
namespace FlowerTemplate\Action; use Muse\Action\AbstractAction; use Muse\FileOperator\CopyOperator; class CopyAllAction extends AbstractAction { protected function doExecute() { $copyOperator = new CopyOperator($this->io, (array) $this->config['tag.variable']); $copyOperator->copy($this->config['path.src'], $this->config['path.dest'], $this->config['replace']); } }
这两个类都非常简单,遵循单一责任原则,我们可以在一个控制器中组织多个操作,如下所示
class Generate extends AbstractTaskController { public function execute() { $this->doAction(new Action\CopyAllAction); $this->doAction(new Action\ImportSqlAction); $this->doAction(new Action\Github\CloneSomeRepoAction); $this->doAction(new Action\User\CreateNewUserAction); } }
单一操作类的优点是我们可以在不同的任务中重用每个类。
文件操作
操作类
我们目前提供了两个操作符,copyOperator
帮助我们复制代码并将标签替换为变量,convertOperator
也帮助我们复制代码,但用标签替换变量。
只需创建一个实例并使用复制方法
$copyOperator = new CopyOperator($this->io, array('{@', '@}')); $copyOperator->copy($src, $dest, $replaceArray);
未来还将有更多操作符(例如:databaseOperator
、gitOperator
)。
文件系统
有三个文件系统类:Path
、File
和Folder
,它们扩展自Windwalker文件系统包,请参阅:https://github.com/ventoviro/windwalker-filesystem
简单用法
namespace Muse\Filesystem; Filesystem\Folder::copy($src, $dest); Filesystem\Folder::move($src, $dest); Filesystem\Folder::create($path); Filesystem\Folder::delete($path); Filesystem\File::copy($src, $dest); Filesystem\File::move($src, $dest); Filesystem\File::write($path, $buffer); Filesystem\File::delete($path); // Replace / and \ to DIRECTORY_SEPARATOR $path = Filesystem\Path::clean($path);
因此,您可以在Action类中使用文件系统类来帮助您操作文件和目录。
创建一个新任务
如果您想创建一个新的任务控制器,这将需要一些步骤来创建任务。这个过程并不简单,我们将在未来简化这个过程。
(1) 创建一个新的命令
在src/Muse/Windwalker/Command/MyTask/MyTask.php
中创建一个命令类
namespace Muse\Windwalker\Command\MyTask; use Muse\Controller\GeneratorController; use Muse\Windwalker\IO; use Windwalker\Console\Command\Command; class MyTask extends Command { protected $name = 'mytask'; protected $description = 'Desc of my task.'; protected $usage = 'mytask <cmd><tmpl-name></cmd> <option>[option]</option>'; public function configure() { parent::configure(); } protected function doExecute() { $controller = new GeneratorController(new IO($this)); $controller->setTask('mytask')->execute(); } }
如何使用Windwalker控制台和命令?请参阅:https://github.com/ventoviro/windwalker-console
(2) 将您的命令注册到应用程序中
在src/Muse/Windwalker/Application::registerCommands()
中注册此命令
protected function registerCommands() { $this->addCommand(new Command\Generate\Generate); $this->addCommand(new Command\Init\Init); $this->addCommand(new Command\Convert\Convert); // Add here $this->addCommand(new Command\MyTask\Task); }
您将获得如下新的帮助信息
Available commands:
gen Genarate operation.
tmpl-init Init a new extension.
tmpl-convert Convert a code folder back to a template.
mytask Desc of my task.
(3) 创建一个新的任务控制器
在src/FlowerTemplate/Task/MyTask.php
中创建一个类
namespace FlowerTemplate\Task; use FlowerTemplate\Action; use Muse\Controller\TaskController; class MyTask extends TaskController { public function execute() { $this->doAction(new Action\CopyAllAction); } }
现在您可以在这里执行一些操作。
(4) 测试您的任务
输入此命令,您就可以进入您的任务控制器
php bin/muse mytask <arguments>
集成到您的项目或框架中
Muse可以集成到任何框架中,而不是默认的Windwalker控制台应用程序。只需创建一个IO
类来帮助Muse输入和输出一些信息
use Muse\IO\IOInterface; class MyIOAdapter implements IOInterface { // Implement this interface }
然后在您的项目入口中使用GeneratorController
(例如:Symfony控制台)
$controller = new GeneratorController(new MyIOAdapter($input, $output)); $controller->setTask($input->getArgument('task'))->execute();
非常好,代码食谱中的时间过得很快。
待办事项
- DatabaseOperator
- GitOperator
- FtpOperator
- UnitTest
- 完成docblock
- 易于添加任务控制器和命令