Skip to content

Instantly share code, notes, and snippets.

@walva
Created August 18, 2018 16:21
Show Gist options
  • Save walva/3eeefae6178077ac7e5aa51feecbe494 to your computer and use it in GitHub Desktop.
Save walva/3eeefae6178077ac7e5aa51feecbe494 to your computer and use it in GitHub Desktop.
Create repository from entity
<?php
/**
* Created by PhpStorm.
* User: Benjamin
* Date: 18-08-18
* Time: 17:47
*/
namespace App\Maker;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\ORM\Mapping\Column;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
class MakeRepository extends AbstractMaker
{
public static function getCommandName(): string
{
return 'make:repository';
}
public function configureCommand(Command $command, InputConfiguration $inputConf)
{
$command
->setDescription('Creates or updates a Doctrine repository class')
->addArgument('name', InputArgument::OPTIONAL, sprintf('Class name of the entity to create or update (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())))
->addOption('regenerate', null, InputOption::VALUE_NONE, 'Instead of adding new fields, simply generate the methods (e.g. getter/setter) for existing fields')
->addOption('overwrite', null, InputOption::VALUE_NONE, 'Overwrite any existing getter/setter methods')
->setHelp(file_get_contents(__DIR__.'/../../templates/help/MakeRepository.txt'))
;
$inputConf->setArgumentAsNonInteractive('name');
}
public function configureDependencies(DependencyBuilder $dependencies)
{
// guarantee DoctrineBundle
$dependencies->addClassDependency(
DoctrineBundle::class,
'orm'
);
// guarantee ORM
$dependencies->addClassDependency(
Column::class,
'orm'
);
}
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
{
if (\PHP_VERSION_ID < 70100) {
throw new RuntimeCommandException('The make:entity command requires that you use PHP 7.1 or higher.');
}
$overwrite = $input->getOption('overwrite');
$regenerate = $input->getOption('regenerate');
$entityClassDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
'Entity\\'
);
$repositoryClassDetails = $generator->createClassNameDetails(
$entityClassDetails->getRelativeName(),
'Repository\\',
'Repository'
);
$entityAlias = strtolower($entityClassDetails->getShortName()[0]);
$generator->generateClass(
$repositoryClassDetails->getFullName(),
'doctrine/Repository.tpl.php',
[
'entity_full_class_name' => $entityClassDetails->getFullName(),
'entity_class_name' => $entityClassDetails->getShortName(),
'entity_alias' => $entityAlias,
]
);
$generator->writeChanges();
}
}
App\Maker\:
resource: '../src/Maker'
tags: ['maker.command']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment