ninsuo/code-generator-bundle

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

This package is auto-updated.

Last update: 2024-08-21 08:23:11 UTC


README

What is it?

It's a code generator heavily inspired from the Symfony maker, developed in a web interface.

Create snippets:

  • Add your context in YAML
  • Enrich it with PHP code
  • Write down your Twig templates

Use snippets

  • Either use "Copy" buttons in the UX
  • Or use the interactive maker-like command-line

Share snippets:

  • Easily export and import snippets in the UX
  • Share snippets in a project using fixtures

intro

Check out more details and screenshots here.

Standalone install

You can install this generator standalone if needed, just download a new symfony project.

Ensure symfony cli is installed and run:

symfony new codegen --full

You'll need to set up a storage, in .env you may uncomment:

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

Bundle install

Use composer

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

or in composer.json:

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

Add the bundle

The PHP enricher is a beautiful RCE vulnerability, so please enable this bundle in development environment only!

In config/bundles.php:

<?php

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

Add the routing

In config/routes/annotations.yaml:

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

Generate schema

Run the following command:

php bin/console make:migration

Edit the generated file to ensure migration will only run on dev environment.

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

        // ...
    }

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

        // ...
    }

Then, run:

php bin/console doctrine:migrations:migrate

Run

Run symfony start and go to http://127.0.0.1:8000/snippet route

Sharing snippets

Use the import / export feature at the bottom to share snippets with crewmates.

If you are using this bundle inside a project, you can also use fixtures to share snippets:

<?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'
        );
    }
}