You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RepositoryMaker.php 4.4KB

3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Lc\SovBundle\Maker;
  3. use Doctrine\Common\Annotations\Annotation;
  4. use Symfony\Bundle\MakerBundle\ConsoleStyle;
  5. use Symfony\Bundle\MakerBundle\DependencyBuilder;
  6. use Symfony\Bundle\MakerBundle\Generator;
  7. use Symfony\Bundle\MakerBundle\InputConfiguration;
  8. use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
  9. use Symfony\Component\Console\Command\Command;
  10. use Symfony\Component\Console\Input\InputArgument;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use function Symfony\Component\String\u;
  13. class RepositoryMaker extends AbstractMaker
  14. {
  15. public static function getCommandName(): string
  16. {
  17. return 'make:repository';
  18. }
  19. public static function getCommandDescription(): string
  20. {
  21. return 'Creates a repository, query and store';
  22. }
  23. public function configureCommand(Command $command, InputConfiguration $inputConf)
  24. {
  25. $command
  26. ->addArgument(
  27. 'namespace',
  28. InputArgument::OPTIONAL,
  29. 'Namespace (e.g. <fg=yellow>Lc\SovBundle\Repository</>)'
  30. )
  31. ->addArgument(
  32. 'domain',
  33. InputArgument::OPTIONAL,
  34. 'Domain (e.g. <fg=yellow>Order</>)'
  35. )
  36. ->addArgument(
  37. 'entity-class',
  38. InputArgument::OPTIONAL,
  39. 'Define the entity (e.g. <fg=yellow>MyEntity</>)'
  40. )
  41. ->setHelp('');
  42. }
  43. public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
  44. {
  45. $entityDetails = $generator->createClassNameDetails(
  46. $input->getArgument('entity-class'),
  47. 'Entity\\',
  48. ''
  49. );
  50. $options = [
  51. 'namespace_path' => $input->getArgument('namespace'),
  52. 'domain' => $input->getArgument('domain'),
  53. 'entity_class' => $input->getArgument('entity-class'),
  54. 'entity' => $entityDetails->getFullName(),
  55. ];
  56. // repository
  57. $repositoryClass = $input->getArgument('entity-class') . 'Repository';
  58. $this->generateClass($generator, $repositoryClass, 'Repository\\', 'repository.tpl.php', $options);
  59. // repository query
  60. $repositoryQueryClass = $input->getArgument('entity-class') . 'RepositoryQuery';
  61. $options['class_name_repository'] = $repositoryClass;
  62. $this->generateClass($generator, $repositoryQueryClass, 'Repository\\', 'repository_query.tpl.php', $options);
  63. // store
  64. $storeClass = $input->getArgument('entity-class') . 'Store';
  65. $options['class_name_repository_query'] = $repositoryQueryClass;
  66. $this->generateClass($generator, $storeClass, 'Repository\\', 'store.tpl.php', $options);
  67. // interfaces
  68. //$this->generateInterface($input, $generator, $repositoryQueryClass . 'Interface');
  69. //$this->generateInterface($input, $generator, $storeClass . 'Interface');
  70. // write files
  71. $generator->writeChanges();
  72. $this->writeSuccessMessage($io);
  73. $io->text('Next: Open your new factory class and configure it!');
  74. }
  75. public function generateClass($generator, $class, $namespacePrefix, $tpl, $options)
  76. {
  77. $repositoryDetails = $generator->createClassNameDetails(
  78. $class,
  79. $namespacePrefix,
  80. ''
  81. );
  82. $generator->generateController(
  83. $repositoryDetails->getFullName(),
  84. __DIR__ . '/../Resources/maker/repository/'.$tpl,
  85. $options
  86. );
  87. }
  88. public function generateInterface($input, $generator, $interfaceName)
  89. {
  90. $generator->generateFile(
  91. $generator->getRootDirectory() . '/src/Repository/' . $interfaceName . '.php',
  92. __DIR__ . '/../Resources/maker/repository/interface.tpl.php',
  93. [
  94. 'namespace_path' => $input->getArgument('namespace'),
  95. 'domain' => $input->getArgument('domain'),
  96. 'interface_name' => $interfaceName,
  97. ]
  98. );
  99. }
  100. public function configureDependencies(DependencyBuilder $dependencies)
  101. {
  102. $dependencies->addClassDependency(
  103. Annotation::class,
  104. 'doctrine/annotations'
  105. );
  106. }
  107. }