您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FactoryMaker.php 3.6KB

3 年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 FactoryMaker extends AbstractMaker
  14. {
  15. public static function getCommandName(): string
  16. {
  17. return 'make:factory';
  18. }
  19. public static function getCommandDescription(): string
  20. {
  21. return 'Creates a factory';
  22. }
  23. public function configureCommand(Command $command, InputConfiguration $inputConf)
  24. {
  25. $command
  26. /*->addArgument(
  27. 'factory-class',
  28. InputArgument::OPTIONAL,
  29. 'Choose a name for your factory (e.g. <fg=yellow>MyEntityFactory</>)'
  30. )*/
  31. ->addArgument(
  32. 'namespace',
  33. InputArgument::OPTIONAL,
  34. 'Namespace (e.g. <fg=yellow>Lc\SovBundle\Factory</>)'
  35. )
  36. ->addArgument(
  37. 'domain',
  38. InputArgument::OPTIONAL,
  39. 'Domain (e.g. <fg=yellow>Order</>)'
  40. )
  41. ->addArgument(
  42. 'entity-class',
  43. InputArgument::OPTIONAL,
  44. 'Define the entity (e.g. <fg=yellow>MyEntity</>)'
  45. )
  46. ->setHelp('');
  47. }
  48. public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
  49. {
  50. $entityDetails = $generator->createClassNameDetails(
  51. $input->getArgument('entity-class'),
  52. 'Entity\\',
  53. ''
  54. );
  55. $factoryClass = $input->getArgument('entity-class') . 'Factory';
  56. $factoryDetails = $generator->createClassNameDetails(
  57. $factoryClass,
  58. 'Factory\\',
  59. ''
  60. );
  61. $options = [
  62. 'namespace_path' => $input->getArgument('namespace'),
  63. 'domain' => $input->getArgument('domain'),
  64. 'entity_class' => $input->getArgument('entity-class'),
  65. 'entity' => $entityDetails->getFullName(),
  66. 'entity_variable' => '$' . u($input->getArgument('entity-class'))->camel()
  67. ];
  68. $generator->generateController(
  69. $factoryDetails->getFullName(),
  70. __DIR__ . '/../Resources/maker/factory/factory.tpl.php',
  71. $options
  72. );
  73. $interfaceName = $factoryClass . 'Interface';
  74. $generator->generateFile(
  75. $generator->getRootDirectory() . '/src/Factory/' . $interfaceName . '.php',
  76. __DIR__ . '/../Resources/maker/factory/factory_interface.tpl.php',
  77. [
  78. 'namespace_path' => $input->getArgument('namespace'),
  79. 'domain' => $input->getArgument('domain'),
  80. 'interface_name' => $interfaceName,
  81. ]
  82. );
  83. $generator->writeChanges();
  84. $this->writeSuccessMessage($io);
  85. $io->text('Next: Open your new factory class and configure it!');
  86. }
  87. public function configureDependencies(DependencyBuilder $dependencies)
  88. {
  89. $dependencies->addClassDependency(
  90. Annotation::class,
  91. 'doctrine/annotations'
  92. );
  93. }
  94. }