1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Bundle\Maker; |
4
|
|
|
|
5
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
6
|
|
|
use Symfony\Bundle\MakerBundle\ConsoleStyle; |
7
|
|
|
use Symfony\Bundle\MakerBundle\DependencyBuilder; |
8
|
|
|
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; |
9
|
|
|
use Symfony\Bundle\MakerBundle\Generator; |
10
|
|
|
use Symfony\Bundle\MakerBundle\InputConfiguration; |
11
|
|
|
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker; |
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Kevin Bond <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class MakeFactory extends AbstractMaker |
21
|
|
|
{ |
22
|
|
|
private ManagerRegistry $managerRegistry; |
23
|
|
|
|
24
|
|
|
public function __construct(ManagerRegistry $managerRegistry) |
25
|
|
|
{ |
26
|
|
|
$this->managerRegistry = $managerRegistry; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function getCommandName(): string |
30
|
|
|
{ |
31
|
|
|
return 'make:factory'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function configureCommand(Command $command, InputConfiguration $inputConfig): void |
35
|
|
|
{ |
36
|
|
|
$command |
37
|
|
|
->setDescription('Creates a custom factory for a Doctrine entity class') |
38
|
|
|
->addArgument('entity', InputArgument::OPTIONAL, 'Entity class to create a factory for') |
39
|
|
|
->addOption('test', null, InputOption::VALUE_NONE, 'Create in <fg=yellow>tests/</> instead of <fg=yellow>src/</>') |
40
|
|
|
; |
41
|
|
|
|
42
|
|
|
$inputConfig->setArgumentAsNonInteractive('entity'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void |
46
|
|
|
{ |
47
|
|
|
if ($input->getArgument('entity')) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$argument = $command->getDefinition()->getArgument('entity'); |
52
|
|
|
$entity = $io->choice($argument->getDescription(), $this->entityChoices()); |
53
|
|
|
|
54
|
|
|
$input->setArgument('entity', $entity); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void |
58
|
|
|
{ |
59
|
|
|
if (!$input->getOption('test')) { |
60
|
|
|
$io->text('// Note: pass <fg=yellow>--test</> if you want to generate factories in your <fg=yellow>tests/</> directory'); |
61
|
|
|
$io->newLine(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$class = $input->getArgument('entity'); |
65
|
|
|
|
66
|
|
|
if (!\class_exists($class)) { |
|
|
|
|
67
|
|
|
$class = $generator->createClassNameDetails($class, 'Entity\\')->getFullName(); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (!\class_exists($class)) { |
71
|
|
|
throw new RuntimeCommandException(\sprintf('Entity "%s" not found.', $input->getArgument('entity'))); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$entity = new \ReflectionClass($class); |
75
|
|
|
$factory = $generator->createClassNameDetails( |
76
|
|
|
$entity->getShortName(), |
77
|
|
|
$input->getOption('test') ? 'Tests\\Factory' : 'Factory', |
78
|
|
|
'Factory' |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$repository = new \ReflectionClass($this->managerRegistry->getRepository($entity->getName())); |
82
|
|
|
|
83
|
|
|
if (0 !== \mb_strpos($repository->getName(), $generator->getRootNamespace())) { |
84
|
|
|
// not using a custom repository |
85
|
|
|
$repository = null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$generator->generateClass( |
89
|
|
|
$factory->getFullName(), |
90
|
|
|
__DIR__.'/../Resources/skeleton/Factory.tpl.php', |
91
|
|
|
[ |
92
|
|
|
'entity' => $entity, |
93
|
|
|
'repository' => $repository, |
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$generator->writeChanges(); |
98
|
|
|
|
99
|
|
|
$this->writeSuccessMessage($io); |
100
|
|
|
|
101
|
|
|
$io->text([ |
102
|
|
|
'Next: Open your new factory and set default values/states.', |
103
|
|
|
'Find the documentation at https://github.com/zenstruck/foundry#model-factories', |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function configureDependencies(DependencyBuilder $dependencies): void |
108
|
|
|
{ |
109
|
|
|
// noop |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function entityChoices(): array |
113
|
|
|
{ |
114
|
|
|
// todo remove choices that already have a factory |
115
|
|
|
$choices = []; |
116
|
|
|
|
117
|
|
|
foreach ($this->managerRegistry->getManagers() as $manager) { |
118
|
|
|
foreach ($manager->getMetadataFactory()->getAllMetadata() as $metadata) { |
119
|
|
|
$choices[] = $metadata->getName(); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $choices; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|