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