symfony / maker-bundle

Symfony Maker Bundle
https://symfony.com/
MIT License
3.35k stars 406 forks source link

Create a CustomMaker that replace existing Maker #1250

Closed cavasinf closed 5 months ago

cavasinf commented 1 year ago

Hi, I want to make some changes with the current crud maker command:

  1. Create sub-folders
  2. PHP files generated
  3. Twig files generated
  4. Maybe Other..

Since we are not able to extend existing Maker NOR change skeletons, I want to "override" the existing make:crud from MakerBundle.

I've created my own CrudMaker in src/Maker with the command name as make:crud, but it doesn't hide or override the default one.

Here's my Maker:

<?php

namespace App\Maker;

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;

class CrudMaker extends AbstractMaker
{
    public static function getCommandName(): string
    {
        return 'make:crud';
    }

    public static function getCommandDescription(): string
    {
        return 'CRUD Maker override';
    }

    public function configureCommand(Command $command, InputConfiguration $inputConfig)
    {
        // TODO: Implement configureCommand() method.
    }

    public function configureDependencies(DependencyBuilder $dependencies)
    {
        // TODO: Implement configureDependencies() method.
    }

    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
    {
        $this->writeSuccessMessage($io);
        $io->text('CRUD Maker override');
    }
}

Result of php bin/console make::

 Command "make:" is ambiguous.                                                                                               
  Did you mean one of these?                                                                                                  
+     make:crud                  Creates CRUD for Doctrine entity class                                                       
      make:auth                  Creates a Guard authenticator of different flavors                                           
      make:command               Creates a new console command class                                                          
      make:twig-component        Creates a twig (or live) component                                                           
      make:controller            Creates a new controller class               
-     make:crud                  Creates CRUD for Doctrine entity class                                                 
      make:docker:database       Adds a database container to your docker-compose.yaml file                                   
      make:entity                Creates or updates a Doctrine entity class, and optionally an API Platform resource          
      make:fixtures              Creates a new class to load Doctrine fixtures                                                
      make:form                  Creates a new form class                                                                     
      make:message               Creates a new message and handler                                                            
      make:messenger-middleware  Creates a new messenger middleware                                                           
      make:registration-form     Creates a new registration form system                                                       
      make:reset-password        Create controller, entity, and repositories for use with symfonycasts/reset-password-bundle  
      make:serializer:encoder    Creates a new serializer encoder class                                                       
      make:serializer:normalizer Creates a new serializer normalizer class                                                    
      make:subscriber            Creates a new event subscriber class                                                         
      make:twig-extension        Creates a new Twig extension with its runtime class                                          
      make:test                  Creates a new test class                                                                     
      make:validator             Creates a new validator and constraint class                                                 
      make:voter                 Creates a new security voter class                                                           
      make:user                  Creates a new security user class                                                            
      make:migration             Creates a new migration based on database changes                                            
      make:stimulus-controller   Creates a new Stimulus controller                                                            
      make:data-provider         Creates an API Platform data provider                                                        
      make:data-persister        Creates an API Platform data persister                                                       
      make:state-processor       Creates an API Platform state processor                                                      
      make:state-provider        Creates an API Platform state provider.      

ATE, it just moved to the top of the list. Are we also not able to replace existing Makers?

In our company we are used to mechanically typing make:crud when we work. And this custom Maker is only for ONE of our Symfony projects. So I don't want to create a new command like make:front and tell others "in this specific project and case, use this because this and not the default make:crud"... So I really want to make the transition to the new maker as invisible as possible to others.

Version

weaverryan commented 1 year ago

This seems reasonable - we should give the tags an optional priority and the highest priority wins. PR welcome :)

susonwaiba commented 1 year ago

@cavasinf,

As provided above.

# src/Maker/CrudMaker.php

namespace App\Maker;

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;

class CrudMaker extends AbstractMaker
{
    public static function getCommandName(): string
    {
        return 'make:crud';
    }

    public static function getCommandDescription(): string
    {
        return 'CRUD Maker override';
    }

    public function configureCommand(Command $command, InputConfiguration $inputConfig)
    {
        // TODO: Implement configureCommand() method.
    }

    public function configureDependencies(DependencyBuilder $dependencies)
    {
        // TODO: Implement configureDependencies() method.
    }

    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
    {
        $this->writeSuccessMessage($io);
        $io->text('CRUD Maker override');
    }
}

Solution -> overwrite service id to overwrite command

# config/services.yaml

services:
    # ...
    maker.maker.make_crud:
        alias: 'App\Maker\CrudMaker'

Result:-

Placement same and overwrite works as expected.

 make
  make:auth                              Creates a Guard authenticator of different flavors
  make:command                           Creates a new console command class
  make:controller                        Creates a new controller class
  make:crud                              CRUD Maker override
  make:docker:database                   Adds a database container to your docker-compose.yaml file
  make:entity                            Creates or updates a Doctrine entity class, and optionally an API Platform resource
  make:fixtures                          Creates a new class to load Doctrine fixtures
  make:form                              Creates a new form class
cavasinf commented 1 year ago

@susonwaiba is that a temporary fix or the final solution?

susonwaiba commented 1 year ago

@weaverryan can you please, suggest your view.

jrushlow commented 5 months ago

Howdy! - For now, we're are going to go with @susonwaiba solution of overriding the service via an alias in services.yaml. E.g.

# config/services.yaml

services:
    # ...
    maker.maker.make_crud:
        alias: App\Maker\CrudMaker

We currently do not have any immediate plans on implementing command priorities in MakerBundle. However, if someone would like submit PR with such a feature - we'll be happy to give it a review... Thanks!