php 如何定制Symfony MakerBundle

ddrv8njm  于 2022-11-21  发布在  PHP
关注(0)|答案(2)|浏览(129)

我正在尝试自定义 maker:crud
官方文档和The Symfony MakerBundle表明“* 您可以创建自己的make:...命令来重新使用此捆绑包提供的工具。为此,您应该在src/Maker/ directory*"中创建一个扩展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');
    }
yb3bgrhw

yb3bgrhw1#

您可以使用composer对其进行修补:

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

您可以修改该文件
here的一个例子

eimct9ow

eimct9ow2#

我没有注意到我的问题的答案早就被回答了,我要感谢@Barthy和@ Cadot.eu做了这件事。
我最近有机会把他们的建议付诸实践,它创造了奇迹。我已经能够用symfony做很多CRUD,这节省了我很多时间。Symfony的默认CRUD是可行的,但相当简单的UI明智的。我已经能够适应这一代做Bootstrap 5和Omines/Datatables。一个美丽的。
这里只需要说明一下,对于基于Windows的开发环境,我必须安装slowprog/composer-copy-file
非常非常感谢!

相关问题