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
|
|
|
use Zenstruck\Foundry\ModelFactory; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Kevin Bond <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class MakeFactory extends AbstractMaker |
22
|
|
|
{ |
23
|
|
|
/** @var ManagerRegistry */ |
24
|
|
|
private $managerRegistry; |
25
|
20 |
|
|
26
|
|
|
/** @var string[] */ |
27
|
20 |
|
private $entitiesWithFactories; |
28
|
20 |
|
|
29
|
|
|
public function __construct(ManagerRegistry $managerRegistry, \Traversable $factories) |
30
|
4 |
|
{ |
31
|
|
|
$this->managerRegistry = $managerRegistry; |
32
|
4 |
|
$this->entitiesWithFactories = \array_map( |
33
|
|
|
static function(ModelFactory $factory) { |
34
|
|
|
return $factory::getEntityClass(); |
35
|
20 |
|
}, |
36
|
|
|
\iterator_to_array($factories) |
37
|
|
|
); |
38
|
20 |
|
} |
39
|
20 |
|
|
40
|
20 |
|
public static function getCommandName(): string |
41
|
|
|
{ |
42
|
|
|
return 'make:factory'; |
43
|
20 |
|
} |
44
|
20 |
|
|
45
|
|
|
public static function getCommandDescription(): string |
46
|
20 |
|
{ |
47
|
|
|
return 'Creates a Foundry model factory for a Doctrine entity class'; |
48
|
20 |
|
} |
49
|
12 |
|
|
50
|
|
|
public function configureCommand(Command $command, InputConfiguration $inputConfig): void |
51
|
|
|
{ |
52
|
8 |
|
$command |
53
|
4 |
|
->setDescription(self::getCommandDescription()) |
54
|
4 |
|
->addArgument('entity', InputArgument::OPTIONAL, 'Entity class to create a factory for') |
55
|
|
|
->addOption('namespace', null, InputOption::VALUE_REQUIRED, 'Customize the namespace for generated factories', 'Factory') |
56
|
|
|
->addOption('test', null, InputOption::VALUE_NONE, 'Create in <fg=yellow>tests/</> instead of <fg=yellow>src/</>') |
57
|
8 |
|
; |
58
|
8 |
|
|
59
|
|
|
$inputConfig->setArgumentAsNonInteractive('entity'); |
60
|
8 |
|
} |
61
|
8 |
|
|
62
|
|
|
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void |
63
|
20 |
|
{ |
64
|
|
|
if ($input->getArgument('entity')) { |
65
|
20 |
|
return; |
66
|
|
|
} |
67
|
20 |
|
|
68
|
4 |
|
if (!$input->getOption('test')) { |
69
|
|
|
$io->text('// Note: pass <fg=yellow>--test</> if you want to generate factories in your <fg=yellow>tests/</> directory'); |
70
|
|
|
$io->newLine(); |
71
|
20 |
|
} |
72
|
4 |
|
|
73
|
|
|
$argument = $command->getDefinition()->getArgument('entity'); |
74
|
|
|
$entity = $io->choice($argument->getDescription(), $this->entityChoices()); |
75
|
16 |
|
|
76
|
16 |
|
$input->setArgument('entity', $entity); |
77
|
16 |
|
} |
78
|
16 |
|
|
79
|
16 |
|
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void |
80
|
|
|
{ |
81
|
|
|
$class = $input->getArgument('entity'); |
82
|
16 |
|
|
83
|
|
|
if (!\class_exists($class)) { |
|
|
|
|
84
|
16 |
|
$class = $generator->createClassNameDetails($class, 'Entity\\')->getFullName(); |
|
|
|
|
85
|
|
|
} |
86
|
16 |
|
|
87
|
|
|
if (!\class_exists($class)) { |
88
|
|
|
throw new RuntimeCommandException(\sprintf('Entity "%s" not found.', $input->getArgument('entity'))); |
|
|
|
|
89
|
16 |
|
} |
90
|
16 |
|
|
91
|
16 |
|
$namespace = $input->getOption('namespace'); |
92
|
|
|
|
93
|
16 |
|
// strip maker's root namespace if set |
94
|
16 |
|
if (0 === \mb_strpos($namespace, $generator->getRootNamespace())) { |
95
|
|
|
$namespace = \mb_substr($namespace, \mb_strlen($generator->getRootNamespace())); |
96
|
|
|
} |
97
|
|
|
|
98
|
16 |
|
$namespace = \trim($namespace, '\\'); |
99
|
|
|
|
100
|
16 |
|
// if creating in tests dir, ensure namespace prefixed with Tests\ |
101
|
|
|
if ($input->getOption('test') && 0 !== \mb_strpos($namespace, 'Tests\\')) { |
102
|
16 |
|
$namespace = 'Tests\\'.$namespace; |
103
|
16 |
|
} |
104
|
|
|
|
105
|
|
|
$entity = new \ReflectionClass($class); |
|
|
|
|
106
|
16 |
|
$factory = $generator->createClassNameDetails($entity->getShortName(), $namespace, 'Factory'); |
107
|
|
|
|
108
|
20 |
|
$repository = new \ReflectionClass($this->managerRegistry->getRepository($entity->getName())); |
109
|
|
|
|
110
|
|
|
if (0 !== \mb_strpos($repository->getName(), $generator->getRootNamespace())) { |
111
|
20 |
|
// not using a custom repository |
112
|
|
|
$repository = null; |
113
|
8 |
|
} |
114
|
|
|
|
115
|
8 |
|
$generator->generateClass( |
116
|
|
|
$factory->getFullName(), |
117
|
8 |
|
__DIR__.'/../Resources/skeleton/Factory.tpl.php', |
118
|
8 |
|
[ |
119
|
8 |
|
'entity' => $entity, |
120
|
|
|
'repository' => $repository, |
121
|
|
|
] |
122
|
|
|
); |
123
|
8 |
|
|
124
|
|
|
$generator->writeChanges(); |
125
|
8 |
|
|
126
|
|
|
$this->writeSuccessMessage($io); |
127
|
|
|
|
128
|
|
|
$io->text([ |
129
|
|
|
'Next: Open your new factory and set default values/states.', |
130
|
|
|
'Find the documentation at https://github.com/zenstruck/foundry#model-factories', |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function configureDependencies(DependencyBuilder $dependencies): void |
135
|
|
|
{ |
136
|
|
|
// noop |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function entityChoices(): array |
140
|
|
|
{ |
141
|
|
|
$choices = []; |
142
|
|
|
|
143
|
|
|
foreach ($this->managerRegistry->getManagers() as $manager) { |
144
|
|
|
foreach ($manager->getMetadataFactory()->getAllMetadata() as $metadata) { |
145
|
|
|
if (!\in_array($metadata->getName(), $this->entitiesWithFactories, true)) { |
146
|
|
|
$choices[] = $metadata->getName(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
\sort($choices); |
152
|
|
|
|
153
|
|
|
if (empty($choices)) { |
154
|
|
|
throw new RuntimeCommandException('No entities found.'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $choices; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|