首页 > 解决方案 > 如何自定义 Symfony MakerBundle

问题描述

我正在尝试自定义maker:crud

官方文档和Symfony MakerBundle表明“您可以创建自己的 make: ... 命令重用此包提供的工具。为此,您应该在 src/Maker/ 目录中创建一个扩展 AbstractMaker 的类”。

所以我的第一步是从存储库中复制,这也在文档中指出,脚本MakeCrud.phpsrc/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');
    }

标签: phpsymfony

解决方案


您可以使用作曲家对其进行修补:

  • 建立一个目录./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"
    ]
},

你可以修改文件

这里的一个例子


推荐阅读