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