php - 如何自定义 Symfony MakerBundle
问题描述
我正在尝试自定义maker:crud。
官方文档和Symfony MakerBundle表明“您可以创建自己的 make: ... 命令重用此包提供的工具。为此,您应该在 src/Maker/ 目录中创建一个扩展 AbstractMaker 的类”。
所以我的第一步是从存储库中复制,这也在文档中指出,脚本MakeCrud.php在src/Maker/MakeCustomCrud.php(我重命名它)。我应用了一些基本更改,例如重命名类和命令名称。
准备好可用命令后,我正在等待在命令之间查看make:custom-crud。显然,当试图调用它(php bin/console make:custom-crud)时,我被告知命令“make:custom-crud”未定义。 我做错了什么,我不知道它是什么。
我在src/Maker/MakeCustomCrud.php下面转录部分代码:
<?php
/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\MakerBundle\Maker;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Common\Inflector\Inflector;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Renderer\FormTypeRenderer;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Validator;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Csrf\CsrfTokenManager;
use Symfony\Component\Validator\Validation;
/**
* @author Sadicov Vladimir <sadikoff@gmail.com>
*/
final class MakeCustomCrud extends AbstractMaker
{
private $doctrineHelper;
private $formTypeRenderer;
public function __construct(DoctrineHelper $doctrineHelper, FormTypeRenderer $formTypeRenderer)
{
$this->doctrineHelper = $doctrineHelper;
$this->formTypeRenderer = $formTypeRenderer;
}
public static function getCommandName(): string
{
return 'make:custom-crud';
}
/**
* {@inheritdoc}
*/
public function configureCommand(Command $command, InputConfiguration $inputConfig)
{
$command
->setDescription('Creates Custom CRUD for Doctrine entity class')
->addArgument('entity-class', InputArgument::OPTIONAL, sprintf('The class name of the entity to create Custom CRUD (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())))
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeCrud.txt'))
;
$inputConfig->setArgumentAsNonInteractive('entity-class');
}
解决方案
您可以使用作曲家对其进行修补:
- 建立一个目录
./patch
- 复制供应商目录的文件
./vendor/symfony/maker--bundle/src/Resources/skeleton/form/Type.tpl.php
并添加到您的composer.json
例子:
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"patch": [
"cp patch/patch_crud/Type.tpl.php vendor/symfony/maker-bundle/src/Resources/skeleton/form/."
],
"post-install-cmd": [
[
"@patch",
"@auto-scripts"
]
],
"post-update-cmd": [
"@patch",
"@auto-scripts"
]
},
你可以修改文件
这里的一个例子
推荐阅读
- java - 在 Spring Controller 中捕获无效参数
- rust - 如何为 UnixStream 和 TcpStream 创建多态类型
- postgresql - 不明确的列引用 - 它可以引用 PL/pgSQL 变量或表列?
- raspberry-pi - GPSD 没有看到完整的数据
- reactjs - 如何在 Azure 上部署 NextJs SSR React 应用程序
- javascript - JavaScript modal 只会在第一次打开后关闭,但不会在第二次打开后关闭
- paypal - PayPal webhook 状态从“待处理”变为成功需要发生什么?
- python - 滚动您自己的自定义容器 docker 映像:如何为本地系统 GPU 创建用户定义的命令
- c# - 使用 C# 和 SQL 解决 datagridview 中重复条目的最佳方法是什么?
- python - 混淆Python中的属性或方法