1- <?php
1+ <?php declare (strict_types = 1 );
22
33namespace MartenB \Nextras \ORM \Console \Command ;
44
1313class GeneratorCommand extends Command
1414{
1515
16+ /** @var mixed[] */
1617 private $ config = [
17- 'directory ' => NULL ,
18- 'namespace ' => 'App\Model ' ,
19- 'entityExtends ' => 'App\Model\BaseEntity ' ,
20- 'repositoryExtends ' => 'App\Model\BaseRepository ' ,
21- 'mapperExtends ' => 'App\Model\BaseMapper ' ,
18+ 'directory ' => null ,
19+ 'namespace ' => 'App\Model\Orm ' ,
20+ 'entityExtends ' => 'App\Model\Orm\ BaseEntity ' ,
21+ 'repositoryExtends ' => 'App\Model\Orm\ BaseRepository ' ,
22+ 'mapperExtends ' => 'App\Model\Orm\ BaseMapper ' ,
2223 ];
2324
24-
25- protected function configure ()
25+ protected function configure (): void
2626 {
2727 $ this ->setName ('orm:generator ' )
2828 ->setDescription ('Generate entity, repository and mapper ' )
@@ -36,32 +36,46 @@ protected function configure()
3636 ->addOption ('mapperExtends ' , 'me ' , InputOption::VALUE_OPTIONAL , 'Mapper extends class name ' );
3737 }
3838
39-
40- public function setConfig (array $ config = [])
39+ /**
40+ * @param string[] $config
41+ */
42+ public function setConfig (array $ config = []): void
4143 {
4244 $ this ->config = $ config ;
4345 }
4446
45-
46- protected function getOption (InputInterface $ input , string $ name )
47+ protected function getOption (InputInterface $ input , string $ name ): string
4748 {
4849 return $ input ->getOption ($ name ) ?? $ this ->config [$ name ];
4950 }
5051
51-
52- protected function execute (InputInterface $ input , OutputInterface $ output )
52+ protected function execute (InputInterface $ input , OutputInterface $ output ): ?int
5353 {
54+ if (!is_string ($ input ->getArgument ('entityName ' ))) {
55+ throw new Exception ('Argument entityName must be a string. ' );
56+ }
57+
58+ if (is_array ($ input ->getArgument ('repositoryName ' ))) {
59+ throw new Exception ('Argument entityName must be a string or null. ' );
60+ }
61+
62+ if (is_array ($ input ->getArgument ('mapperName ' ))) {
63+ throw new Exception ('Argument entityName must be a string or null. ' );
64+ }
65+
5466 $ directory = $ this ->getOption ($ input , 'directory ' ) . '/ ' . $ input ->getArgument ('entityName ' );
67+
5568 if (is_dir ($ directory )) {
5669 throw new Exception ('Directory already exists. ' );
5770 }
58- mkdir ($ directory , 0777 , TRUE );
71+
72+ mkdir ($ directory , 0777 , true );
5973
6074 $ namespace = $ this ->getOption ($ input , 'namespace ' ) . '\\' . $ input ->getArgument ('entityName ' );
6175
6276 // entity
6377 $ entityName = $ input ->getArgument ('entityName ' );
64- $ file = new PhpFile ;
78+ $ file = new PhpFile () ;
6579 $ file
6680 ->addNamespace ($ namespace )
6781 ->addClass ($ entityName )
@@ -72,27 +86,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
7286
7387 // repository
7488 $ repositoryName = ($ input ->getArgument ('repositoryName ' ) ?? $ input ->getArgument ('entityName ' )) . 'Repository ' ;
75- $ file = new PhpFile ;
89+ $ file = new PhpFile () ;
7690 $ file
7791 ->addNamespace ($ namespace )
7892 ->addClass ($ repositoryName )
7993 ->setExtends ($ this ->getOption ($ input , 'repositoryExtends ' ))
8094 ->addMethod ('getEntityClassNames ' )
81- ->setStatic (TRUE )
95+ ->setStatic (true )
8296 ->setReturnType ('array ' )
8397 ->setBody ('return [ ' . $ input ->getArgument ('entityName ' ) . '::class]; ' );
8498
8599 file_put_contents ($ directory . '/ ' . $ repositoryName . '.php ' , (string ) $ file );
86100
87101 // mapper
88102 $ mapperName = ($ input ->getArgument ('mapperName ' ) ?? ($ input ->getArgument ('repositoryName ' ) ?? $ input ->getArgument ('entityName ' ))) . 'Mapper ' ;
89- $ file = new PhpFile ;
103+ $ file = new PhpFile () ;
90104 $ file
91105 ->addNamespace ($ namespace )
92106 ->addClass ($ mapperName )
93107 ->setExtends ($ this ->getOption ($ input , 'mapperExtends ' ));
94108
95109 file_put_contents ($ directory . '/ ' . $ mapperName . '.php ' , (string ) $ file );
110+
111+ return 0 ;
96112 }
97113
98114}
0 commit comments