Search code examples
phpsymfonydoctrineentityauto-generate

Custom Symfony generation scripts for entities


I'm just wondering is there any way to modify entity generation scripts (called with generate:doctrine:entity).

For example script creates EntityRepository like this

use Doctrine\ORM\EntityRepository;

/**
 * FreeTokenRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class FreeTokenRepository extends EntityRepository
{
}

but I would like to have EntityRepository extend my own Repository subclass (and also use different conventions for brackets) like this

use AppBundle\Model\Repository;

/**
 * FreeTokenRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class FreeTokenRepository extends Repository {
}

Is there any way to customize symfony/doctrine entity creation scripts? And all other generation scripts? So I don't have to change my autogenerated classes each time?


Solution

  • We created our own command for that. I'll share the code here with you, because it seems to match perfectly with your requirements.

    Of course you'll have to adapt some folders etc. but basically it does exactly what you're asking for. Just tweak it to your other needs.

    Just call app (Symfony 2) or bin (Symfony 3) /console company:createrepository MyEntity Extends and it'll create your Repository File.

    <?php
    
    namespace Company\MyBundle\Command;
    
    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\Question\ConfirmationQuestion;
    use Symfony\Component\HttpKernel\Kernel;
    
    /**
     * Creates a Repository File for a given Entity Class
     *
     * You will find a file called EntityRepository.php in Repositories folder
     * Reformat the output code and enjoy
    *
    *
    */
    class CreaterepositoryCommand extends ContainerAwareCommand
    {
    /**
     *
     */
    protected function configure()
    {
        $this
            ->setName('company:createrepository')
            ->setDescription('Create an Repository for entity class')
            ->addArgument('entity', InputArgument::REQUIRED, 'Enter an entity class name')
            ->addArgument(
                'extends',
                InputArgument::OPTIONAL,
                'Enter which other Repository should be extended');
    }
    
    /**
     * @param InputInterface  $input
     * @param OutputInterface $output
     *
     * @return int|null|void
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
    
        $manualEntity = $input->getArgument('entity');
    
        if ($input->getArgument('extends')) {
            $extends = $input->getArgument('extends') . "Repository";
            $use = '';
        } else {
            $extends = "EntityRepository";
            $use = '
            use Doctrine\ORM\EntityRepository;
            ';
        }
    
        $fileContent = '
        <?php
    
            namespace Company\MyBundle\Entity\Repositories;
            ' . $use . '
            class ' . $manualEntity . 'Repository extends ' . $extends . '
            {
    
            } ';
    
        /** @var Kernel $kernel */
        $kernel = $this->getContainer()->get('kernel');
        $path = $kernel->locateResource('@CompanyMyBundle');
        $path .= 'Entity/Repositories';
        $fileName = $manualEntity . "Repository.php";
        $fileNameInclPath = $path . "/" . $fileName;
    
        if (file_exists($fileNameInclPath)) {
            $helper = $this->getHelper('question');
            $question = new ConfirmationQuestion(
                '<fg=blue>File: </fg=blue>' . $fileName . '<fg=blue> already exists Overwrite?</fg=blue>',
                false);
    
            if (!$helper->ask($input, $output, $question)) {
                $output->writeln('<fg=red>Aborted by user.</fg=red>');
            } else {
                $fp = fopen($fileNameInclPath, "wb");
                fwrite($fp, $fileContent);
                fclose($fp);
                $output->writeln('<fg=green>File:</fg=green> ' . $fileName . '<fg=green> created</fg=green>');
            }
        } else {
            $fp = fopen($fileNameInclPath, "wb");
            fwrite($fp, $fileContent);
            fclose($fp);
            $output->writeln('<fg=green>File:</fg=green> ' . $fileName . '<fg=green> created</fg=green>');
        }
    }
    }