实体的自定义 Symfony 生成脚本
Custom Symfony generation scripts for entities
我只是想知道是否有任何方法可以修改实体生成脚本(使用 generate:doctrine:entity 调用)。
例如脚本像这样创建 EntityRepository
use Doctrine\ORM\EntityRepository;
/**
* FreeTokenRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class FreeTokenRepository extends EntityRepository
{
}
但我想让 EntityRepository 像这样扩展我自己的 Repository 子类(并且对括号使用不同的约定)
use AppBundle\Model\Repository;
/**
* FreeTokenRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class FreeTokenRepository extends Repository {
}
有什么方法可以自定义 symfony/doctrine 实体创建脚本吗?还有所有其他生成脚本?所以我不必每次都更改自动生成的 类?
我们为此创建了自己的命令。我将在这里与您分享代码,因为它似乎完全符合您的要求。
当然,您必须调整一些文件夹等,但基本上它完全符合您的要求。
只需根据您的其他需求进行调整即可。
只需调用 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>');
}
}
}
我只是想知道是否有任何方法可以修改实体生成脚本(使用 generate:doctrine:entity 调用)。
例如脚本像这样创建 EntityRepository
use Doctrine\ORM\EntityRepository;
/**
* FreeTokenRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class FreeTokenRepository extends EntityRepository
{
}
但我想让 EntityRepository 像这样扩展我自己的 Repository 子类(并且对括号使用不同的约定)
use AppBundle\Model\Repository;
/**
* FreeTokenRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class FreeTokenRepository extends Repository {
}
有什么方法可以自定义 symfony/doctrine 实体创建脚本吗?还有所有其他生成脚本?所以我不必每次都更改自动生成的 类?
我们为此创建了自己的命令。我将在这里与您分享代码,因为它似乎完全符合您的要求。
当然,您必须调整一些文件夹等,但基本上它完全符合您的要求。 只需根据您的其他需求进行调整即可。
只需调用 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>');
}
}
}