ninsuo/code-generator-bundle

dev-main 2022-01-21 02:54 UTC

This package is auto-updated.

Last update: 2024-09-21 08:28:19 UTC


README

这是什么?

这是一个受Symfony maker强烈启发的代码生成器,在一个网络界面中开发。

创建代码片段

  • 在YAML中添加您的上下文
  • 用PHP代码丰富它
  • 写下您的Twig模板

使用代码片段

  • 要么使用UX中的"复制"按钮
  • 要么使用交互式maker-like命令行

分享代码片段

  • 轻松在UX中导出和导入代码片段
  • 使用固定值在项目中分享代码片段

intro

更多信息及截图请访问这里

独立安装

如果需要,您可以独立安装此生成器,只需下载一个新的symfony项目。

确保symfony cli已安装并运行

symfony new codegen --full

您需要设置一个存储,在.env中可能需要取消注释

# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"

安装包

使用composer

composer require --dev ninsuo/code-generator-bundle

composer.json

{
  "require": {
    "ninsuo/code-generator-bundle": "dev-main"
  }
}

添加包

PHP丰富器是一个美丽的RCE漏洞,因此请仅在开发环境中启用此包!

config/bundles.php

<?php

return [
    // ...
    Bundles\CodeGeneratorBundle\CodeGeneratorBundle::class => ['dev' => true],
];

添加路由

config/routes/annotations.yaml

snippet:
  resource: '@CodeGeneratorBundle/Controller/'
  type: annotation

生成模式

运行以下命令

php bin/console make:migration

编辑生成的文件以确保迁移仅在开发环境中运行。

    public function up(Schema $schema): void
    {
        if ('dev' !== $_ENV['APP_ENV']) {
            return;
        }

        // ...
    }

    public function down(Schema $schema): void
    {
        if ('dev' !== $_ENV['APP_ENV']) {
            return;
        }

        // ...
    }

然后,运行

php bin/console doctrine:migrations:migrate

运行

运行symfony start并访问http://127.0.0.1:8000/snippet路由

分享代码片段

使用底部的导入/导出功能与团队成员分享代码片段。

如果您在此项目中使用此包,您还可以使用固定值来分享代码片段。

<?php

namespace App\Test\DataFixtures;

use Bundles\CodeGeneratorBundle\Repository\SnippetRepository;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

class SnippetFixtures extends Fixture
{
    private SnippetRepository $snippetRepository;

    public function __construct(SnippetRepository $snippetRepository)
    {
        $this->snippetRepository = $snippetRepository;
    }

    public function load(ObjectManager $manager)
    {
        $this->snippetRepository->import(
            'eyJuIjoiSGVsbG8sIHdvcmxkIiwiYyI6ImZvbzogYmFyIiwiZSI6IiRjb250ZXh0WydiYXonXSA9IHN0cnRvdXBwZXIoJGNvbnRleHRbJ2ZvbyddKTsiLCJmIjpbeyJkIjoiaWYgeW91IHNlYXJjaCBmb3IgbWUsIGknbSBhdCB0aGUge3sgYmF6IH19IiwidCI6IkhlbGxvLCB7eyBmb28gfX0ifV19'
        );
    }
}