2727use Symfony \Component \Console \Input \InputArgument ;
2828use Symfony \Component \Console \Input \InputInterface ;
2929use Symfony \Component \Console \Input \InputOption ;
30+ use Symfony \Component \Filesystem \Path ;
3031
3132final class MakeResource extends AbstractMaker implements InputAwareMakerInterface
3233{
3334 const NAMESPACE_PREFIX = 'Infrastructure \\ApiPlatform \\' ;
3435 const CONFIG_PATH = 'config/packages/api_platform.yaml ' ;
35- const CONFIG_PATH_XML = 'src/ Infrastructure/ApiPlatform/Config ' ;
36+ const CONFIG_PATH_XML = 'Infrastructure/ApiPlatform/Config ' ;
3637
3738 const CONFIG_FLAVOR_ATTRIBUTE = 'attribute ' ;
3839 const CONFIG_FLAVOR_XML = 'xml ' ;
@@ -76,6 +77,13 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
7677 'Config flavor to create (attribute|xml). ' ,
7778 null
7879 )
80+ ->addOption (
81+ 'base-path ' ,
82+ null ,
83+ InputOption::VALUE_REQUIRED ,
84+ 'Base path from which to generate model & config. ' ,
85+ null
86+ )
7987 ;
8088 }
8189
@@ -102,13 +110,21 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
102110 $ configFlavor = $ io ->choice (
103111 'Config flavor to create (attribute|xml). (<fg=yellow>%sModel</>) ' ,
104112 [
105- ' attribute ' => 'PHP attributes ' ,
106- ' xml ' => 'XML mapping ' ,
113+ self :: CONFIG_FLAVOR_ATTRIBUTE => 'PHP attributes ' ,
114+ self :: CONFIG_PATH_XML => 'XML mapping ' ,
107115 ],
108- ' attribute '
116+ self :: CONFIG_FLAVOR_ATTRIBUTE
109117 );
110118 $ input ->setOption ('config ' , $ configFlavor );
111119 }
120+
121+ if (null === $ input ->getOption ('base-path ' )) {
122+ $ basePath = $ io ->ask (
123+ 'Which base path should be used? Default is " ' . PathGenerator::DEFAULT_BASE_PATH . '" ' ,
124+ PathGenerator::DEFAULT_BASE_PATH ,
125+ );
126+ $ input ->setOption ('base-path ' , $ basePath );
127+ }
112128 }
113129
114130 /**
@@ -118,37 +134,41 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
118134 {
119135 $ baseName = $ input ->getArgument ('name ' );
120136 $ configFlavor = $ input ->getOption ('config ' );
137+ $ pathGenerator = new PathGenerator ($ input ->getOption ('base-path ' ));
138+
139+ if (!in_array ($ configFlavor , [self ::CONFIG_FLAVOR_ATTRIBUTE , self ::CONFIG_PATH_XML ])) {
140+ throw new RuntimeCommandException ('Unknown config flavor: ' . $ configFlavor );
141+ }
121142
122143 $ modelClassNameDetails = $ generator ->createClassNameDetails (
123144 $ baseName ,
124- 'Domain \\Model \\' ,
125- '' ,
145+ $ pathGenerator ->namespacePrefix ('Domain \\Model \\' ),
126146 );
127147
128148 if (!class_exists ($ modelClassNameDetails ->getFullName ())) {
129149 throw new RuntimeCommandException ("Could not find model {$ modelClassNameDetails ->getFullName ()}! " );
130150 }
131151
132- $ identityClassNameDetails = $ this ->ensureIdentity ($ modelClassNameDetails , $ generator );
152+ $ identityClassNameDetails = $ this ->ensureIdentity ($ modelClassNameDetails , $ generator, $ pathGenerator );
133153
134154 $ classNameDetails = $ generator ->createClassNameDetails (
135155 $ baseName ,
136- self ::NAMESPACE_PREFIX . 'Resource ' ,
156+ $ pathGenerator -> namespacePrefix ( self ::NAMESPACE_PREFIX . 'Resource ' ) ,
137157 'Resource ' ,
138158 );
139159
140- $ this ->ensureConfig ($ generator , $ configFlavor );
160+ $ this ->ensureConfig ($ generator , $ pathGenerator , $ configFlavor );
141161
142162 $ providerClassNameDetails = $ generator ->createClassNameDetails (
143163 $ baseName ,
144- self ::NAMESPACE_PREFIX . 'State ' ,
164+ $ pathGenerator -> namespacePrefix ( self ::NAMESPACE_PREFIX . 'State ' ) ,
145165 'Provider ' ,
146166 );
147167 $ this ->generateProvider ($ providerClassNameDetails , $ generator );
148168
149169 $ processorClassNameDetails = $ generator ->createClassNameDetails (
150170 $ baseName ,
151- self ::NAMESPACE_PREFIX . 'State ' ,
171+ $ pathGenerator -> namespacePrefix ( self ::NAMESPACE_PREFIX . 'State ' ) ,
152172 'Processor ' ,
153173 );
154174 $ this ->generateProcessor ($ processorClassNameDetails , $ generator );
@@ -178,7 +198,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
178198 );
179199
180200 if ($ configFlavor === self ::CONFIG_FLAVOR_XML ) {
181- $ targetPathResourceConfig = self ::CONFIG_PATH_XML . '/ ' . $ classNameDetails ->getShortName () . '.xml ' ;
201+ $ targetPathResourceConfig = $ pathGenerator -> path ( ' src/ ' , self ::CONFIG_PATH_XML . '/ ' . $ classNameDetails ->getShortName () . '.xml ' ) ;
182202 $ generator ->generateFile (
183203 $ targetPathResourceConfig ,
184204 __DIR__ .'/../Resources/skeleton/resource/ResourceXmlConfig.tpl.php ' ,
@@ -190,7 +210,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
190210 ]
191211 );
192212
193- $ targetPathPropertiesConfig = self ::CONFIG_PATH_XML . '/ ' . $ classNameDetails ->getShortName () . 'Properties.xml ' ;
213+ $ targetPathPropertiesConfig = $ pathGenerator -> path ( ' src/ ' , self ::CONFIG_PATH_XML . '/ ' . $ classNameDetails ->getShortName () . 'Properties.xml ' ) ;
194214 $ generator ->generateFile (
195215 $ targetPathPropertiesConfig ,
196216 __DIR__ .'/../Resources/skeleton/resource/PropertiesXmlConfig.tpl.php ' ,
@@ -210,13 +230,14 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
210230 * ensure custom resource path(s) are added to config
211231 *
212232 * @param Generator $generator
233+ * @param PathGenerator $pathGenerator
213234 * @param string $configFlavor
214235 * @return void
215236 */
216- private function ensureConfig (Generator $ generator , string $ configFlavor ): void
237+ private function ensureConfig (Generator $ generator , PathGenerator $ pathGenerator , string $ configFlavor ): void
217238 {
218- $ customResourcePath = '%kernel.project_dir%/src/ Infrastructure/ApiPlatform/Resource ' ;
219- $ customConfigPath = '%kernel.project_dir%/ ' . self ::CONFIG_PATH_XML ;
239+ $ customResourcePath = $ pathGenerator -> path ( '%kernel.project_dir%/src ' , ' Infrastructure/ApiPlatform/Resource ') ;
240+ $ customConfigPath = $ pathGenerator -> path ( '%kernel.project_dir%/src ' , self ::CONFIG_PATH_XML ) ;
220241
221242 if (!$ this ->fileManager ->fileExists (self ::CONFIG_PATH )) {
222243 $ generator ->generateFile (
@@ -248,8 +269,9 @@ private function ensureConfig(Generator $generator, string $configFlavor): void
248269 * @param ClassNameDetails $providerClassNameDetails
249270 * @param Generator $generator
250271 * @return void
272+ * @throws \Exception
251273 */
252- private function generateProvider (ClassNameDetails $ providerClassNameDetails , Generator $ generator )
274+ private function generateProvider (ClassNameDetails $ providerClassNameDetails , Generator $ generator ): void
253275 {
254276 $ templateVars = [
255277 'use_statements ' => new UseStatementGenerator ([
@@ -272,8 +294,9 @@ private function generateProvider(ClassNameDetails $providerClassNameDetails, Ge
272294 * @param ClassNameDetails $processorClassNameDetails
273295 * @param Generator $generator
274296 * @return void
297+ * @throws \Exception
275298 */
276- private function generateProcessor (ClassNameDetails $ processorClassNameDetails , Generator $ generator )
299+ private function generateProcessor (ClassNameDetails $ processorClassNameDetails , Generator $ generator ): void
277300 {
278301 $ templateVars = [
279302 'use_statements ' => new UseStatementGenerator ([
@@ -295,13 +318,14 @@ private function generateProcessor(ClassNameDetails $processorClassNameDetails,
295318 /**
296319 * @param ClassNameDetails $modelClassNameDetails
297320 * @param Generator $generator
321+ * @param PathGenerator $pathGenerator
298322 * @return ClassNameDetails
299323 */
300- private function ensureIdentity (ClassNameDetails $ modelClassNameDetails , Generator $ generator ): ClassNameDetails
324+ private function ensureIdentity (ClassNameDetails $ modelClassNameDetails , Generator $ generator, PathGenerator $ pathGenerator ): ClassNameDetails
301325 {
302326 $ idEntity = $ generator ->createClassNameDetails (
303327 $ modelClassNameDetails ->getShortName (),
304- 'Domain \\Model \\ValueObject \\Identity ' ,
328+ $ pathGenerator -> namespacePrefix ( 'Domain \\Model \\ValueObject \\Identity ' ) ,
305329 'Id ' ,
306330 );
307331
@@ -311,7 +335,7 @@ private function ensureIdentity(ClassNameDetails $modelClassNameDetails, Generat
311335
312336 $ uuidEntity = $ generator ->createClassNameDetails (
313337 $ modelClassNameDetails ->getShortName (),
314- 'Domain \\Model \\ValueObject \\Identity ' ,
338+ $ pathGenerator -> namespacePrefix ( 'Domain \\Model \\ValueObject \\Identity ' ) ,
315339 'Uuid ' ,
316340 );
317341
0 commit comments