实体的自定义Symfony生成脚本

时间:2016-10-17 08:35:52

标签: php symfony doctrine entity auto-generate

我只是想知道是否有任何方法可以修改实体生成脚本(使用generate:doctrine:entity调用)。

例如,脚本会像这样创建EntityRepository

use AppBundle\Model\Repository;

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

但我想让EntityRepository扩展我自己的Repository子类(并且也使用不同的括号约定),如下所示

{{1}}

有没有办法自定义symfony / doctrine实体创建脚本?和所有其他代脚本?所以我不必每次都更改自动生成的类?

1 个答案:

答案 0 :(得分:1)

我们为此创建了自己的命令。我将在这里与您分享代码,因为它似乎与您的要求完全匹配。

当然你必须调整一些文件夹等,但基本上它确实是你所要求的。 只需根据您的其他需求进行调整即可。

只需调用app(Symfony 2)或bin(Symfony 3)/ console company:createrepository MyEntity Extends,它将创建您的存储库文件。

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