1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\EasyExtendsBundle\Command; |
15
|
|
|
|
16
|
|
|
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata; |
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Generate Application entities from bundle entities. |
26
|
|
|
* |
27
|
|
|
* @author Thomas Rabaix <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class GenerateCommand extends ContainerAwareCommand |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
protected function configure(): void |
35
|
|
|
{ |
36
|
|
|
$this |
37
|
|
|
->setName('sonata:easy-extends:generate') |
38
|
|
|
->setHelp( |
39
|
|
|
<<<'EOT' |
40
|
|
|
The <info>easy-extends:generate:entities</info> command generating a valid bundle structure from a Vendor Bundle. |
41
|
|
|
|
42
|
|
|
<info>ie: ./app/console sonata:easy-extends:generate SonataUserBundle</info> |
43
|
|
|
EOT |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$this->setDescription('Create entities used by Sonata\'s bundles'); |
47
|
|
|
|
48
|
|
|
$this->addArgument('bundle', InputArgument::IS_ARRAY, 'The bundle name to "easy-extends"'); |
49
|
|
|
$this->addOption( |
50
|
|
|
'dest', |
51
|
|
|
'd', |
52
|
|
|
InputOption::VALUE_OPTIONAL, |
53
|
|
|
'The base folder where the Application will be created', |
54
|
|
|
false |
55
|
|
|
); |
56
|
|
|
$this->addOption('namespace', 'ns', InputOption::VALUE_OPTIONAL, 'The namespace for the classes', false); |
57
|
|
|
$this->addOption('namespace_prefix', 'nsp', InputOption::VALUE_OPTIONAL, 'The namespace prefix for the classes', false); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
64
|
|
|
{ |
65
|
|
|
$destOption = $input->getOption('dest'); |
66
|
|
|
if ($destOption) { |
67
|
|
|
$dest = realpath($destOption); |
68
|
|
|
if (false === $dest) { |
69
|
|
|
throw new \RuntimeException(sprintf( |
70
|
|
|
'The provided destination folder \'%s\' does not exist!', |
71
|
|
|
$destOption |
72
|
|
|
)); |
73
|
|
|
} |
74
|
|
|
} else { |
75
|
|
|
$dest = $this->getContainer()->get('kernel')->getRootDir(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$namespace = $input->getOption('namespace'); |
79
|
|
|
if ($namespace) { |
80
|
|
|
if (!preg_match('/^(?:(?:[[:alnum:]]+|:vendor)\\\\?)+$/', $namespace)) { |
81
|
|
|
throw new \InvalidArgumentException(sprintf( |
82
|
|
|
'The provided namespace "%s" is not a valid namespace!', |
83
|
|
|
$namespace |
84
|
|
|
)); |
85
|
|
|
} |
86
|
|
|
} else { |
87
|
|
|
$namespace = 'Application\:vendor'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$configuration = [ |
91
|
|
|
'application_dir' => sprintf( |
92
|
|
|
'%s%s%s', |
93
|
|
|
$dest, |
94
|
|
|
\DIRECTORY_SEPARATOR, |
95
|
|
|
str_replace('\\', \DIRECTORY_SEPARATOR, $namespace) |
96
|
|
|
), |
97
|
|
|
'namespace' => $namespace, |
98
|
|
|
'namespace_prefix' => '', |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
if ($namespacePrefix = $input->getOption('namespace_prefix')) { |
102
|
|
|
$configuration['namespace_prefix'] = rtrim($namespacePrefix, '\\').'\\'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$bundleNames = $input->getArgument('bundle'); |
106
|
|
|
|
107
|
|
|
if (empty($bundleNames)) { |
108
|
|
|
$output->writeln(''); |
109
|
|
|
$output->writeln('<error>You must provide a bundle name!</error>'); |
110
|
|
|
$output->writeln(''); |
111
|
|
|
$output->writeln(' Bundles availables :'); |
112
|
|
|
/** @var BundleInterface $bundle */ |
113
|
|
|
foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) { |
114
|
|
|
$bundleMetadata = new BundleMetadata($bundle, $configuration); |
115
|
|
|
|
116
|
|
|
if (!$bundleMetadata->isExtendable()) { |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$output->writeln(sprintf(' - %s', $bundle->getName())); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$output->writeln(''); |
124
|
|
|
} else { |
125
|
|
|
foreach ($bundleNames as $bundleName) { |
|
|
|
|
126
|
|
|
$processed = $this->generate($bundleName, $configuration, $output); |
127
|
|
|
|
128
|
|
|
if (!$processed) { |
129
|
|
|
throw new \RuntimeException(sprintf( |
130
|
|
|
'<error>The bundle \'%s\' does not exist or is not registered in the kernel!</error>', |
131
|
|
|
$bundleName |
132
|
|
|
)); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$output->writeln('done!'); |
138
|
|
|
|
139
|
|
|
return 0; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Generates a bundle entities from a bundle name. |
144
|
|
|
*/ |
145
|
|
|
protected function generate(string $bundleName, array $configuration, OutputInterface $output): bool |
146
|
|
|
{ |
147
|
|
|
$processed = false; |
148
|
|
|
|
149
|
|
|
/** @var BundleInterface $bundle */ |
150
|
|
|
foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) { |
151
|
|
|
if ($bundle->getName() !== $bundleName) { |
152
|
|
|
continue; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$processed = true; |
156
|
|
|
$bundleMetadata = new BundleMetadata($bundle, $configuration); |
157
|
|
|
|
158
|
|
|
// generate the bundle file. |
159
|
|
|
if (!$bundleMetadata->isExtendable()) { |
160
|
|
|
$output->writeln(sprintf('Ignoring bundle : "<comment>%s</comment>"', $bundleMetadata->getClass())); |
161
|
|
|
|
162
|
|
|
continue; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// generate the bundle file |
166
|
|
|
if (!$bundleMetadata->isValid()) { |
167
|
|
|
$output->writeln(sprintf( |
168
|
|
|
'%s : <comment>wrong directory structure</comment>', |
169
|
|
|
$bundleMetadata->getClass() |
170
|
|
|
)); |
171
|
|
|
|
172
|
|
|
continue; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$output->writeln(sprintf('Processing bundle : "<info>%s</info>"', $bundleMetadata->getName())); |
176
|
|
|
|
177
|
|
|
$this->getContainer()->get('sonata.easy_extends.generator.bundle') |
178
|
|
|
->generate($output, $bundleMetadata); |
179
|
|
|
|
180
|
|
|
$output->writeln(sprintf('Processing Doctrine ORM : "<info>%s</info>"', $bundleMetadata->getName())); |
181
|
|
|
$this->getContainer()->get('sonata.easy_extends.generator.orm') |
182
|
|
|
->generate($output, $bundleMetadata); |
183
|
|
|
|
184
|
|
|
$output->writeln(sprintf('Processing Doctrine ODM : "<info>%s</info>"', $bundleMetadata->getName())); |
185
|
|
|
$this->getContainer()->get('sonata.easy_extends.generator.odm') |
186
|
|
|
->generate($output, $bundleMetadata); |
187
|
|
|
|
188
|
|
|
$output->writeln(sprintf('Processing Doctrine PHPCR : "<info>%s</info>"', $bundleMetadata->getName())); |
189
|
|
|
$this->getContainer()->get('sonata.easy_extends.generator.phpcr') |
190
|
|
|
->generate($output, $bundleMetadata); |
191
|
|
|
|
192
|
|
|
$output->writeln(sprintf('Processing Serializer config : "<info>%s</info>"', $bundleMetadata->getName())); |
193
|
|
|
$this->getContainer()->get('sonata.easy_extends.generator.serializer') |
194
|
|
|
->generate($output, $bundleMetadata); |
195
|
|
|
|
196
|
|
|
$output->writeln(''); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $processed; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.