petkopara / PetkoparaCrudGeneratorBundle

Symfony3 CRUD generator bundle with pagination, filter, bulk actions and Twitter bootstrap 3.3.6 features.
MIT License
70 stars 17 forks source link

Possibility to generate CRUD for all entities at once #38

Closed romainlaisne closed 7 years ago

romainlaisne commented 7 years ago

Hi,

Would be nice to be able to generate CRUD for all entities at once.

romainlaisne commented 7 years ago

Is there any options for that already?

petkopara commented 7 years ago

Currently there is no option for this and you need to run the generation command for each entity. By the way this is a good idea and I'll implement it in near future.

romainlaisne commented 7 years ago

Here is a command that will generate the crud for all entities dynamically. It uses $em->getMetadataFactory()->getAllMetadata() to retrieve all entities from your applications automatically.

<?php

namespace YOUR_NAMESPACE\Command;

use DateTime;
use Exception;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Style\SymfonyStyle;

class GenerateCrudCommand extends ContainerAwareCommand
{
    private $container;

    private $em;

    private $io;

    private $output;

    private $dryrun;

    protected function configure()
    {
        $this
            ->setName('myprojectname:generateCrud')
            ->setDescription('Generate CRUD for all entities using the Petkopara CRUD generator');

    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->output = $output;
        $this->io = new SymfonyStyle($input, $this->output);
        $this->io->title('Generate CRUD for all entities using the Petkopara CRUD generator');
        $this->io->text('This command will generate the CRUD interface for all entities using Petkopara CRUD generator.');

        if ($this->io->confirm('Proceed and generate the CRUD interface for all entities?')) {

            $entities = array();
            $this->container = $this->getContainer();
            $em = $this->container->get('doctrine.orm.default_entity_manager');
            $meta = $em->getMetadataFactory()->getAllMetadata();

            foreach ($meta as $m) {
                $entities[] = $m->getName();
                $currentEntity = $m->getName();
                $currentEntityShortcutName = $this->convertClassNameToShortcutNotations($currentEntity);

                exec('php bin/console petkopara:generate:crud  --no-interaction --bundle-views --overwrite --entity=' . $currentEntityShortcutName . ' --template=XodmpKernelBundle:General:layout.html.twig' . ' --filter-type=input --format=yml', $output1, $returnvar1);
                $this->io->text("-> CRUD generated for " . $currentEntityShortcutName . '.');
                //$this->io->text('Output:');
                //print_r($output1);
                //die;
            }

            return true;
        }else {
            $this->io->warning("Generation of the CRUD interface aborted.");
        };

        //print_r($entities);

    }

    public function convertClassNameToShortcutNotations($className)
    {
        $cleanClassName = str_replace('\\Entity', '\:', $className);
        $parts = explode('\\', $cleanClassName);
        unset($parts[1]);

        return implode('', $parts);
    }

}